PHP 9: 表达式
本章介绍PHP的表达式。
PHP的表达式其实和其他语言没有什么区别。普通的赋值是表达式,函数也是表达式,通过函数赋值也是。三元条件运算符也是,即:
最后举个来自PHP网站上的例子好了:
PHP的表达式其实和其他语言没有什么区别。普通的赋值是表达式,函数也是表达式,通过函数赋值也是。三元条件运算符也是,即:
$first ? $second : $third
这个很多语言里都有,不再多说。最后举个来自PHP网站上的例子好了:
1 <?php
2 function double($i)
3 {
4 return $i*2;
5 }
6 $b = $a = 5; /* assign the value five into the variable $a and $b */
7 $c = $a++; /* post-increment, assign original value of $a
8 (5) to $c */
9 $e = $d = ++$b; /* pre-increment, assign the incremented value of
10 $b (6) to $d and $e */
11
12 /* at this point, both $d and $e are equal to 6 */
13
14 $f = double($d++); /* assign twice the value of $d before
15 the increment, 2*6 = 12 to $f */
16 $g = double(++$e); /* assign twice the value of $e after
17 the increment, 2*7 = 14 to $g */
18 $h = $g += 10; /* first, $g is incremented by 10 and ends with the
19 value of 24. the value of the assignment (24) is
20 then assigned into $h, and $h ends with the value
21 of 24 as well. */
22 ?>
2 function double($i)
3 {
4 return $i*2;
5 }
6 $b = $a = 5; /* assign the value five into the variable $a and $b */
7 $c = $a++; /* post-increment, assign original value of $a
8 (5) to $c */
9 $e = $d = ++$b; /* pre-increment, assign the incremented value of
10 $b (6) to $d and $e */
11
12 /* at this point, both $d and $e are equal to 6 */
13
14 $f = double($d++); /* assign twice the value of $d before
15 the increment, 2*6 = 12 to $f */
16 $g = double(++$e); /* assign twice the value of $e after
17 the increment, 2*7 = 14 to $g */
18 $h = $g += 10; /* first, $g is incremented by 10 and ends with the
19 value of 24. the value of the assignment (24) is
20 then assigned into $h, and $h ends with the value
21 of 24 as well. */
22 ?>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?