PHPのif/then文の「then」部分で論理演算子を使用することは可能ですか?
これは私のコードです:
if ($TMPL['duration'] == NULL) {
$TMPL['duration'] = ('120' or '124' or '114' or '138'); }
else {
$TMPL['duration'] = ''.$TMPL['duration']; }
PHPのif/then文の「then」部分で論理演算子を使用することは可能ですか?
これは私のコードです:
if ($TMPL['duration'] == NULL) {
$TMPL['duration'] = ('120' or '124' or '114' or '138'); }
else {
$TMPL['duration'] = ''.$TMPL['duration']; }
を使用しelse if
ます。
$a = 1;
if($a === 1) {
// do something
} else if ($a === 2) {
// do something else
}
ほとんどの場合、次のように switch ステートメントの方が適していることに注意してください。
switch($a) {
case 1:
// do something
break;
case 2:
// do something else
break;
}
また:
switch(TRUE) {
case $a === 1 :
// do something else
break;
case $b === 2 :
// do something else
break;
}
また、以下を利用して次のようなこともできますin_array
。
if ($TMPL['duration'] === NULL
|| in_array($TMPL['duration'], array('120','124','114','138')) {
// Do something if duration is NULL or matches any item in the array
} else {
// Do something if duration is not NULL or does not match any item in array
}