- PHP If Statement
- Syntax
- If Else
- Syntax
- Example
- Using Operators
- Example – Comparing two strings && (And) 0perator
- Example – Comparing two strings || (OR) operator
- Example – Less than and greater than
- Case-Sensitive Issues
- Operators
- Summary
- PHP Tutorial
- PHP if. else. elseif Statements
- PHP Conditional Statements
- PHP — The if Statement
- Syntax
- Example
- PHP — The if. else Statement
- Syntax
- Example
- PHP — The if. elseif. else Statement
- Syntax
- Example
- PHP — The switch Statement
- PHP if
- Introduction to the PHP if statement
- Curly braces
- Nesting if statements
- Embed if statement in HTML
- A common mistake with the PHP if statement
- Summary
PHP If Statement
If Statements are used to make decisions, and as such they are used frequently.
Syntax
The If Statement condition evaluates something which is true; for example, the following code evaluates if the variable $name is equal to “Asim”:
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
Example
$name = ‘Asim’; if ($name == ‘Asim’) < echo 'Welcome!'; >else
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
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
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.
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
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
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
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.
PHP Tutorial
- Basics
- PHP Tutorial
- PHP Installation
- General
- PHP Syntax
- PHP Strings
- PHP Variables
- PHP Operators
- Decision Statements
- PHP If Statement
- PHP For Loops
- PHP Switch Statement
- Loops
- PHP While Statement
- PHP Do While
- PHP Date and Time
- Forms
- PHP $_POST Predefined Variable
- PHP $_GET Predefined Variable
- Advanced
- PHP Arrays
- PHP Functions
Affiliate Links — Advertising Disclosure
If you purchase a product or service linked from this site, we may receive an «affiliate commission». We are disclosing this in accordance with the Federal Trade Commission’s 16 CFR, Part 255: «Guides Concerning the Use of Endorsements and Testimonials in Advertising» and also in accordance to amazon associates programme operating agreement.
Amazon
The owner of this website is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to amazon.com.
Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.
PHP if. else. elseif Statements
Conditional statements are used to perform different actions based on different conditions.
PHP Conditional Statements
Very often when you write code, you want to perform different actions for different conditions. You can use conditional statements in your code to do this.
In PHP we have the following conditional statements:
- if statement — executes some code if one condition is true
- if. else statement — executes some code if a condition is true and another code if that condition is false
- if. elseif. else statement — executes different codes for more than two conditions
- switch statement — selects one of many blocks of code to be executed
PHP — The if Statement
The if statement executes some code if one condition is true.
Syntax
Example
Output «Have a good day!» if the current time (HOUR) is less than 20:
PHP — The if. else Statement
The if. else statement executes some code if a condition is true and another code if that condition is false.
Syntax
if (condition) code to be executed if condition is true;
> else code to be executed if condition is false;
>
Example
Output «Have a good day!» if the current time is less than 20, and «Have a good night!» otherwise:
if ($t < "20") echo "Have a good day!";
> else echo «Have a good night!»;
>
?>
PHP — The if. elseif. else Statement
The if. elseif. else statement executes different codes for more than two conditions.
Syntax
if (condition) code to be executed if this condition is true;
> elseif (condition) code to be executed if first condition is false and this condition is true;
> else code to be executed if all conditions are false;
>
Example
Output «Have a good morning!» if the current time is less than 10, and «Have a good day!» if the current time is less than 20. Otherwise it will output «Have a good night!»:
if ($t < "10") echo "Have a good morning!";
> elseif ($t < "20") echo "Have a good day!";
> else echo «Have a good night!»;
>
?>
PHP — The switch Statement
The switch statement will be explained in the next chapter.
PHP if
Summary: in this tutorial, you’ll learn about the PHP if statement and how to use it to execute a code block conditionally.
Introduction to the PHP if statement
The if statement allows you to execute a statement if an expression evaluates to true . The following shows the syntax of the if statement:
if ( expression ) statement;
Code language: HTML, XML (xml)
In this syntax, PHP evaluates the expression first. If the expression evaluates to true , PHP executes the statement . In case the expression evaluates to false , PHP ignores the statement .
The following flowchart illustrates how the if statement works:
The following example uses the if statement to display a message if the $is_admin variable sets to true :
$is_admin = true; if ($is_admin) echo 'Welcome, admin!';
Code language: HTML, XML (xml)
Since $is_admin is true , the script outputs the following message:
Curly braces
If you want to execute multiple statements in the if block, you can use curly braces to group multiple statements like this:
if ( expression ) < statement1; statement2; // more statement >
Code language: HTML, XML (xml)
The following example uses the if statement that executes multiple statements:
$can_edit = false; $is_admin = true; if ( $is_admin ) < echo 'Welcome, admin!'; $can_edit = true; >
Code language: HTML, XML (xml)
In this example, the if statement displays a message and sets the $can_edit variable to true if the $is_admin variable is true .
It’s a good practice to always use curly braces with the if statement even though it has a single statement to execute like this:
if ( expression )
Code language: HTML, XML (xml)
In addition, you can use spaces between the expression and curly braces to make the code more readable.
Nesting if statements
It’s possible to nest an if statement inside another if statement as follows:
if ( expression1 ) < // do something if( expression2 ) < // do other things > >
Code language: HTML, XML (xml)
The following example shows how to nest an if statement in another if statement:
$is_admin = true; $can_approve = true; if ($is_admin) < echo 'Welcome, admin!'; if ($can_approve) < echo 'Please approve the pending items'; > >
Code language: HTML, XML (xml)
Embed if statement in HTML
To embed an if statement in an HTML document, you can use the above syntax. However, PHP provides a better syntax that allows you to mix the if statement with HTML nicely:
if ( expession) : ?> endif; ?>
Code language: HTML, XML (xml)
The following example uses the if statement that shows the edit link if the $is_admin is true :
html>
html lang="en"> head> meta charset="UTF-8"> title>PHP if Statement Demo title> head> body> $is_admin = true; ?> if ( $is_admin ) : ?> a href="#">Edit a> endif; ?> a href="#">View a> body> html>Code language: HTML, XML (xml)
Since the $is_admin is true , the script shows the Edit link. If you change the value of the $is_admin to false , you won’t see the Edit link in the output.
A common mistake with the PHP if statement
A common mistake that you may have is to use the wrong operator in the if statement. For example:
$checked = 'on'; if( $checked = 'off' ) < echo 'The checkbox has not been checked'; >
Code language: HTML, XML (xml)
This script shows a message if the $checke d is ‘off’ . However, the expression in the if statement is an assignment, not a comparison:
$checked = 'off'
Code language: PHP (php)
This expression assigns the literal string ‘off’ to the $checked variable and returns that variable. It doesn’t compare the value of the $checked variable with the ‘off’ value. Therefore, the expression always evaluates to true , which is not correct.
To avoid this error, you can place the value first before the comparison operator and the variable after the comparison operator like this:
$checked = 'on'; if('off' == $checked ) < echo 'The checkbox has not been checked'; >
Code language: HTML, XML (xml)
If you accidentally use the assignment operator (=), PHP will raise a syntax error instead:
$checked = 'on'; if ('off' = $checked) < echo 'The checkbox has not been checked'; >
Code language: HTML, XML (xml)
Parse error: syntax error, unexpected '=' .
Code language: JavaScript (javascript)
Summary
- The if statement executes a statement if a condition evaluates to true .
- Always use curly braces even if you have a single statement to execute in the if statement. It makes the code more obvious.
- Do use the pattern if ( value == $variable_name ) <> to avoid possible mistakes.