post

Learn the PHP If Statement

The if statement is one of the most important features of any programming language because it allows you to evaluate expressions and selectively execute blocks of code if the expression evaluates to true. The principal behind the if statement is simply if (expression) is true, then do this or else do something different or nothing at all. There are three different ways to write an if statement depending on what you need to do. A simple one line if statement should be used if you only need to execute one line of code if a certain condition is met. You can use an if/else statement when you need to execute a different code block should the conditions you are evaluating for be false. Or you can use an if/elseif/else statement if you need to evaluate multiple conditions in order to choose the appropriate code block to execute. In the event that you need to execute multiple lines of code within your if statement, you will have to enclose each code block using curly braces { } around your lines of code.
 
 

PHP If Statement Examples

 

Example PHP If Statement

<?php
$my_condition=4; 

if ($my_condition>3) echo "$my_condition is greater than 3.";
?>

 

Result:

4 is greater than 3.

 

Example PHP If, Else Statement

<?php
//rand(1, 6) assigns the variable with a random value from 1 to 6
$my_condition=rand(1, 6);
                
if ($my_condition>3)
    echo "$my_condition is greater than 3.";
else
    echo "$my_condition is not greater than 3.";
?>

 

Result:

2 is not greater than 3.

 

Example If, Elseif, else Statement

<?php
$my_condition=rand(1, 6);

if ($my_condition==1)
    echo "\$my_condition is 1.";
elseif ($my_condition==2)
    echo "\$my_condition is 2.";
else
    echo "\$my_condition is neither 1 or 2, but it is $my_condition.";
?>

 

Result:

$my_condition is 2.

 

Example of Multiple Lines of Code in an If Statement

<?php
$my_condition=rand(1, 6);

if ($my_condition==1){
    echo "\$my_condition is 1. ";
    echo "One is the loneliest number. <br />";
}elseif ($my_condition==2){
    echo "\$my_condition is 2. ";
    echo "Two can be as bad as one. It's the loneliest number since the number one. <br />";
}else{
    echo "\$my_condition is neither 1 or 2, but it is $my_condition. ";
    echo "$my_condition has to be better than 1 or 2, right? <br />";
}
?>

 

Result:

$my_condition is 2. Two can be as bad as one. It's the loneliest number since the number one.

 
 

Nested if, Else Statements

 
If statements can be nested within if statements indefinitely, which can be very useful when trying to make decisions, but can also be tricky to keep track of. I recommend indenting each nested if statement and its code block in order to easily track the phases of the evaluation process. This is another reason I suggest using an IDE, such as Netbeans, as opposed to something as simple as notepad. Netbeans’ formatting features allow you to easily keep your code aligned and will automatically highlight its opposing counterpart when you select a single brace character–which is very useful when troubleshooting large nested if statements.
 

Example of Nested PHP if Statements

<?php
$my_condition=rand(1, 2);
$my_other_condition=rand(1,2);

//Step one: evaluate $my_condition
if ($my_condition==1){
    //Step two: evaluate $my_other_condition, if $my_condition==1
    if ($my_other_condition==1)
        echo "both \$my_condition and \$my_other_condition are 1. <br />";
    elseif ($my_other_condition==2)
        echo "\$my_condition is 1, but \$my_other_condition is 2. <br />";
    
}elseif ($my_condition==2){
    //Step two: evaluate $my_other_condition, if $my_condition==2
    if ($my_other_condition==1)
        echo "\$my_condition is 2, but \$my_other_condition is 1. <br />";
    elseif ($my_other_condition==2)
        echo "both \$my_condition and \$my_other_condition are 2. <br />";       
}
?>

 

Result:

$my_condition is 1, but $my_other_condition is 2.

 

Speak Your Mind

*

*