Conditional Statements

PHP’s IF statement enables you to perform different actions based on the results of evaluating some condition. For an example think of a login feature of some website. When you enter the username and a password, the login page checks to see if the username/password combination is correct or not. If the combination is correct you will be logged into the site and redirect to the page you want to access. If the combination is incorrect you will get a message saying its not correct. Technically, behind the scenes, the ‘IF statement’ is what makes all those decision making scenarios possible.

Lets have a look at the IF Statement syntax:

if(expression) {
    statement(s);
}

There is‘if’ keyword followed by the expression to evaluate inside parentheses. Then we have one or more statements wrapped inside the curly braces. Those statements will get executed only if the expression evaluates to TRUE.

Lets look at an example:

<?php

# create a variable and set its value to TRUE
$status = TRUE;

# If '$status' is TRUE, display a message 
if($status == TRUE)
{
   echo 'The Status is TRUE';
}

In the above example, we have a variable called $status with its value set to TRUE. Then we have an IF statement. It checks to see if the $status variable is true ($status == TRUE). If it does, we will see the message ‘The Status is TRUE’. That’s exactly what you’ll see when you run this code in the browser.

We can re-write the above IF Statement as shown below as well. Here you’ll notice that we have used the variable itself as the expression to evaluate instead of using any comparison operators as in the previous example:

...

if($status)
{
   echo ...
}

This still gives the same result.

Now, modify the above example as shown below:

<?php

# Re-assign the variable value to FALSE
$status = FALSE;

# If '$status' is TRUE, display a message 
if($status == TRUE)
{
   echo 'The Status is TRUE';
}
echo '<br>';
echo '<br>';
echo 'The End!';

When you run this modified script you won’t see ‘The Status is TRUE’ message in the browser. Why? The reason is pretty simple.

Explanation:

IF Statement will evaluate the expression ($status == TRUE) to see if the $status variable is TRUE. But since we have set it to FALSE in line 4, the result of that evaluation ( is $status is true? ) will be FALSE. In other words NO. A script executes the body of an if statement only when it’s expression evaluates to TRUE. In this case because of the expression has evaluated to FALSE, the script will not execute any statement in the if statement body. Instead the execution has skipped over to the statement that shows ‘The End!’. So whenever an expression becomes false in an if statement, the script execution will skip the body of that if statement and continue with the statements below that.

What if we want to show a message when the variable is set to FALSE?

For that you’ll have to modify the If statement’s expression to check if the variable is FALSE instead of checking whether it is TRUE.

Check below code to see this in action. It is the same code that we used in the previous example. Only the If Statement has changed:

<?php

# Re-assign the variable value to FALSE
$status = FALSE;

# If '$status' is FALSE, display a message 
if($status == FALSE)
{
   echo 'The Status is FALSE';
}

...
...

When you run this code, you will see ‘The Status is FALSE’ getting printed in the browser. This time If statement checks to see if the $status is FALSE. As you can see in the line 4, it is in fact set to FALSE. So the expression evaluation ( if($status == FALSE) ) will result in TRUE. That means ‘Yes! $status is false’. So the script executes the echo statement that is inside the curly braces.

Comments

So empty here ... leave a comment!

Leave a Reply

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

Sidebar



Do NOT follow this link or you will be banned from the site!