PHP IF statements

Discussion in 'Web Design & Programming' started by RHochstenbach, Mar 8, 2011.

  1. RHochstenbach

    RHochstenbach Administrator

    Likes Received:
    26
    Trophy Points:
    48
    Let's say you want PHP to perform different actions depending on a situation. For example, we set a variable called 'name' with the value 'John Doe'.
    PHP:
    $name "John Doe";
    Now we want to have PHP perform a different action depending on the value of the 'name' variable. Let's say I want PHP to show the text 'You are John' if the value is 'John Doe'. We do it like this:

    PHP:
    if($name == "John Doe") {

    echo 
    "You are John";

    }
    If you load the page, it should show the text 'You are John'. Now change the 'name' variable into something different. It now show nothing. How did this work?

    The word 'if' is the PHP instruction to check something. The condition to check should be put between ( ) symbols. You can now compare two values. These can be both text, both variables or a variable and text. Between those, you put the condition. We want to check if both are equal, so we use a double equal-to sign ( == ). After that, you put an opening bracket ( { ) behind it. Then write the PHP instructions that should be executed if this condition is true. When you're done, put a closing bracket ( } ) on a new line. Note that you don't have to put a semicolon after an instruction with brackets (but you DO need to use them with the PHP instructions inside the brackets!).

    We can even execute a statement if something is not true using an ELSE statement. If the variable 'name' is set to 'John Doe', write 'You are John'. If it's not, then write 'You are somebody else'.
    PHP:
    if($name == "John Doe") {

    echo 
    "You are John";

    } else {

    echo 
    "You are somebody else";

    }
    It's even possible to check for multiple conditions. If the variable 'name' is set to 'John Doe', write 'You are John'. If it's not, then check if it's set to 'Santa Clause'. If that's true, write "Where are my presents?". If that's also not true, then show 'You are somebody else'. We do that with the ELSEIF statement.

    PHP:
    if($name == "John Doe") {

    echo 
    "You are John";

    } elseif(
    $name == "Santa Clause") {

    echo 
    "Where are my presents?"

    } else {

    echo 
    "You are somebody else";

    }
    Notice that ELSEIF requires an IF statement, but an ELSE statement is optional.

    The last thing I'll teach you here is how to check if a condition is false. Let's say we want to check if the 'name' variable is NOT set to 'John Doe'. We do it like this:

    PHP:
    if(!($name == "John Doe")) {

    echo 
    "I don't know you";

    }
    With the exclamation mark we tell PHP to check if a condition is NOT true.

    Now remove the 'name' variable, the entire line except for the entire IF statement. The variable is now not set. The IF variable will still work, but it shows an error about an unidentified index. Basically you mentioned the 'name' variable in the IF statement, but it doesn't exist. Therefore PHP will complain.

    If you want to check if a variable has been set, use this IF statement:
    PHP:
    if(isset($name)) {

    }
    This will check if the variable 'name' has been set. Now we can use the other IF statement inside this statement:

    PHP:
    if(isset($name)) {
    // If the 'name' variable is set, do this:
    if(!($name == "John Doe")) {

    echo 
    "I don't know you";

    // end of the John Doe IF statement
    // end of the isset IF statement
     

Share This Page