Home  >  Article  >  Backend Development  >  How to use conditional statements in php?

How to use conditional statements in php?

WBOY
WBOYOriginal
2023-05-31 16:01:361449browse

PHP is a popular programming language commonly used to develop web applications. In PHP, conditional statements are an important part. Conditional statements allow a program to execute different blocks of code based on different conditions. In this article, we will introduce conditional statements in PHP and give some examples and usage tips.

  1. if statement
    The if statement is one of the most commonly used conditional statements in PHP. Its basic usage is as follows:
if (条件) {
    // 如果条件成立执行这里的代码
}

The condition can be any expression, for example:

if ($x > 10) {
    echo "x 大于 10";
}

The above code will output "x is greater than 10" when $x is greater than 10 ".

The if statement also supports the else statement, which can execute additional code when the condition is not true. For example:

if ($x > 10) {
    echo "x 大于 10";
} else {
    echo "x 小于等于 10";
}

The above code will output "x is less than or equal to 10" when $x is less than or equal to 10.

In addition to if and else, there is also an elseif statement, which can be used to choose between multiple conditions. For example:

if ($x > 10) {
    echo "x 大于 10";
} elseif ($x < 10) {
    echo "x 小于 10";
} else {
    echo "x 等于 10";
}
  1. switch statement
    The switch statement is also a commonly used conditional statement, suitable for selecting between multiple options. The basic usage is as follows:
switch (表达式) {
    case 值1:
        // 如果表达式等于值1,则执行这里的代码
        break;
    case 值2:
        // 如果表达式等于值2,则执行这里的代码
        break;
    default:
        // 如果表达式不等于任何一个值,则执行这里的代码
}

The following is an example:

$day = "星期三";
switch ($day) {
    case "星期一":
        echo "今天是星期一";
        break;
    case "星期二":
        echo "今天是星期二";
        break;
    case "星期三":
        echo "今天是星期三";
        break;
    default:
        echo "今天不是工作日";
}

The above code will output "Today is Wednesday".

  1. Ternary operator
    The ternary operator is also a commonly used conditional statement, which can complete simple conditional judgments in one line of code. The basic usage is as follows:
$variable = (条件) ? 表达式1 : 表达式2;

If the condition is true, assign expression 1 to the variable, otherwise assign expression 2 to the variable. This can be used to set the value of a variable based on conditions. For example:

$age = 25;
$message = ($age >= 18) ? "成年人" : "未成年人";
echo $message;

The above code will output "Adult".

  1. NULL coalescing operator
    PHP7 introduces a new conditional statement: the NULL coalescing operator. It can be used to simplify the code that determines whether a variable is null. The basic usage is as follows:
$variable = $value ?? $default;

If $value is not null, assign it to $variable, otherwise assign $default to $variable. For example:

$username = $_GET["username"] ?? "guest";
echo $username;

The above code will output the user name obtained from the GET request, or "guest" if there is no user name.

Summary
This article introduces conditional statements in PHP, including if, switch, ternary operator and NULL coalescing operator. These conditional statements allow the program to execute different code blocks based on different conditions, thereby achieving more flexible logical judgments. When using conditional statements, you need to pay attention to the format of the conditional expression and code block to avoid syntax errors.

The above is the detailed content of How to use conditional statements in php?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn