fny5jt9 发表于 2024-10-5 10:27:23

PHP8的运算符-PHP8知识详解


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运算符是<span style="color: black;">能够</span><span style="color: black;">经过</span>给出的一或多个值(用编程行话<span style="color: black;">来讲</span>,表达式)来产生另一个值(因而<span style="color: black;">全部</span>结构<span style="color: black;">作为</span>一个表达式)的东西。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">PHP8的运算符有<span style="color: black;">非常多</span>,按类型分有一元运算符、二元运算符、三元运算符。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">一元运算符只对一个表达式执行操作,只能接受一个值,例如 !(<span style="color: black;">规律</span>取反运算符)或 ++(递增运算符)。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">二元运算符用于两个操作数之间,可接受两个值,例如<span style="color: black;">熟练</span>的算术运算符 +(加)和 -(减),大<span style="color: black;">都数</span> PHP 运算符都是这种。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">三元运算符用在三个操作数之间,可接受三个值,列如<span style="color: black;">独一</span>的三元运算符 ? :。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">本节<span style="color: black;">重点</span>的知识点有:运算符优先级、算术运算符、赋值运算符、位运算符、比较运算符、错误<span style="color: black;">掌控</span>运算符、执行运算符、递增/递减运算符、<span style="color: black;">规律</span>运算符、字符串运算符、数组运算符、类型运算符、三元运算符。</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/afbde8612c2f44c68f31de04dff49304~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728303808&amp;x-signature=GRGY7%2FQ%2BCzbBdfr4RU8JPRlGgPI%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">1、</span>运算符优先级</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">PHP8 的运算符完整列表见下面运算符优先级。解释了运算符优先级和结合方向,这<span style="color: black;">掌控</span>着在表达式<span style="color: black;">包括</span>有若干个<span style="color: black;">区别</span>运算符时<span style="color: black;">到底</span><span style="color: black;">怎么样</span>对其求值。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运算符优先级指定了两个表达式绑定得有多“紧密”。例如,表达式 1 + 5 * 3 的结果是 16 而不是 18 是<span style="color: black;">由于</span>乘号(“*”)的优先级比加号(“+”)高。必要时<span style="color: black;">能够</span>用括号来强制改变优先级。例如:(1 + 5) * 3 的值为 18。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>运算符优先级相同,那运算符的结合方向决定了该<span style="color: black;">怎样</span>运算。例如,"-"是左联的,<span style="color: black;">那样</span> 1 - 2 - 3 就等同于 (1 - 2) - 3 并且结果是 -4. <span style="color: black;">另一</span>一方面,"="是右联的,<span style="color: black;">因此</span> $a = $b = $c 等同于 $a = ($b = $c)。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">无</span>结合的相同优先级的运算符<span style="color: black;">不可</span>连在<span style="color: black;">一块</span><span style="color: black;">运用</span>,例如 1 &lt; 2 &gt; 1 在PHP是不合法的。但<span style="color: black;">另一</span>一方面表达式 1 &lt;= 1 == 1 是合法的, <span style="color: black;">由于</span> == 的优先级<span style="color: black;">小于</span> &lt;=。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">相关</span>性仅对二(三)元操作符有<span style="color: black;">道理</span>。 一元操作符是前缀<span style="color: black;">或</span>后缀,<span style="color: black;">因此</span>不适用该概念。 例如 !!$a 仅可分为 !(!$a)。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">括号的<span style="color: black;">运用</span>,哪怕在不是必要的场合下,<span style="color: black;">经过</span>括号的配对来<span style="color: black;">知道</span>标明运算<span style="color: black;">次序</span>,而非靠运算符优先级和结合性来决定,<span style="color: black;">一般</span>能够<span style="color: black;">增多</span>代码的可读性。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">下表<span style="color: black;">根据</span>优先级从高到低列出了运算符。同一行中的运算符<span style="color: black;">拥有</span>相同优先级,此时它们的结合方向决定求值<span style="color: black;">次序</span>。</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p26-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/962b12b98dc8444c9a357fd1646b61d8~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728303808&amp;x-signature=KmOnHNQNAoY3DnRboiGgG8DA0AI%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">2、</span>算术运算符</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1、+$a , 标识运算, <span style="color: black;">按照</span><span style="color: black;">状况</span>将 $a 转化为 int 或 float。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2、-$a ,取反运算, $a 的负值。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">3、$a + $b, 加法运算, $a 和 $b 的和。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">4、$a - $b, 减法运算, $a 和 $b 的差。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">5、$a * $b, 乘法运算, $a 和 $b 的积。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">6、$a / $b, 除法运算, $a 除以 $b 的商。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">7、$a % $b, 取模运算, $a 除以 $b 的余数。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">8、$a ** $b, 求幂运算, $a 的 $b次方的值。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">示例:</span></p>&lt;?php
    echo (5 % 3); // 打印 2
    echo "&lt;br&gt;"; //换行<span style="color: black;">表示</span>
    echo (5 % -3); // 打印 2
    echo "&lt;br&gt;"; //换行<span style="color: black;">表示</span>
    echo (-5 % 3); // 打印 -2
    echo "&lt;br&gt;"; //换行<span style="color: black;">表示</span>
    echo (-5 % -3); // 打印 -2
    ?&gt;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运行结果如下:</span></p>2
    2
    -2
    -2
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">3、</span>赋值运算符</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">赋值运算符的<span style="color: black;">功效</span>是把<span style="color: black;">必定</span>的数值加载给特定的变量。基本的赋值运算符是“=”。即把右边表达式的值赋给左边的运算数(变量)。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">3.1、算术赋值运算符</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1、$a += $b, 等同于 $a = $a + $b ,加法运算符</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2、$a -= $b, 等同于 $a = $a - $b ,减法运算符</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">3、$a *= $b, 等同于 $a = $a * $b ,乘法运算符</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">4、$a /= $b, 等同于 $a = $a / $b ,除法运算符</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">5、$a %= $b,等同于 $a = $a % $b, 取模运算符</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">6、$a **= $b ,等同于 $a = $a ** $b ,指数运算符</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">3.2、位赋值运算符</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1、$a &amp;= $b, 等同于$a = $a &amp; $b, 按位与</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2、$a |= $b, 等同于$a = $a | $b ,按位或</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">3、$a ^= $b ,等同于$a = $a ^ $b ,按位异或</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">4、$a &lt;&lt;= $b,等同于 $a = $a &lt;&lt; $b, 左移</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">5、$a &gt;&gt;= $b ,等同于$a = $a &gt;&gt; $b, 右移</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">3.3、其他赋值运算符</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1、$a .= $b, 等同于$a = $a . $b ,字符串拼接</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2、$a ??= $b, 等同于$a = $a ?? $b ,NULL 合并</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">4、</span>位运算符</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">位运算符<span style="color: black;">准许</span>对整型数中指定的位进行求值和操作。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1、$a &amp; $b ,And(按位与), 将把 $a 和 $b 中都为 1 的位设为 1。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2、$a | $b ,Or(按位或), 将把 $a 和 $b 中任何一个为 1 的位设为 1。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">3、$a ^ $b ,Xor(按位异或), 将把 $a 和 $b 中一个为 1 另一个为 0 的位设为 1。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">4、~ $a Not,(按位取反), 将 $a 中为 0 的位设为 1,反之<span style="color: black;">也</span>然。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">5、$a &lt;&lt; $b, Shift left(左移), 将 $a 中的位向左移动 $b 次(每一次移动都<span style="color: black;">暗示</span>“乘以 2”)。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">6、$a &gt;&gt; $b, Shift right(右移), 将 $a 中的位向右移动 $b 次(每一次移动都<span style="color: black;">暗示</span>“除以 2”)。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">要用括号<span style="color: black;">保证</span>想要的优先级。例如 $a &amp; $b == true 先进行比较再进行按位与;而 ($a &amp; $b) == true 则先进行按位与再进行比较。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">位移在 PHP 中是数学运算。向任何方向移出去的位都被丢弃。左移时右侧以零填充,符号位被移走<span style="color: black;">寓意</span>着正负号不被<span style="color: black;">保存</span>。右移时左侧以符号位填充,<span style="color: black;">寓意</span>着正负号被<span style="color: black;">保存</span>。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span> &amp;、 | 和 ^ 运算符的<span style="color: black;">上下</span>两个操作对象都是字符串,将对会<span style="color: black;">构成</span>字符串的字符 ASCII 值执行操作,结果<span style="color: black;">亦</span>是一个字符串。除此之外,两个操作对象都将 转换为整数 ,结果<span style="color: black;">亦</span>将会是整数。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span> ~ 运算符的操作对象是字符串,则将对<span style="color: black;">构成</span>字符串的字符 ASCII 值进行操作, 结果将会是字符串,否则操作对象和结构都会是整数。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">&lt;&lt; 和 &gt;&gt; 运算符的操作对象和结果始终都是整数。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">5、</span>比较运算符</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">比较运算符,如同它们名<span style="color: black;">叫作</span>所暗示的,<span style="color: black;">准许</span>对两个值进行比较。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1、$a == $b ,等于, true,<span style="color: black;">倘若</span>类型转换后 $a 等于 $b。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2、$a === $b, 全等于, true,<span style="color: black;">倘若</span> $a 等于 $b,并且它们的类型<span style="color: black;">亦</span>相同。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">3、$a != $b ,不等, true,<span style="color: black;">倘若</span>类型转换后 $a 不等于 $b。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">4、$a &lt;&gt; $b ,不等 ,true,<span style="color: black;">倘若</span>类型转换后 $a 不等于 $b。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">5、$a !== $b ,不全等 ,true,<span style="color: black;">倘若</span> $a 不等于 $b,<span style="color: black;">或</span>它们的类型<span style="color: black;">区别</span>。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">6,、$a &lt; $b, 小与, true,<span style="color: black;">倘若</span> $a 严格<span style="color: black;">少于</span> $b。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">7、$a &gt; $b, 大于, true,<span style="color: black;">倘若</span> $a 严格大于 $b。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">8、$a &lt;= $b, <span style="color: black;">少于</span>等于, true,<span style="color: black;">倘若</span> $a <span style="color: black;">少于</span><span style="color: black;">或</span>等于 $b。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">9,、$a &gt;= $b, 大于等于, true,<span style="color: black;">倘若</span> $a 大于<span style="color: black;">或</span>等于 $b。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">十、</span>$a &lt;=&gt; $b, 太空船运算符(组合比较符), 当$a<span style="color: black;">少于</span>、等于、大于 $b时 分别返回一个<span style="color: black;">少于</span>、等于、大于0的 int 值。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">6、</span>错误<span style="color: black;">掌控</span>运算符</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">PHP 支持一个错误<span style="color: black;">掌控</span>运算符“@”。当将其<span style="color: black;">安置</span>在一个 PHP 表达式之前,该表达式可能产生的任何错误诊断都被<span style="color: black;">控制</span>。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">7、</span>执行运算符</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">PHP 支持一个执行运算符:反引号(``)。<span style="color: black;">重视</span>这不是单引号!PHP 将尝试将反引号中的内容<span style="color: black;">做为</span> shell 命令来执行,并将其输出信息返回(即,<span style="color: black;">能够</span>赋给一个变量而不是简单地丢弃到标准输出)。<span style="color: black;">运用</span>反引号运算符“`”的效果与函数 shell_exec() 相同。</span></p>&lt;?php
    $output = `ls -al`;
    echo "&lt;pre&gt;$output&lt;/pre&gt;";
    ?&gt;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">重视</span>:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1、关闭了 shell_exec() 时反引号运算符是无效的。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2、与其它某些语言<span style="color: black;">区别</span>,反引号<span style="color: black;">不可</span>在双引号字符串中<span style="color: black;">运用</span>。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">8、</span>递增/递减运算符</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">PHP 支持 C 风格的前/后递增与递减运算符。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1、++$a, 前加: $a 的值加一,<span style="color: black;">而后</span>返回 $a。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2、$a++ ,后加: 返回 $a,<span style="color: black;">而后</span>将 $a 的值加一。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">3、--$a ,前减 :$a 的值减一, <span style="color: black;">而后</span>返回 $a。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">4、$a-- ,后减 :返回 $a,<span style="color: black;">而后</span>将 $a 的值减一。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">9、</span><span style="color: black;">规律</span>运算符</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">一个编程语言最重要的功能之一<span style="color: black;">便是</span>进行<span style="color: black;">规律</span>判断和运算,<span style="color: black;">例如</span><span style="color: black;">规律</span>与、<span style="color: black;">规律</span>或、<span style="color: black;">规律</span>非、<span style="color: black;">规律</span>异或。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1、$a and $b ,And(<span style="color: black;">规律</span>与): true,<span style="color: black;">倘若</span> $a 和 $b 都为 true。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2、$a or $b, Or(<span style="color: black;">规律</span>或): true,<span style="color: black;">倘若</span> $a 或 $b 任一为 true。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">3、$a xor $b, Xor(<span style="color: black;">规律</span>异或): true,<span style="color: black;">倘若</span> $a 或 $b 任一为 true,但<span style="color: black;">区别</span>时是。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">4、! $a, Not(<span style="color: black;">规律</span>非): true,<span style="color: black;">倘若</span> $a 不为 true。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">5、$a &amp;&amp; $b ,And(<span style="color: black;">规律</span>与): true,<span style="color: black;">倘若</span> $a 和 $b 都为 true。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">6、$a || $b, Or(<span style="color: black;">规律</span>或): true,<span style="color: black;">倘若</span> $a 或 $b 任一为 true。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">十、字符串运算符</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">有两个字符串(string)运算符。<span style="color: black;">第1</span>个是连接运算符(“.”),它返回其<span style="color: black;">上下</span>参数连接后的字符串。第二个是连接赋值运算符(“.=”),它将右边参数附加到左边的参数之后。</span></p>&lt;?php
    $a = "你好"; //给变量$a赋值
    $b = $a . "PHP8"; //<span style="color: black;">此刻</span> $b <span style="color: black;">包括</span> "你好 PHP8"
    echo $b; //输出变量$b
    echo "&lt;br&gt;"; //换行<span style="color: black;">表示</span>
    $a = "你好"; //给变量$a赋值
    $a .= "PHP8"; //<span style="color: black;">此刻</span> $a <span style="color: black;">包括</span> "你好 PHP8"
    echo $a; //不打引号输出变量$a
    echo "&lt;br&gt;"; //换行<span style="color: black;">表示</span>
    echo $a; //打单引号输出变量$a
    echo "&lt;br&gt;"; //换行<span style="color: black;">表示</span>
    echo "$a"; //打双引号输出变量$a
    ?&gt;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运行结果如下:</span></p>你好PHP8
    你好PHP8
    $a
    你好PHP8
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">温馨提醒,<span style="color: black;">针对</span>字符串型数据输出,<span style="color: black;">能够</span><span style="color: black;">运用</span>单引号,<span style="color: black;">亦</span><span style="color: black;">能够</span><span style="color: black;">运用</span>双引号,<span style="color: black;">亦</span><span style="color: black;">能够</span>不<span style="color: black;">运用</span>引号。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">运用</span>单引号和双引号输出同一个变量,结果是<span style="color: black;">区别</span>的。单引号输出的是字符串,双引号和不带引号输出的是变量的值。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">十<span style="color: black;">1、</span>数组运算符</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1、$a + $b ,联合: $a 和 $b 的联合。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2、$a == $b ,相等 :<span style="color: black;">倘若</span> $a 和 $b <span style="color: black;">拥有</span>相同的键/值对则为 true。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">3、$a === $b ,全等: <span style="color: black;">倘若</span> $a 和 $b <span style="color: black;">拥有</span>相同的键/值对并且<span style="color: black;">次序</span>和类型都相同则为 true。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">4、$a != $b, 不等 :<span style="color: black;">倘若</span> $a 不等于 $b 则为 true。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">5、$a &lt;&gt; $b, 不等 :<span style="color: black;">倘若</span> $a 不等于 $b 则为 true。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">6、$a !== $b, 不全等: <span style="color: black;">倘若</span> $a 不全等于 $b 则为 true。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">+ 运算符把右边的数组元素附加到左边的数组后面,两个数组中都有的键名,则只用左边数组中的,右边的被忽略。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">数组中的单元<span style="color: black;">倘若</span><span style="color: black;">拥有</span>相同的键名和值则比较时相等。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">十<span style="color: black;">2、</span>类型运算符</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">运用</span>instanceof 来进行实例引用。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1、确定一个 PHP 变量<span style="color: black;">是不是</span>属于某一类 class 的实例:</span></p>&lt;?php
    class MyClass
    {
    }

    class NotMyClass
    {
    }
    $a = new MyClass;

    var_dump($a instanceof MyClass);
    var_dump($a instanceof NotMyClass);
    ?&gt;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">以上示例会输出:</span></p>bool(true)
    bool(false)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2、确定一个变量是不是继承自某一父类的子类的实例:</span></p>&lt;?php
    class ParentClass
    {
    }

    class MyClass extends ParentClass
    {
    }

    $a = new MyClass;

    var_dump($a instanceof MyClass);
    var_dump($a instanceof ParentClass);
    ?&gt;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">以上示例会输出:</span></p>bool(true)
    bool(true)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">3、<span style="color: black;">检测</span>一个对象<span style="color: black;">是不是</span>不是某个类的实例</span></p>&lt;?php
    class MyClass
    {
    }

    $a = new MyClass;
    var_dump(!($a instanceof stdClass));
    ?&gt;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">以上示例会输出:</span></p>bool(true)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">4、用于确定一个变量是不是实现了某个接口的对象的实例</span></p>&lt;?php
    interface MyInterface
    {
    }

    class MyClass implements MyInterface
    {
    }

    $a = new MyClass;

    var_dump($a instanceof MyClass);
    var_dump($a instanceof MyInterface);
    ?&gt;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">以上示例会输出:</span></p>bool(true)
    bool(true)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">十<span style="color: black;">3、</span>三元运算符</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">三元运算符是一种在编程中常用的<span style="color: black;">要求</span>语句,<span style="color: black;">亦</span>被<span style="color: black;">叫作</span>为三目运算符或<span style="color: black;">要求</span>运算符。它<span style="color: black;">一般</span>用于简化<span style="color: black;">要求</span>判断和赋值操作。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在PHP8中,三元运算符“?:”被<span style="color: black;">叫作</span>为“三元运算符”或“三目运算符”,其语法格式为:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">要求</span>表达式 ? 表达式1 : 表达式2</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">其中,<span style="color: black;">要求</span>表达式为一个布尔表达式,<span style="color: black;">倘若</span>该表达式的值为真,则返回表达式1的值;<span style="color: black;">倘若</span>该表达式的值为假,则返回表达式2的值。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">例如,以下是一个<span style="color: black;">运用</span>三元运算符的示例:</span></p>&lt;?php
    $a = "99&gt;88";
    $b = $a ?: "100不大于99";
    $c = $a ? "100大于99" : "100不大于99";
    echo $b;
    echo "&lt;br&gt;";
    echo $c;
    ?&gt;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运行结果如下:</span></p>99&gt;88
    100大于99
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">到此为止,本文<span style="color: black;">重点</span>讲解的运算符优先级、算术运算符、赋值运算符、位运算符、比较运算符、错误<span style="color: black;">掌控</span>运算符、执行运算符、递增/递减运算符、<span style="color: black;">规律</span>运算符、字符串运算符、数组运算符、类型运算符、三元运算符就讲解完毕了,<span style="color: black;">大众</span>慢慢参考学习。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">space</span></p>




b1gc8v 发表于 2024-10-21 01:23:05

楼主发的这篇帖子,我觉得非常有道理。
页: [1]
查看完整版本: PHP8的运算符-PHP8知识详解