Ok. I am continuing the CodeAcademy.com php tutorial that I started yesterday eve (sat).
It is now asking me …
“Write an if/elseif/else statement inside the editor and make it output anything you wish.”
Problem is, it never taught the ElseIf. Only the If & Else.
Anyway, I knew a little about it anyway. Plus, they are giving hints like this:
Now, according to me both the elseIf and Else are true. Since the elseIf comes into play only if the IF is true then the elseIf won’t run since the If was false. Therefore, the else will be triggered.
But, looking at the CodeAcademy editor, I see this:
1.51
Now, that is not right is it ?
Check the attachment for proof.
What is going on there, I wonder.
As far as I know it goes through the options and at the first true option it exits. So you have to be careful in what order you write them.
The else is a default in case none of the elseif’s are true.
Yeah. But, was the code editor showing the right answer or not ? That was the major question.
Anyway, I submitted my 1st sample and the tutorial accepted that as a valid structure.
So if I understand your original question, you were wondering why you were getting 1.51 as the result? And yet, no where are you actually echoing 1.51. Hmm.
What is happening is that certain characters have significance in html. In particular the > character in 1.5 > 1 needs special handling. The code academy tool thinks it is part of html and is swallowing it. Most browsers would actually display it.
Yeah. You are right in your wondering. It didn’t take much wandering off anywhere to wonder what I was wondering. Now did it ? Lol!
To a newbie, all this htmlspecialchars and stuffs are complicated and at this level (in their tutorial) the student has not come across them and so he’d be puzzled like me (even though I have come across htmlspecialchars I still couldn’t figure-out what the heck was wrong!) and so they should have taken precautions. How unreliable they have become on this issue.
Not every one of their students would use a forum like this where they get answers from the likes of you and so they’d get confused, puzzled, flummoxed and not to mention flabbargasted enough to “wander off” elsewhere. Dear me!
No, they are not necessary, however, if you chooes to not use them, you can only use a single line after the IF/ELSE statement. Any lines after 1, must be enclosed in curly brackets.
For readability, it is generally encourage to use curly brackets, but this is very much a user-preference, not a requirement.
Else is not a condition as such, so is neither true or false. It is the fall-back for what to do if “none of the above” evaluate to true. So if the if is false and any number of elseif are false, we do the else.
No. The elseif is evaluated only if the if is false. If the if is true, we do that and everything after is ignored.
Only one thing will happen, according to the first condition to evaluate as true. If none are true, then the else is used.
To be fair to the tutorial, it’s you that added the complexity of using HTML delimiters as part of your output, the tutorial said “make it output anything you wish”. If you’d just output some normal text, you wouldn’t have had a problem. Given that you’ve got lots of other HTML in that bit of code, it’s reasonable to presume that you know what the > and < does.
Agreed, I would think even a beginner’s PHP tutorial should assume a basic knowledge of HTML and how it works in the browser.
This is not a PHP problem, it’s HTML.
I’m sure it was mentioned before, to get a good handle on HTML before attempting to learn PHP.
Maybe you are right about the deliberation or maybe it is a glitch like Ahundiak is saying. I’m leaning more towards the glitch.
But, you are right, I should take precautions myself and use the single quotes whenever possible to prevent any evaluation. Have to get into the habit of single quoting.
Thanks for reminding. I keep forgetting about how the elseif works. A lot of things I keep forgetting. Hence, gonna program my .exe php bot to remember these things and remind me about them too.
To recap …
If the IF works then the ElseIf and Else don’t trigger.
If the IF is false then the script does not immediately “hop” to the ELSE. It first hobbles along one after the other to all the ElseIfs and if any prove TRUE then the ELSE does not get triggered. Else it does.