7wu1wm0 发表于 2024-10-5 10:20:50

PHP 中的规律运算符 – 初学者指南


    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/94604628b8484ec5bc88fc7f66eaf5dd~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728303658&amp;x-signature=e1GkIMoAmh33vkqCWBRYGDcTUIw%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <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;">在 PHP 中,有四个基本<span style="color: black;">规律</span>运算符:AND、OR、NOT 和 XOR。本指南将<span style="color: black;">帮忙</span>您<span style="color: black;">认识</span>这些运算符,我将<span style="color: black;">运用</span>代码示例和<span style="color: black;">实质</span>用例解释它们的工作原理。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">规律</span>与运算符 (&amp;&amp;)</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">AND 运算符的写法类似于“&amp;&amp;”,仅当其两个操作数都为 true 时,其计算结果才为 true。<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>用于在 if 语句或循环中组合多个<span style="color: black;">要求</span>。它有助于<span style="color: black;">保证</span>满足所有<span style="color: black;">要求</span>以使整体<span style="color: black;">要求</span>为真。</span></p>
    <span style="color: black;">// Checking if two conditions are true using the AND operator</span>
    $mx = <span style="color: black;">5</span>;
    $my = <span style="color: black;">10</span>;

    <span style="color: black;">if</span> ($mx &gt; <span style="color: black;">0</span> &amp;&amp; $my &gt; <span style="color: black;">0</span>) {
    <span style="color: black;">echo</span> <span style="color: black;">"Both conditions are true!"</span>;
    } <span style="color: black;">else</span> {
    <span style="color: black;">echo</span> <span style="color: black;">"This condition is false."</span>;
    }<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在上面的示例中,AND 运算符验证 </span><span style="color: black;">$x</span> 和 <span style="color: black;">$y</span> 都大于 0。</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>都为真,则 if 块内的代码将执行,并<span style="color: black;">表示</span>“两个<span style="color: black;">要求</span>都为真!”。<span style="color: black;">或</span>,当 else 块被触发时,它<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>将焦点转移到 AND 运算符的对应项 – OR 运算符。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">规律</span>或运算符 (||)</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">PHP OR 运算符的写法类似于“||”,<span style="color: black;">倘若</span><span style="color: black;">最少</span>有一个操作数为 true,则返回 true。仅当两个操作数均为 false 时,它的计算结果才为 false。</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></p>
    <span style="color: black;">// Checking if at least one condition is true using the OR operator</span>
    $mx = <span style="color: black;">5</span>;
    $my = <span style="color: black;">10</span>;

    <span style="color: black;">if</span> ($mx &gt; <span style="color: black;">0</span> || $my &gt; <span style="color: black;">0</span>) {
    <span style="color: black;">echo</span> <span style="color: black;">"At least one condition is true!"</span>;
    } <span style="color: black;">else</span> {
    <span style="color: black;">echo</span> <span style="color: black;">"Both conditions are false."</span>;
    }<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>示例中,OR 运算符<span style="color: black;">检测</span> </span><span style="color: black;">$x</span>或<span style="color: black;">$y</span> (或两者)<span style="color: black;">是不是</span>大于 0。<span style="color: black;">倘若</span>任何<span style="color: black;">要求</span>为 true,则 if 块内的代码将执行,<span style="color: black;">表示</span>“<span style="color: black;">最少</span>有一个<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;">否则,else 块将执行,<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>探索 NOT 运算符并<span style="color: black;">认识</span>它的工作原理。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">规律</span>非运算符 (!)</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">NOT 运算符,写法类似于“!”,是一个一元运算符,用于反转其操作数的值。当操作数为假时,将返回真。反之,当操作数为 true 时,将返回 false。</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>
    <span style="color: black;">// Checking if a condition is false using the NOT operator</span>
    $x = <span style="color: black;">5</span>;

    <span style="color: black;">if</span> (!($x &gt; <span style="color: black;">10</span>)) {
    <span style="color: black;">echo</span> <span style="color: black;">"Condition is false!"</span>;
    } <span style="color: black;">else</span> {
    <span style="color: black;">echo</span> <span style="color: black;">"Condition is true."</span>;
    }<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在上面的示例中,NOT 运算符对<span style="color: black;">要求</span> </span><span style="color: black;">$x &gt; 10</span>的结果求反。<span style="color: black;">倘若</span><span style="color: black;">要求</span>为假(在本例中<span style="color: black;">便是</span><span style="color: black;">这般</span>),则 if 块内的代码将执行,并<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>为真,则执行 else 块将确认<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> PHP 中的 XOR 运算符,更好地<span style="color: black;">认识</span>其用法和<span style="color: black;">行径</span>。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">规律</span> XOR 运算符(异或)</h1>
    <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>特定的 XOR 运算符,但<span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">运用</span>其他<span style="color: black;">规律</span>运算符的组合来模拟 XOR <span style="color: black;">行径</span>。<span style="color: black;">倘若</span>其中一个操作数为 true,则 XOR 返回 true;<span style="color: black;">倘若</span>两个操作数都为 true 或 false,则 XOR 返回 false。</span></p><span style="color: black;">// XOR operator implementation using AND, OR, and NOT operators</span>
    $x = <span style="color: black;">true</span>;
    $y = <span style="color: black;">false</span>;

    <span style="color: black;">if</span>(($x || $y) &amp;&amp; !($x &amp;&amp; $y)) {<span style="color: black;">echo</span> <span style="color: black;">"Exactly one condition is true (XOR)!"</span>;
    } <span style="color: black;">else</span> {
    <span style="color: black;">echo</span> <span style="color: black;">"Both conditions are either true or false."</span>;
    }<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><span style="color: black;">$x</span> 或 <span style="color: black;">$y</span> <span style="color: black;">是不是</span>为 true ( <span style="color: black;">$x || $y</span>) 并<span style="color: black;">保证</span>两个<span style="color: black;">要求</span><span style="color: black;">区别</span>时为 true 来创建 XOR <span style="color: black;">行径</span>时间(<span style="color: black;">!($x &amp;&amp; $y)</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>为真,则 if 块内的代码将执行,<span style="color: black;">表示</span>“恰好一个<span style="color: black;">要求</span>为真 (XOR)!”否则,else 块将执行,<span style="color: black;">显示</span>两个<span style="color: black;">要求</span>要么为真,要么为假。</span></p>




qzmjef 发表于 2024-10-31 04:19:57

我深感你的理解与共鸣,愿对话长流。

nqkk58 发表于 2024-11-11 03:34:55

i免费外链发布平台 http://www.fok120.com/

4lqedz 发表于 7 天前

你的见解真是独到,让我受益匪浅。
页: [1]
查看完整版本: PHP 中的规律运算符 – 初学者指南