试试下面的例子就明白了条件运算符。复制并粘贴到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