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*
}
}
e开发者_JAVA百科lse
{
*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?
<?php
$a = ($var1 === $var2);
$b = ($condition1 > 0);
if (!$a || $b)
{
*lots of code here*
}
?>
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'.
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{
}
精彩评论