PHP If Statement

If Statements are used to make decisions, and as such they are used frequently.

Syntax

if (condition) {
 execution code
}

The If Statement condition evaluates something which is true; for example, the following code evaluates if the variable $name is equal to “Asim”:

$name = 'Asim';

if ($name == 'Asim') {
 echo 'Welcome!';
}

If you try this you should get the result Welcome displaying; however if you change the variable name ($name) to something like $name = ‘asim’;  it will not work, since $name == ‘Asim’ is not true (PHP is case sensitive).

If Else

In this case you can use the If Else Statement, so if $name == does not equal Asim it is false, so execute the Else Statement.

Syntax

If (condition) {
Execute code if statement is true
} else {
Execute if statement is false!
}

Example

$name = 'Asim';

if ($name == 'Asim') {
echo 'Welcome!';
}  else {
 echo 'Unknown user!';
}

If the variable name equals Asim then it will execute “Welcome”, otherwise it will execute “Unknown User” since the condition is false.

Using Operators

The If Else Statement allows a variety of operators which we can use for various functions; for example, we may want to compare two strings or see if an integer is greater than or less than a certain number.

Example – Comparing two strings && (And) 0perator

<?php

$user = "Asim";
$password = "12345";

if ($user == "Asim" && $password == "12345"){
    echo 'Welcome Asim!';
}else{
    echo 'Error: Invalid user or password';
}

?>

The condition here simply translates to “If user is equal to Asim and password is equal to 12345″. If the condition is true then the first part of the code will execute. If not, the second will execute since the condition is false.

Example – Comparing two strings || (OR) operator

<?php

$user = "Aim";
$password = "12345";

if ($user == "Asim" || $password == "12345"){
    echo 'Welcome Asim!';
}else{
    echo 'Error: Invalid user or password';
}

?>

This is similar to the && operator, however this time we are saying “If user is equal to “Asim” OR password is equal to 12345″. The condition will result in true because the user equals “Aim” but the password equals 12345, so the password is still the same. With the OR operator, only one variable needs to be true.

Example – Less than and greater than

The less than < and greater than > operators are applied to numbers.

<?php

$myNumber = 16;

if ($myNumber > 17){
    echo 'My number is greater than 17, my number = '.$myNumber;
}else{
    echo 'Error: My number is not greater than 17 my number = '.$myNumber;
}

?>
In this example we have a variable, $myNumber, which has the value assigned 16, and then we check to see if it is greater than 17. If it is, the first part of the code will execute; otherwise the Else Statement will. You can also use the OR and && operators to see if values match. The above condition translates to “if myNumber is greater than 17″.

Case-Sensitive Issues

PHP is a case sensitive language and at times you might not get the expected results. For example:

$favfood = "Pizza";

if ($favfood == "pizza")
{
 echo "Pizza it is!";
}else {
echo "Pasta?";
}

Now this example simply checks if favfood is equal to “pizza”. Here, the issue is that the cases are different so it will result in false. We believe that it is the right result because we are not interested in the case, but to the computer it is the wrong answer.

We can overcome an issue like this by simply forcing the case of $favfood to lower using the strtolower function.

$favfood = "Pizza";

if (strtolower($favfood) == "pizza")
{
 echo "Pizza it is!";
}else {
echo "Pasta?";
}

This will output the result we expect. Remember that if you are collecting user data (in multiple cases, upper/lower) and then comparing it, make sure you change the case or you won’t get the results you expect. It is probably best to format it to lower case, as most people do not normally type in upper case.

Operators

Operator Description
< Less than
> Greater than
== Is equal to
|| Or
&& And

Summary

This tutorial covered how to use the If Statement as well as how to compare results, and addressed case sensitive issues. Remember that If Statements evaluate a condition which is true.