Home >Backend Development >PHP Problem >Detailed explanation of if usage in php

Detailed explanation of if usage in php

hzc
hzcOriginal
2020-06-16 11:00:387910browse

Detailed explanation of if usage in php

Detailed explanation of if usage in php

When you write code, you often want to perform different actions for different decisions . You can do this using conditional statements in your code.

In PHP, we can use the following conditional statements:

1.if statement - If the specified condition is true, the code is executed

2.if...else Statement - If the condition is true, execute the code; if the condition is false, execute the other end of the code

3.if...elseif....else Statement - Select one of several code blocks to execute

4.switch statement - statement one of multiple code blocks to execute

if (条件) {
  当条件为 true 时执行的代码;
}
if (条件) {
  条件为 true 时执行的代码;
} else {
  条件为 false 时执行的代码;
}
if (条件) {
  条件为 true 时执行的代码;
} elseif (condition) {
  条件为 true 时执行的代码;
} else {
  条件为 false 时执行的代码;
}
<?php
switch ($x)
{
case 1:
  echo "Number 1";
  break;
case 2:
  echo "Number 2";
  break;
case 3:
  echo "Number 3";
  break;
default:
  echo "No number between 1 and 3";
}
?>

Recommended tutorial: "php tutorial"

The above is the detailed content of Detailed explanation of if usage 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