PHP条件语句解析和用法指南

2021年3月31日13:32:31 发表评论 940 次浏览

PHP使我们能够根据可能是逻辑上或比较上的某种条件来执行操作。基于这些条件的结果, 即TRUE或FALSE, 将按照用户的要求执行操作。就像一条双向路径。如果你想要一些东西, 那就走这条路, 否则就走那条路。要使用此功能, PHP向我们提供了四个条件语句

现在让我们详细研究以下每个:

if语句:此语句使我们可以设置条件。如果为TRUE, 则将执行if子句中包含的以下代码块。

语法:

if (condition){
    // if TRUE then execute this code
}

例子:

<?php
$x = 12;
  
if ( $x > 0) {
     echo "The number is positive" ;
}
?>

输出如下:

The number is positive

流程图:

PHP条件语句解析和用法指南1

if...else语句:我们了解到, 如果条件将保持为TRUE, 则将执行if中的代码块。但是, 如果条件不为TRUE并且我们要执行操作怎么办?这是其他地方起作用的地方。如果条件为TRUE, 则执行块, 否则执行块。

语法:

if (condition) {
    // if TRUE then execute this code
}
else{
    // if FALSE then execute this code
}

例子:

<?php
$x = -12;
  
if ( $x > 0) {
     echo "The number is positive" ;
}
  
else {
     echo "The number is negative" ;
}
?>

输出如下:

The number is negative

流程图:

PHP条件语句解析和用法指南2

if…elseif…else语句:这使我们可以使用多个if ... else语句。当存在TRUE情况的多个条件时, 我们将使用它。

语法:

if (condition) {
    // if TRUE then execute this code
}
elseif {
    // if TRUE then execute this code
}
elseif {
    // if TRUE then execute this code
}
else {
    // if FALSE then execute this code
}

例子:

<?php
$x = "August" ;
  
if ( $x == "January" ) {
     echo "Happy Republic Day" ;
}
  
elseif ( $x == "August" ) {
     echo "Happy Independence Day!!!" ;
}
  
else {
     echo "Nothing to show" ;
}
?>

输出如下:

Happy Independence Day!!!

流程图:

PHP条件语句解析和用法指南3

switch语句:“switch”在不同的情况下执行,例如,它有不同的情况下,它匹配的条件,并适当地执行一个特定的case块。它首先对表达式求值,然后与每种情况的值进行比较。如果匹配,则执行相同的case。要使用switch,我们需要熟悉两个不同的关键字,break和default。

  1. break语句用于停止进入下一个案例的自动控制流并从切换案例退出。
  2. default语句包含在所有情况都不匹配的情况下将执行的代码。

语法:

switch(n) {
    case statement1:
        code to be executed if n==statement1;
        break;
    case statement2:
        code to be executed if n==statement2;
        break;
    case statement3:
        code to be executed if n==statement3;
        break;
    case statement4:
        code to be executed if n==statement4;
        break;
    ......
    default:
        code to be executed if n != any case;

例子:

<?php
$n = "February" ;
  
switch ( $n ) {
     case "January" :
         echo "Its January" ;
         break ;
     case "February" :
         echo "Its February" ;
         break ;
     case "March" :
         echo "Its March" ;
         break ;
     case "April" :
         echo "Its April" ;
         break ;
     case "May" :
         echo "Its May" ;
         break ;
     case "June" :
         echo "Its June" ;
         break ;
     case "July" :
         echo "Its July" ;
         break ;
     case "August" :
         echo "Its August" ;
         break ;
     case "September" :
         echo "Its September" ;
         break ;
     case "October" :
         echo "Its October" ;
         break ;
     case "November" :
         echo "Its November" ;
         break ;
     case "December" :
         echo "Its December" ;
         break ;
     default :
         echo "Doesn't exist" ;
}
?>

输出如下:

Its February

流程图:

PHP条件语句解析和用法指南4

三元运算符

除了所有这些条件语句外, PHP还提供了一种简便的编写方式, if...else, 称为三元运算符。该语句使用问号(?)和冒号(:)并采用三个操作数:要检查的条件, 结果为TRUE和结果为FALSE。

语法:

(condition) ? if TRUE execute this : otherwise execute this;

例子:

<?php
$x = -12;
  
if ( $x > 0) {
     echo "The number is positive \n" ;
}
else {
     echo "The number is negative \n" ;
}
  
// This whole lot can be written in a 
// single line using ternary operator
echo ( $x > 0) ? 'The number is positive' : 
                 'The number is negative' ;
?>

输出如下:

The number is negative
The number is negative

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: