PHP中elseif與else if的區別
阿新 • • 發佈:2017-09-04
else elseif if if ($a > $b):
echo ‘a is larger than b‘;
else if ($a < $b):
echo ‘a is smaller than b‘;
else:
echo ‘a is equals b‘;
endif;
?> endif;
?>
在PHP中,正常情況下elseif和else if的用法及效果是一樣的,但在使用帶冒號的if語句時(也就是php替代)情況會有點不一樣,比如:
<?php
/*正確的寫法*/
$a = 1;
$b = 2;
if ($a > $b):
echo ‘a is larger than b‘;
elseif ($a < $b):
echo ‘a is smaller than b‘;
else:
echo ‘a is equals b‘;
endif;
==================
/*錯誤的寫法,解析器會報錯*/
$a = 1;
$b = 2;
echo ‘a is larger than b‘;
else if ($a < $b):
echo ‘a is smaller than b‘;
else:
echo ‘a is equals b‘;
endif;
?>
php替代實例
<?php
if ($a == 5):
echo "a equals 5";
echo "...";
elseif ($a == 6):
echo "a equals 6";
echo "!!!";
else:
echo "a is neither 5 nor 6";
?>
本文出自 “Linux獄長” 博客,請務必保留此出處http://sswqzx.blog.51cto.com/2494644/1962241
PHP中elseif與else if的區別