Curriculum
In this tutorial, you’ll learn about the PHP if statement and how to use it to conditionally execute a code block.
If an expression evaluates to true, the if statement executes a statement. The syntax of the if statement is shown below:
<?php if ( expression ) statement;
PHP evaluates the expression first in this syntax. PHP executes the statement if the expression evaluates to true. If the expression evaluates to false, PHP disregards the statement.
The if statement is used in the following example to display a message if the $is admin variable is set to true:
<?php $is_admin = true; if ($is_admin) echo 'Welcome, admin!';
Because $is admin is set to true, the script displays the following message:
Output
Welcome, admin!
If you want to execute multiple statements in the if block, use curly braces to group them together, as shown below:
<?php if ( expression ) { statement1; statement2; // more statement }
The if statement is used in the following example to execute multiple statements:
<?php $can_edit = false; $is_admin = true; if ( $is_admin ) { echo 'Welcome, admin!'; $can_edit = true; }
If the $is admin variable is true, the if statement displays a message and sets the $can edit variable to true.
Even though the if statement only has one statement to execute, it’s a good practise to always use curly braces with it:
<?php if ( expression ) { statement; }
You can also use spaces between the expression and the curly braces to make the code more readable.
It is possible to nest one if statement inside another, as shown below:
<?php if ( expression1 ) { // do something if( expression2 ) { // do other things } }
The example below demonstrates how to nest an if statement within another if statement:
<?php $is_admin = true; $can_approve = true; if ($is_admin) { echo 'Welcome, admin!'; if ($can_approve) { echo 'Please approve the pending items'; } }
if
statement in HTMLThe above syntax can be used to embed an if statement in an HTML document. PHP, on the other hand, has a better syntax that allows you to mix the if statement with HTML nicely:
<?php if ( expession) : ?> <!-- HTML code here --> <?php endif; ?>
The following example employs an if statement to display the edit link if $is admin is true:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>PHP if Statement Demo</title> </head> <body> <?php $is_admin = true; ?> <?php if ( $is_admin ) : ?> <a href="#">Edit</a> <?php endif; ?> <a href="#">View</a> </body> </html>
Because $is admin is set to true, the script displays the Edit link. If you set the $is admin variable to false, you will not see the Edit link in the output.
A common error is to use the incorrect operator in the if statement. As an example:
<?php $checked = 'on'; if( $checked = 'off' ) { echo 'The checkbox has not been checked'; }
If the value of $checked is ‘off,’ this script displays a message. The expression in the if statement, on the other hand, is an assignment, not a comparison:
$checked = 'off'
This expression assigns the literal string ‘off’ to the variable $checked and returns it. It does not compare the $checked variable’s value to the ‘off’ value. As a result, the expression is always true, which is incorrect.
To avoid this error, place the value before the comparison operator and the variable after the comparison operator, as shown below:
<?php $checked = 'on'; if('off' == $checked ) { echo 'The checkbox has not been checked'; }
If you use the assignment operator (=) by accident, PHP will generate the following syntax error:
<?php $checked = 'on'; if ('off' = $checked) { echo 'The checkbox has not been checked'; }
Error
Parse error: syntax error, unexpected ‘=’ …