位置:首頁 > 腳本語言 > PHP教學 > PHP賦值運算符示例

PHP賦值運算符示例

試試下麵的例子就明白了所有的賦值運算符。複製並粘貼到test.php文件,將下麵的PHP程序,保存它到PHP服務器的文檔根目錄,使用任何瀏覽器瀏覽。

<html>
<head><title>PHP賦值運算符示例</title><head>
<body>
<?php
    $a = 42;
    $b = 20;
    
    $c = $a + $b;   /* Assignment operator */
    echo "Addtion Operation Result: $c <br/>";
    $c += $a;  /* c value was 42 + 20 = 62 */
    echo "Add AND Assigment Operation Result: $c <br/>";
    $c -= $a; /* c value was 42 + 20 + 42 = 104 */
    echo "Subtract AND Assignment Operation Result: $c <br/>";
    $c *= $a; /* c value was 104 - 42 = 62 */
    echo "Multiply AND Assignment Operation Result: $c <br/>";
    $c /= $a;  /* c value was 62 * 42 = 2604 */
    echo "Division AND Assignment Operation Result: $c <br/>";
    $c %= $a; /* c value was 2604/42 = 62*/
    echo "Modulus AND Assignment Operation Result: $c <br/>";
?>
</body>
</html>

這將產生以下結果

Addtion Operation Result: 62
Add AND Assigment Operation Result: 104
Subtract AND Assignment Operation Result: 62
Multiply AND Assignment Operation Result: 2604
Division AND Assignment Operation Result: 62
Modulus AND Assignment Operation Result: 20