理解PHP';阅读if语句的方法

理解PHP';阅读if语句的方法,php,if-statement,Php,If Statement,PHP如何读取if语句? 我有以下按顺序排列的if语句 if ( $number_of_figures_in_email < 6) { -- cut: gives false } if($number_of_emails > 0) { -- cut: gives false } if ( $number_o

PHP如何读取if语句?

我有以下按顺序排列的if语句

if ( $number_of_figures_in_email < 6) {
       -- cut: gives false
}


if($number_of_emails > 0) {                                                                         
      -- cut: gives false
} 

if ( $number_of_emails == 0) {
   -- cut: gives true
}
if($U电子邮件中的数字数量<6){
--剪:给假
}
如果($numberofemail>0){
--剪:给假
} 
如果($电子邮件数量==0){
--切:真的吗
}
代码的行为是随机的。它有时会转到第三个if子句并给我一个成功的结果,而有时会转到前两个if子句中的一个,当输入变量为常量时


这表明我不能只使用if语句进行编码

如果希望只返回多个不同If语句的一个结果,请使用
elseif
,如下所示:

if ( $number_of_figures_in_email < 6) {
       -- cut: gives false
}
elseif($number_of_emails > 0) {                                                                         
      -- cut: gives false
} 
elseif ( $number_of_emails == 0) {
   -- cut: gives true
}
if($U电子邮件中的数字数量<6){
--剪:给假
}
elseif($电子邮件数量>0){
--剪:给假
} 
elseif(电子邮件数量==0){
--切:真的吗
}
它不是“随机行为”,它会按照您的指示执行:

if ($a) {
    // do A
}

if ($b) {
    // do B
}

if ($c) {
    // do C
}
所有三个
ifs
都是相互独立的。如果
$a
$b
$c
都是
真的
,它将执行a、b和c。如果只有
$a
$c
是真的,它将执行a和c,依此类推

如果要寻找更多“相互依赖”的条件,请使用
If..else
或嵌套
ifs

if ($a) {
    // do A and nothing else
} else if ($b) {
    // do B and nothing else (if $a was false)
} else if ($c) {
    // do C and nothing else (if $a and $b were false)
} else {
    // do D and nothing else (if $a, $b and $c were false)
}
在上述情况下,只执行一个操作

if ($a) {
    // do A and stop
} else {
    // $a was false
    if ($b) {
        // do B
    }
    if ($c) {
        // do C
    }
}
在上面的示例中,B和C都可以完成,但前提是
$a
为false

顺便说一句,这是非常通用的,而不是特定于PHP的