繁体   English   中英

if语句中的if语句

[英]If statement within an if statement

如何将if语句放入if语句中? 现在就是这样;

<?php
if($var1===$var2)
{
if($condition1 > 0)
{
*lots of code here*
}
}
else
{
*lots of code here again*
}
}
?>

这意味着如果$ var1与$ var2不匹配,我希望$ condition1大于0。 但就目前情况而言,我正在复制“大量代码部分”,所以我只想这样做。

if($var1!=$var2){ -apply if statement- } 
*lots of code here*
if($var1!=$var2){ -close if statement- } 

但是怎么样?

您有组合两个if语句的正确方法。 但是,当var1等于var2或condition1大于0时,您要运行很多代码。您可以这样编写:

<?php
if ($var1===$var2 || $condition1 > 0)
{
    *lots of code here again*
}
?>

|| 运算符的意思是“或”。

<?php
$a = ($var1 === $var2);
$b = ($condition1 > 0);

if (!$a || $b)
{
*lots of code here*
}
?>

也许我不明白,但我会这样:

if($var1 === $var2 || $condition1>0){

//lots of code here

}else{

}

编辑-也许你不想要-它读取var1等于var 2还是var1不等于var2并且condition1> 0做很多代码

if($var1 === $var2 || ($var1 !== $var2 && $condition1>0)){

//lots of code here

}else{


}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM