English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn how to write decision-making code using if ... else ... elseif statements in PHP.
Like most programming languages, PHP also allows you to write code that will execute different operations at runtime based on logical or comparison test conditions. This means you can create a test condition in the form of an expression that evaluates to true or false, and based on these results, you can perform certain operations.
PHP has some statements that can be used to make decisions:
if statement
if ... else statement
if...elseif..else statement
switch...case statement
We will discuss each statement in the following section.
Only when the value of the specified condition is trueifThe statement is used to execute a code block. This is the simplest PHP conditional statement, which can be written as follows:
if(condition){ //The code to be executed }
The example will output 'Have a nice weekend!' if today is Friday:
<?php $d = date("D"); if($d == "Fri"){ echo "Have a nice weekend!"; } ?>Test and see‹/›
You can provide an alternative choice by adding an else statement to the if statement to enhance the decision-making process. If the specified condition is True, the if ... else statement allows you to execute one code block, and if the condition is False, you can execute another block of code. It can be written like this:
if(condition){ //The code to be executed when the condition is true } //The code to be executed when the condition is false }
The example will output 'Have a nice weekend!' if the current date is Friday, then it will output 'Have a nice day!'
<?php $d = date("D"); if($d == "Fri"){ echo "Have a nice weekend!"; } else { echo "Have a nice day!"; } ?>Test and see‹/›
if ... elseif ... else special statements are used to combine multiple if ... else statements.
if(condition1{ //if condition1The code to be executed when it is true }2{ //if condition1when it is false and condition2The code to be executed when it is true } //if condition1and condition2The code that will be executed is incorrect }
The example will output 'Have a nice weekend!' if the current date is Friday, and 'Have a nice Sunday!' if the current date is Sunday, otherwise it will output 'Have a nice day!'
<?php $d = date("D"); if($d == "Fri"){ echo "Have a nice weekend!"; } elseif($d == "Sun"){ echo "Have a nice Sunday!"; } else { echo "Have a nice day!"; } ?>Test and see‹/›
InNext chapterIn this chapter, you will learn about PHP switch-information.
The ternary operator provides a convenient way to write if...else statements. The ternary operator is represented by a question mark (?) and has three operands: the condition to check, the result if the condition is true, and the result if the condition is false.
To understand how this operator works, please see the following example:
<?php $age = 15; if($age < 18{ echo 'children'; //If the age is less than18years, then display 'children' } else { echo 'adults'; //If the age is greater than or equal to18years, then display 'adults' } ?>Test and see‹/›
Using the ternary operator, the same code can be written in a more compact way:
<?php $age = 15; echo ($age < 18) ? 'children' : 'adults'; ?>Test and see‹/›
In the example above, the ternary operator returns 'children' if the result of the condition is true (i.e., $age is less than18),then choose the value on the left of the colon (i.e., 'children'), and if the result of the condition is false, choose the value on the right of the colon (i.e., 'adults').
Tip:The code written using the ternary operator may be difficult to read. However, it provides a way to write a compact if-a good way to use the else statement.
PHP 7An new null coalescing operator (??) has been introduced, which you can use as a shorthand form, where you need to combine the ternary operator and the isset() function.
To better understand this, please see the following code line. It gets the value of $_GET['name'], and if it does not exist or is NULL, it returns 'anonymous'.
<?php //Using the ternary operator(?:) $name = isset($_GET['name']) ? $_GET['name'] : 'anonymous'; echo $name; ?>Test and see‹/›
Using the null coalescing operator, the same code can be written as:
<?php //Using the null coalescing operator (??) $name = $_GET['name'] ?? 'anonymous'; echo $name; ?>Test and see‹/›
As you can see, the syntax is more compact and easier to write.