简体   繁体   English

if语句中的if语句

[英]If statement within an if statement

How do I put an if statement within an if statement? 如何将if语句放入if语句中? Right now it's like this; 现在就是这样;

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

Meaning that I want $condition1 to be bigger than 0 IF $var1 does not match $var2. 这意味着如果$ var1与$ var2不匹配,我希望$ condition1大于0。 But as it stands I am duplicating the "lots of code part" so I just want to; 但就目前情况而言,我正在复制“大量代码部分”,所以我只想这样做。

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

But how? 但是怎么样?

You have the right way of combining two if statements. 您有组合两个if语句的正确方法。 However, you want to run lots of code either when var1 equals var2 or when condition1 is bigger than 0. You can write that like this: 但是,当var1等于var2或condition1大于0时,您要运行很多代码。您可以这样编写:

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

The || || operator means 'or'. 运算符的意思是“或”。

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

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

Maybe i don't get it but i'd do it like this: 也许我不明白,但我会这样:

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

//lots of code here

}else{

}

EDIT - maybe you wan't this - it reads if var1 is equal to var 2 OR if var1 is not equal to var2 and condition1>0 do lots of code 编辑-也许你不想要-它读取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