PHP條件運算符示例
試試下麵的例子就明白了條件運算符。複製並粘貼到test.php文件,將下麵的PHP程序保存它在PHP服務器的文檔根目錄,使用瀏覽器瀏覽。
<html> <head><title>Arithmetical Operators</title><head> <body> <?php $a = 10; $b = 20; /* If condition is true then assign a to result otheriwse b */ $result = ($a > $b ) ? $a :$b; echo "TEST1 : Value of result is $result<br/>"; /* If condition is true then assign a to result otheriwse b */ $result = ($a < $b ) ? $a :$b; echo "TEST2 : Value of result is $result<br/>"; ?> </body> </html>
這將產生以下結果
TEST1 : Value of result is 20 TEST2 : Value of result is 10