© Mikael Olsson 2021
M. OlssonPHP 8 Quick Scripting Referencehttps://doi.org/10.1007/978-1-4842-6619-9_6

6. Conditionals

Mikael Olsson1  
(1)
Hammarland, Finland
 

Conditional statements are used to execute different code blocks based on different conditions.

If Statement

The if statement only executes if the condsition inside the parentheses is evaluated to true. The condition can include any of the comparison and logical operators.
$x = 1;
// ...
if ($x == 1) {
  echo 'x is 1';
}
To test for other conditions, the if statement can be extended with any number of elseif clauses. Each additional condition is only tested if all previous conditions are false.
elseif ($x == 2) {
  echo 'x is 2';
}
For handling all other cases, there can be one else clause at the end, ...

Get PHP 8 Quick Scripting Reference: A Pocket Guide to PHP Web Scripting now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.