[英]How to assign 2 variables with if statements in php?
我想為 3 種不同的條件分配寬度和高度變量。 但是,這不起作用:
if ($d=="1") $w=100 ;$h=200;
if ($d=="2") $w=200 ;$h=300;
if ($d=="3") $w=200 ;$h=400;
如何使用 php 對其進行編碼?
你注意到“# etc”部分了嗎?
if($c1){
$w=200;
$h=100;
}
elseif($c2){
$w=300;
$h=200;
}
else{
$w=400;
$h=300;
}
if($c1){
$w = 200;
$h = 100;
}
# etc
根據 OP 的評論,您缺少括號,還要確保您的變量$d
是字符串,因為比較字符串和整數會產生意外結果
if ($d=="1") {
$w=100 ;
$h=200;
} elseif ($d=="2") {
$w=200 ;
$h=300;
} elseif ($d=="3") {
$w=200 ;
$h=400;
} else {
//default values
}
我認為 switch case 會比 if 語句更好..
switch ($ch) {
case 1:
$w=200px;
$h=100px;
break;
case 2:
$w=300px;
$h=200px;
break;
case 3:
$w=400px ;
$h=300px;
break;
}
if($condition==1)
{
$w=200;
$h=100;
}else if($condition==2)
{
$w=300;
$h=200;
}else if($condition==3)
{
$w=400;
$h=300;
}
else
{
//Other Data
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.