簡體   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