⌂ Home
▲ Previous: Control flow
▼ Next: if-else conditional statement
if conditional statementPHP 4, PHP 5, PHP 7, PHP 8
The if construct is one of the most important features of many languages, PHP included. It allows for conditional execution of code fragments. PHP features an if structure that is similar to that of C:
if (expr)
statement
As described in the section about expressions, expression is evaluated to its boolean value. If expression evaluates to true, PHP will execute statement, and if it evaluates to false - it’ll ignore it.
The following example would display a is bigger than b if $a is bigger than $b:
<?php
if ($a > $b)
echo "a is bigger than b";
?>
Often you’d want to have more than one statement to be executed conditionally. Of course, there’s no need to wrap each statement with an if clause. Instead, you can group several statements into a statement group. For example, this code would display a is bigger than b if $a is bigger than $b, and would then assign the value of $a into $b:
<?php
if ($a > $b) {
echo "a is bigger than b";
$b = $a;
}
?>
If statements can be nested infinitely within other if statements, which provides you with complete flexibility for conditional execution of the various parts of your program.
<?php
if (1 > 2)
print("1 > 2\n");
if (2 > 1)
print("2 > 1\n");
View: Example
Execute:
Result:
2 > 1
The following example shows the shortened form for HTML templates.
<?php
if (1 > 2):
print("1 > 2\n");
endif;
if (2 > 1):
print("2 > 1\n");
endif;
View: Example
Execute:
Result:
2 > 1
The following example shows the all possible formating of the statement (but not all are compliant with the official PHP formatting standards).
<?php
$condition = (1 > 2);
if ($condition)
print("1 > 2\n");
$condition = (2 > 1);
if ($condition)
print("2 > 1\n");
print("\n");
if (1 > 2)
print("1 > 2\n");
if (2 > 1)
print("2 > 1\n");
print("\n");
if (1 > 2) print("1 > 2\n");
if (2 > 1) print("2 > 1\n");
print("\n");
if (1 > 2) {
print("1 > 2\n");
}
if (2 > 1) {
print("2 > 1\n");
}
print("\n");
// Shortened form for HTML templates:
if (1 > 2):
print("1 > 2\n");
endif;
if (2 > 1):
print("2 > 1\n");
endif;
print("\n");
if (1 > 2): print("1 > 2\n"); endif;
if (2 > 1): print("2 > 1\n"); endif;
print("\n");
View: Example
Execute:
Result:
2 > 1
2 > 1
2 > 1
2 > 1
2 > 1
2 > 1
▵ Up
⌂ Home
▲ Previous: Control flow
▼ Next: if-else conditional statement