ElseIf Or Else Must Be Triggered ? Which One Has Priority When Both Are $TRUE? First-Come - First-Serve?

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:


if (condition) {

} elseif (condition) {

} else {

}

I wrote this code:


<!DOCTYPE html>
<html>
    <head>
		<title></title>
	</head>
	<body>
    <?php
    if(1.5<1) {
        echo "1.5<1";
    } elseif (1.5>1) {
        echo "1.5>1";
    } else {
        echo "1.5=1.5";
    }
    ?>
    </body>
</html>

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. :wonky:

I have now rearranged the operator but the code editor still shows the answer as: 1.51.
Here is my code:


<!DOCTYPE html>
<html>
    <head>
		<title></title>
	</head>
	<body>
    <?php
    if(1.5<1) {
        echo "1.5<1";
    } elseif (1.5>1) {
        echo "1.5>1";
    } else {
        echo "1.5=1.5";
    }
    ?>
    </body>
</html>

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.

I think this is easier to read:

<?php
if(1.5<1) {
        echo "1.5<1";
    }
elseif (1.5>1) {
        echo "1.5>1";
    }
else {
        echo "1.5=1.5";
    }
    ?>

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.

Folks,

Are not curly braces needed here to represent the code of ELSE etc. ?
Check the elseif code here:

Looks like this:


<html>
   <body>
   
      <?php
         $d = date("D");
         
         if ($d == "Fri")
            echo "Have a nice weekend!";
         
         elseif ($d == "Sun")
            echo "Have a nice Sunday!"; 
         
         else
            echo "Have a nice day!"; 
      ?>
      
   </body>
</html>

Should have been like this:


<html>
   <body>
   
      <?php
         $d = date("D");
         
         if ($d == "Fri") {
             echo "Have a nice weekend!";
         } elseif ($d == "Sun") {
            echo "Have a nice Sunday!"; 
         } else {
            echo "Have a nice day!"; 
         }
      ?>
      
   </body>
</html>

Or, like this:


<html>
   <body>
   
      <?php
         $d = date("D");
         
         if ($d == "Fri")
            {
                  echo "Have a nice weekend!";
            }
         elseif ($d == "Sun")
            {
                   echo "Have a nice Sunday!"; 
            }
         else
            {
                  echo "Have a nice day!";
            }
      ?>
      
   </body>
</html>

Right ?

PS - I actually use the last example as it’s easier to understand. Like this:

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.

You need to escape output that contains special characters using htmlspecialchars. http://php.net/manual/en/function.htmlspecialchars.php

<!DOCTYPE html>
<html><head><title></title></head>
<body>
<?php
if(1.5<1) {
    echo htmlspecialchars("1.5<1");
} elseif (1.5>1) {
    echo htmlspecialchars("1.5>1");
} else {
    echo htmlspecialchars("1.5=1.5");
}
?>
</body></html>
3 Likes

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! :wink:

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. :unhappy:

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! :wonky:

Nevermind. I am continuing the tutorial reading.

I think it is deliberate to make you think why the rendered amount was not what you expected.

I would have used single quotes to prevent evaluating the string.

1 Like

Single quotes would not have made any difference in this case. It is the browser that is doing the evaluation.

2 Likes

An alternative would be to echo entities.

echo "1.5 &lt; 1";
3 Likes

I am intrigued and will check when on the desktop.

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.

2 Likes

Thanks everyone for their worthy replies!

I checked on the desktop and the last integer is not showing so it looks as though it is the CodeAcademy online editor.

1 Like

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.

1 Like

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.

2 Likes

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

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.

All good. :slight_smile:

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.