Home » Tutorials » PHP » If Statements

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
$myVar = 1;
if($myVar==1)
{
    echo "It worked.";
}
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
$x = 7;
if($x===7)
{
    //this would be . $x
}
if($x==="7")
{
    //Since our variable $x is not a string this would be not executed because === is a strict operator, which makes sure that the variable is set to the correct value and type of the conditional variable.
}
if($x!=8)
{
    //this would be executed because != means that the variable is not equal to the conditional variable
}
if($x>8)
{
    //Since 7 is not greater 8, this would not be executed
}
if($x<8)
{
    //Since 8 greater 7, this would be executed
}
if($x>=7)
{
    //Since 7 is equal 7, this would be executed
}
if($x<=6)
{
    //Since 7 is not less than or equal to 6 this would not be . executed
}

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
$x = 7;
if($x>=6 && $x<=8)
{
    //Since 7 is greater than 6 and 7 is less than 8, this statement would be executed
}
if($x==7 || $x>=8)
{
    //This if statement says if $x is equal to 7 or if x is greater than 8, then execute this statement. While x is not greater than 8, $x is equal to 7; therefore, it will execute the statement.
}
if(!($x==6))
{
    //Since $x is equal to 7, not 6, and we are checking to see if its false (with the ! before the condition), this statement would be executed
}

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
$x = 6;
if($x == 8)
{
    $x++;
    echo "if($x = 8) and now x = " . $x; //This would not be triggered because $x is not equal to 8
}
else if ($x == 7)
{
    $x = 4;
    echo "else if ($x = 7) and now x = " . $x; //This would not be triggered because $x is not equal to 7
}
else
{
    $x = 20;
    echo "else and now x = " . $x; //This would be triggered because the other conditionals were not met
}
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
$x = 8;
if($x == 8)
{
    $x++;
    echo "if($x = 8) and now x = " . $x; //This statement would be triggered because $x is equal to 8
}
if ($x == 7)
{
    $x = 4;
    echo "else if ($x = 7) and now x = " . $x; //This statement would not be triggered because $x is not equal to 7
}
else
{
    $x = 20;
    echo " else and now x = " . $x;//This statement would be triggered because the other conditionals were not met
}
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.



Link/cite this page

If you use any of the content on this page in your own work, please use the code below to cite this page as the source of the content.

  • Stewart, Suzy. "If Statements". After Hours Programming. Accessed on March 16, 2024. https://www.afterhoursprogramming.com/tutorial/php/if-statements/.

  • Stewart, Suzy. "If Statements". After Hours Programming, https://www.afterhoursprogramming.com/tutorial/php/if-statements/. Accessed 16 March, 2024.

  • Stewart, Suzy. If Statements. After Hours Programming. Retrieved from https://www.afterhoursprogramming.com/tutorial/php/if-statements/.



4 thoughts on “If Statements”

  1. 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.

  2. 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

  3. 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;

    ?>

Leave a Comment

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