下面的示例显示了赋值和复合赋值运算符的用法:
- = 赋值运算符
- += 加法与赋值运算符
- -= 减法与赋值运算符
- *= 乘法与赋值运算符
- /= 除法与赋值运算符
- **= 指数与赋值运算符
- %= 模与赋值运算符
<?php
$a = 25;
echo "a = $a\n";
//加法与赋值运算符
$a += 5;
echo "\$a += 5; 使 a = $a\n";
//减法与赋值运算符
$a -= 8;
echo "\$a -= 8; 使 a = $a\n";
//乘法与赋值运算符
$a *= 4;
echo "\$a *= 4; 使 a = $a\n";
//除法与赋值运算符
$a /= 8;
echo "\$a /= 8; 使 a = $a\n";
//指数与赋值运算符
$a **= 2;
echo "\$a **= 2; 使 a = $a\n";
//模与赋值运算符
//返回整数除法的余数
$a %= 7;
echo "\$a %= 7; 使 a = $a\n";
?>
上述代码的输出将是:
a = 25
$a += 5; 使 a = 30
$a -= 8; 使 a = 22
$a *= 4; 使 a = 88
$a /= 8; 使 a = 11
$a **= 2; 使 a = 121
$a %= 7; 使 a = 2