简体   繁体   中英

If statement within an if statement

How do I put an if statement within an if statement? 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. 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. 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:

<?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

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

//lots of code here

}else{


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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