search
HomeBackend DevelopmentPHP Tutorialphp if else swicth conditional control statement study notes_PHP tutorial

The most commonly used conditional control statements in PHP are if else or switch statements. Now I will give you a detailed introduction to my notes on using if else conditional control statements in PHP. Friends in need can refer to them.

In the PHP language, the basic process control structures are: sequential structure, branch structure, and loop structure.

Commonly used control statements mainly include:

if, if...else judgment statements
switch branch statement
while, do...while loop statement
for loop statement
break, continue interrupt statements

The php if statement is a simple judgment statement, which implements the function of conditional judgment. When the program executes a statement and encounters a fork in the road, it selects and executes the corresponding statement by judging whether the value of the statement meets the conditions. So the if statement is the most basic PHP conditional expression.


The basic structure is:

if (judgment statement)

{

Execution statement body

}

Example:

The code is as follows Copy code
 代码如下 复制代码

 $a=8;
 $b=4;
 if($a>$b){  /* 若$a>$b,则执行大括号内的执行语句 */
  $a++;
 }
 $c=$a+$b;
 echo "a+b=".$c;
?>

$a=8; $b=4;

if($a>$b){ /* If $a>$b, execute the execution statement within the curly brackets */

$a++;

}

$c=$a+$b;

echo "a+b=".$c;

?>

The if statement can choose whether to execute the statement body, while the if...else statement is a choice of two. You must choose one of the two statement bodies to execute. It can be interpreted as "what to do if something happens, otherwise how to solve it", so if...else is essentially a selective statement.

If the value is non-0 (i.e. true), then statement body 1 is executed, and if the value is 0 (i.e. false), then statement body 2 is executed.

The basic structure of the if...else statement is:

if (judgment statement)

{

Execute statement body 1

}

else
 代码如下 复制代码

 $a=11;
 $b=9;
 if($a>$b){
  $c=$a+$b;
 }
 else{
  $c=$a-$b;
 }
 echo $c;
?>

{

Execute statement body 2

}

Example:

The code is as follows Copy code

$a=11;

$b=9;

if($a>$b){

$c=$a+$b;

}

else{

$c=$a-$b;

}

echo $c;

?>

 代码如下 复制代码

 $score=61;
 if($score >=90){
  echo "成绩优秀";
 }
 else if($score >=60){
  echo "及格.";
 }
 else if($score   不及格;
 }
 else echo "成绩有误";
?>

The if...else statement can only implement two-way branches. To achieve multiple branches, just use multiple if...else statements to nest. Its structural form is as follows: if(judgment statement 1){ Execute statement body 1 } else if(judgment statement 2){ Execute statement body 2 } else if… else… Example:
The code is as follows Copy code
$score=61; if($score >=90){ echo "Excellent results"; } else if($score >=60){ echo "passed."; } else if($score Failed; } else echo "Wrong results"; ?>

Multiple if() statements can be nested in the if statement to realize the judgment of multiple parameters. It is generally called multiple nesting of if statements. Its basic structural form is as follows:

if(judgment 1)

if(judgment 2) statement body 1

else statement body 2

else

You should pay attention to the pairing relationship between if and else here. Starting from the inner layer, else is always paired with the nearest if above it. Be particularly careful when programming.

Example:

The code is as follows
 代码如下 复制代码

 $gender="女性";
 $age=28;
 if($gender=="男性"){
  if($age >=18){
   echo "你是个男人";
  }
  else if($age    echo "你是个男孩";
  }
 }
 else{
  if($age >=18){
   echo "你是个女人";
  }
  else if($age    echo "你是个女孩";
  }
 }
?>

Copy code


$gender="female"; $age=28;

if($gender=="male"){
if($age >=18){
echo "You are a man";
}
else if($age echo "You are a boy";
}
}
else{
if($age >=18){
echo "You are a woman";
}
else if($age echo "You are a girl";

}

}
 代码如下 复制代码

 $i=0;
 switch($i){
  case(0):
   echo "you are a girl
";
   break;
  case(1):
   echo "you are a boy
";
   break;
  default:
   echo "it's an animal"; 
 }
?>

?>

As introduced earlier, the if...else nested structure can realize the function of multi-branch selection, but the code of this method is relatively lengthy. To realize the function of multi-branch selection, PHP also provides switch statements. Using the switch statement makes the script more concise. Basic structure: switch(expression){ case 1: Execution statement body 1; break; case 2: Execution statement body 2; break; ... default:
Execution statement body n; break; }
The value of the expression is first matched with the case statement one by one. If there is a match, the statement body is executed, and then the loop is jumped out. If no case statement is matched in the end, the execution statement after default is executed.
Example:
The code is as follows Copy code
$i=0; switch($i){ case(0): echo "you are a girl
"; Break; case(1): echo "you are a boy
"; Break; default: echo "it's an animal"; } ?> http://www.bkjia.com/PHPjc/628841.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628841.htmlTechArticleThe most commonly used conditional control statements in PHP are if else or switch statements. Let me introduce them in detail below. My notes on using if else conditional control statements in php. Friends in need...
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
How do you create and use an interface in PHP?How do you create and use an interface in PHP?Apr 30, 2025 pm 03:40 PM

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

What is the difference between crypt() and password_hash()?What is the difference between crypt() and password_hash()?Apr 30, 2025 pm 03:39 PM

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

How can you prevent Cross-Site Scripting (XSS) in PHP?How can you prevent Cross-Site Scripting (XSS) in PHP?Apr 30, 2025 pm 03:38 PM

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

What is autoloading in PHP?What is autoloading in PHP?Apr 30, 2025 pm 03:37 PM

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

What are PHP streams?What are PHP streams?Apr 30, 2025 pm 03:36 PM

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

What is the maximum size of a file that can be uploaded using PHP ?What is the maximum size of a file that can be uploaded using PHP ?Apr 30, 2025 pm 03:35 PM

The article discusses managing file upload sizes in PHP, focusing on the default limit of 2MB and how to increase it by modifying php.ini settings.

What is Nullable types in PHP ?What is Nullable types in PHP ?Apr 30, 2025 pm 03:34 PM

The article discusses nullable types in PHP, introduced in PHP 7.1, allowing variables or parameters to be either a specified type or null. It highlights benefits like improved readability, type safety, and explicit intent, and explains how to declar

What is the difference between the unset() and unlink() functions ?What is the difference between the unset() and unlink() functions ?Apr 30, 2025 pm 03:33 PM

The article discusses the differences between unset() and unlink() functions in programming, focusing on their purposes and use cases. Unset() removes variables from memory, while unlink() deletes files from the filesystem. Both are crucial for effec

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor