編程新手并尋求幫助,
我需要根據我嘗試使用 if 陳述句但沒有任何運氣的 $egg_type 的值來設定 $b_num 的值
`
$egg_type = $row["egg_type"] ;
if ($egg_type == 'M/S Select Farm')
{
$b_num = '1';
}
if ($egg_type = 'Free Range')
{
$b_num = '1';
}
if ($egg_type = 'Barn')
{
$b_num = '2';
}
if ($egg_type =='Intensive')
{
$b_num = '3';
}
if ($egg_type == 'Organic')
{
$b_num = '0';
}
if ($egg_type == 'Brioche')
{
$b_num = '3';
}
`
嘗試了 if 陳述句,但值沒有改變,
uj5u.com熱心網友回復:
首先,您必須檢查是否設定了 $egg_type 變數,以免以后出錯。其次,您可以這樣做
if(isset($egg_type)) {
if(in_array($egg_type, ['M/S Select Farm', 'Free Range'])) {
$b_num = 1;
} elseif(in_array($egg_type, ['Intensive', 'Brioche'])) {
$b_num = 3;
} elseif($egg_type === 'Organic') {
$b_num = 0;
} elseif($egg_type === 'Barn') {
$b_num = 2;
}
}
uj5u.com熱心網友回復:
也許$egg_type是空值。因此,您可以改進您的代碼(PHP 8.0 版本)并為 b_num 設定默認值,如下所示:
$egg_type = $row["egg_type"] ;
$b_num = match($egg_type) {
'M/S Select Farm' => 1,
'Free Range' => 1,
'Barn' => 1,
'Intensive' => 1,
'Organic' => 1,
'Brioche' => 1,
default => 'nothing any value for b_num'
};
echo $b_num;
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/530845.html
標籤:phpmysql变量
上一篇:如何在一行之間列印輸入值?
下一篇:從python中的主程式訪問變數
