If Statements

In PHP, if statements are the foundation of programming logic. If statements contain statements that are only executed when the condition is satisfied. Let’s go through an example.

Simple If Statement

Example

Result

It worked.

The if statement simply says, if the variable $myVar is equal to 1, then execute the statements inside of the { } brackets. If our variable had been equal to two, the statements inside of the if would not have been executed.

Comparison Operators

You already used one of these above. It was the ==, which asks if the thing before it is equal to the thing after it. If the answer is yes (true), we execute the statement. However, if the answer would have been no (false), we would not have executed the statement.

Example

You can see what each one of the comparison operators do above, but what if you wanted to use two of them simultaneously? You can by using the following logical operators.

Example

If, Else If, and Else

If statements can be just as simple as the examples above, but they can also be more useful when you combine the if statements with else if and else.

Example

Result

else and now x = 20

You might be asking yourself, “What is the point in an else if statement?” Well consider the following examples

Example

Result

if(9 = 8) and now x = 9 else and now x = 20

Why did the if($x == 8) and the else both execute their statements? It is best to think of ifs as completely separate conditionals, but else if and else build off of whichever if they follow. So, if you had an if statement that was below another if statement, the first if statement would be separate from the second if statement just like the example above. However, the second if statement in the example above, has an else built onto it. Since the first if statement’s condition is satisfied, it executes its statements. The second if statement’s condition is not satisfied, but the attached else statement’s condition is satisfied. Therefore, $x is incremented in the first conditional, and “if($x = 8) and now $x = ” . $x is displayed on the screen. Also, we have the second if’s else that sets $x equal to 20, and “else and now $x = ” . $x is displayed on the screen.

If you enjoyed this resource, support me by sharing this page with others.
Share on Facebook
Facebook
Share on Google+
Google+
Share on StumbleUpon
StumbleUpon
Digg this
Digg
Tweet about this on Twitter
Twitter
Share on Reddit
Reddit

Leave a Comment

  1. These If statements are similiar to JavaScript IF statements. Thank God, much easier for me to understand.

  2. In the last example it has the following in the if statement:

    if($x == 8)
    {
    $x++;
    echo "if($x = 8) and now $x = " . $x; //This statement would not be triggered because $x is not equal to 8
    }

    I went back to the PHP variables section where it has the increment and decrement example, and at first I thought you would have to put the ++ or — in back of the variable, but you state $b++ doesn’t increment the variable after we echo it to the screen. So I created a php code to try it out and it work. thanks alot, this stuff is beginning to make alot of sense.

    code I tried:

    <?php

    $myVar = 3;

    echo $myVar++ . "<br>";

    echo $myVar;

    ?>

  3. In the comparison operators example where $x=7

    if($x!=8)
    {
    //this would be not executed because != means that the variable is not equal to the conditional variable
    }

    Actually this would be executed because != means that $x is not equal to 8, and $x=7 so 7 is not equal to 8

  4. In the 2nd box, $x =7; then you have: if(!($x==6))
    {
    //Since $x is equal to 7, this statement would not be executed
    }

    But, while it’s true that $x is not equal to 6, making the inner parenthesis false, that makes the negation, and thus the if statement, true, and the statement "would" be executed. (wouldn’t it?) Doesn’t it read "if it is not true that $x equals 6" …which it isn’t, because $x = 7, so, it "is" true that it’s not true.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.