nqkk58 发表于 2024-10-5 07:37:59

PHP 运算符和表达式


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">php<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;">一元运算符:取反运算符 !true ++ --</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">二元运算符:$a + $b</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">三元运算符:true?1:0</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">PHP运算符<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;">算术运算符:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">+ 加法运算 $a + $b</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">- 减法/取负运算 $a - $b</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">* 乘法运算 $a * $b</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/ 除法运算 $a / $b</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">% 求模运算(取余运算) $a % $b</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">++ 自加运算 $a++ ++$a</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">-- 自减运算 $a-- --$a</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">求模运算符号</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">重视</span>:php中的取模%运算只针对"整数",<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;">转换:简单去除小数部分。</p>$v1 =<span style="color: black;">7.5</span> % <span style="color: black;">3.5</span>; <span style="color: black;">//</span>结果是<span style="color: black;">1</span>,<span style="color: black;">由于</span><span style="color: black;">实质</span>运算的是:<span style="color: black;">7</span>%3
    $v2 = <span style="color: black;">7.5</span> % <span style="color: black;">3</span>; <span style="color: black;">//</span>结果是<span style="color: black;">1</span>,<span style="color: black;">由于</span><span style="color: black;">实质</span>运算的是:<span style="color: black;">7</span>%3<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">对比:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">javascript中,该运算符<span style="color: black;">能够</span>针对"小数":</p><span style="color: black;">var</span> v1 = <span style="color: black;">7.5</span> % <span style="color: black;">3.5</span>; <span style="color: black;">//结果是:0.5</span>
    <span style="color: black;">var</span> v2 = <span style="color: black;">7.5</span> % <span style="color: black;">3</span>; <span style="color: black;">//结果是:1.5</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">% 有两个目的: 整除运算; <span style="color: black;">掌控</span>范围,不要用小数, <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;">求模运算时会将%运算符两边的操作数转换为整型。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span>求模运算符判断闰年和平年</p>$year=<span style="color: black;">2010</span>;
    <span style="color: black;">if</span>(($year%4==<span style="color: black;">0</span> &amp;&amp; $year%100!=<span style="color: black;">0</span>) || $year%400==<span style="color: black;">0</span>){
    echo <span style="color: black;">"$year 是闰年"</span>;
    }<span style="color: black;">else</span>{
    echo <span style="color: black;">"$year 是平年"</span>;
    }<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span>求模运算符限定一个数的范围</p>$num=<span style="color: black;">rand</span>()%10; <span style="color: black;">//</span>让一个随机数不超过<span style="color: black;">10</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">自增自减运算符</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$a++ 先计算表达式的值, 执行递增的操作</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">++$a 先执行递增的操作, 再计算表达式的值</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$a-- 先计算表达式的值, 再执行递减的操作</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">--$a 先执行递减的操作, 在计算表达式的值</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">自增自减, 都只是针对"变量"值, 而<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;">常规应用:对数字进行自加1或自减1。</p><span style="color: black;">$i</span> = 10; <span style="color: black;">$i</span>++;(11) ++<span style="color: black;">$i</span>;(12)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">字符串: 只能自增,且自增的效果<span style="color: black;">便是</span>"下一个字符"</p><span style="color: black;">$s1</span> = <span style="color: black;">"A"</span>; <span style="color: black;">$s2</span> = <span style="color: black;">"ABC"</span>; <span style="color: black;">$s3</span> = <span style="color: black;">"XYZ"</span>; <span style="color: black;">$s4</span> = <span style="color: black;">"ZZZ"</span>; <span style="color: black;">$s5</span> = <span style="color: black;">"abZZ"</span>; <span style="color: black;">$s6</span>=<span style="color: black;">"ab9"</span>;
    <span style="color: black;">$s1</span>++; <span style="color: black;">$s2</span>++; <span style="color: black;">$s3</span>++; <span style="color: black;">$s4</span>++; <span style="color: black;">$s5</span>++; <span style="color: black;">$s6</span>++;
    <span style="color: black;">echo</span> <span style="color: black;">"s1=<span style="color: black;">$s1</span>, s2=<span style="color: black;">$s2</span>, s3=<span style="color: black;">$s3</span>, s4=<span style="color: black;">$s4</span>, s5=<span style="color: black;">$s5</span>, s6=<span style="color: black;">$s6</span>, "</span>;
    // 输出结果:s1=B, s2=ABD, s3=XZA, s4=AAAA, s5=acAA, s6=ac0,<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">重视</span>:这种字符的"自增",只针对62个字符有效:<span style="color: black;">体积</span>写字母和10个数字字符。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">布尔值递增递减无效</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">null递减无效,递增结果为1</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">前自增和后自增的辨析:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">自增有两种形式:$i++; ++$i; <span style="color: black;">倘若</span><span style="color: black;">她们</span>都是"独立一个语句",则<span style="color: black;">无</span>区别,都是自增1。</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;">$v1 = $i++; 或 echo $i++;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$v1 = ++$i; 或 echo ++$i;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">前++:$v1 = ++$i;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">先完成变量的递增运算,再取得该变量的值参与别的运算。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">分3步:(1)递增$i的值 (2)取得$i的值 (3)赋值给$v1</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">后++:$v1 = $i++; 先将原来变量的值临时存储,再将变量值递增,最后再将临时存储的值参与别的运算。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">分4步:(1)取得$i的值存起来 (2)递增$i的值 (3)取回刚才临时存储的值 (4)赋值给$v1</p><span style="color: black;">$i1</span> = 1;
    <span style="color: black;">$i2</span> = 1;
    <span style="color: black;">$v1</span> = <span style="color: black;">$i1</span>++;
    <span style="color: black;">$v2</span>= ++<span style="color: black;">$i2</span>;
    <span style="color: black;">echo</span> <span style="color: black;">"&lt;br /&gt;i1=<span style="color: black;">$i1</span>, v1=<span style="color: black;">$v1</span>"</span>;
    <span style="color: black;">echo</span> <span style="color: black;">"&lt;br /&gt;i2=<span style="color: black;">$i2</span>, v2=<span style="color: black;">$v2</span>"</span>;
    <span style="color: black;">echo</span> <span style="color: black;">"&lt;br /&gt;"</span>; <span style="color: black;">echo</span> <span style="color: black;">$i1</span>++;
    <span style="color: black;">echo</span> <span style="color: black;">"&lt;br /&gt;"</span>; <span style="color: black;">echo</span> ++<span style="color: black;">$i2</span>;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">输出结果:</p>i1=2, v1=1
    i2=2, v2=2
    2
    3<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">结论:<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;">倘若</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;">意见</span><span style="color: black;">运用</span>前自加);</p>$i= <span style="color: black;">1</span>;
    <span style="color: black;">echo</span> $i++; <span style="color: black;">//运行结果:10</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">重视</span>: echo内置结构其<span style="color: black;">亦</span>算运算的一种</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">字符串运算符: .</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">. <span style="color: black;">亦</span>叫连接运算符 {$变量名}<span style="color: black;">亦</span>可代替连接运算符的功能</p><span style="color: black;">$name</span>=<span style="color: black;">"tom"</span>;
    <span style="color: black;">$age</span>=27;
    <span style="color: black;">$height</span>=1.75;
    <span style="color: black;">echo</span> <span style="color: black;">"我的名子是:{<span style="color: black;">$name</span>}我的年龄是:{<span style="color: black;">$age</span>}我的身高是:{<span style="color: black;">$height</span>}米&lt;br&gt;"</span>;
    <span style="color: black;">echo</span> <span style="color: black;">我的名子是:</span>.<span style="color: black;">$name</span>.<span style="color: black;">我的年龄是:</span>.<span style="color: black;">$age</span>.<span style="color: black;">我的身高是:</span>.<span style="color: black;">$height</span>.<span style="color: black;">米</span>.<span style="color: black;">&lt;br&gt;</span>;
    <span style="color: black;">echo</span> <span style="color: black;">"\$age="</span>.<span style="color: black;">$age</span>;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">赋值运算符:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">= $x=3</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">+= $x+=3 等价于$x=$x+3</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">-+ $x-=3 等价于$x=$x-3</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">*= $x*=3 等价于$x=$x*3</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/= $x/=3 等价于$x=$x/3</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">%= $x%=3 等价于$x=$x%3</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.= $x.=3 等价于$x=$x.3</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">比较运算符:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">比较运算符用于对运算符两边的操作数进行比较,其结果只能是布尔值,<span style="color: black;">倘若</span>比较的关系为真,则结果为true,否则为false;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&gt; 大于</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&lt; <span style="color: black;">少于</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&gt;= 大于等于</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&lt;= <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>上"=="是先将两个操作数自动转为相同类型,<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>相<span style="color: black;">同期</span>返回true,否则返回false</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">!=或&lt;&gt; 不等于</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">!== 非全等于 当左边操作数不等于右边操作数时返回true,<span style="color: black;">或</span>它们的类型不相<span style="color: black;">同期</span>返回true,否则返回false</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">一般</span>,比较运算符都是针对"数字"进行,<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;">==和===比较</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">模糊相等:==, <span style="color: black;">暗示</span>两个数据"内容相同"就<span style="color: black;">能够</span>满足,<span style="color: black;">一般</span>会进行自动的转换。</p><span style="color: black;">1</span> <span style="color: black;">==</span> <span style="color: black;">1</span><span style="color: black;">;</span> <span style="color: black;">//显然成立</span>
    <span style="color: black;">1.0</span> <span style="color: black;">==</span> <span style="color: black;">1</span><span style="color: black;">;</span> <span style="color: black;">//成立</span>
    <span style="color: black;">1</span><span style="color: black;">==true</span> <span style="color: black;">//成立</span>
    <span style="color: black;">"1"</span> <span style="color: black;">==</span> <span style="color: black;">1</span><span style="color: black;">;</span> <span style="color: black;">//成立</span>
    <span style="color: black;">2</span> <span style="color: black;">==</span> <span style="color: black;">true</span> <span style="color: black;">//成立</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">严格相等:===,<span style="color: black;">暗示</span>2个数据"完全相等",必须<span style="color: black;">需求</span>数据类型和数据内容都<span style="color: black;">同样</span>才算相等。</p><span style="color: black;">1</span> === <span style="color: black;">1</span><span style="color: black;">; //成立</span>
    <span style="color: black;">1.0</span> === <span style="color: black;">1</span><span style="color: black;">; //不成立</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><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;">这里的比较,其实是值非"严格相等比较运算符"的比较,即排除这两个:=== , !==</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>两端有布尔值,转为布尔值比较,布尔值规则:true==true,false==false</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1== true; 成立</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2.2==true; 成立</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">"abc"==true; 成立</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">"0"==true; 不成立</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">"0"==false; 成立</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;">否则,两边都是纯数字字符串,转为数字比较:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">"123" == "123.0"; //相等的</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">"123" == "123abc"; //不相等</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">但<span style="color: black;">重视</span>: $v1 = 1 + "123abc"; //结果是124;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">否则, 就按字符串比较:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">字符串的比较规则:对每一个字符按<span style="color: black;">次序</span>从前往后比,碰到的<span style="color: black;">第1</span>个大的字符,则该字符串就算是大的,</p><span style="color: black;">"abc"</span> == <span style="color: black;">"abc"</span>; <span style="color: black;">//成立</span>
    <span style="color: black;">"abc"</span> &gt; <span style="color: black;">"abc"</span>; <span style="color: black;">//不成立</span>
    <span style="color: black;">"abc"</span> &gt; <span style="color: black;">"ab"</span>; <span style="color: black;">//成立</span>
    <span style="color: black;">"abc"</span> &gt; <span style="color: black;">"abb"</span>; <span style="color: black;">//成立</span>
    <span style="color: black;">"abc"</span> &gt; <span style="color: black;">"acb"</span>; <span style="color: black;">//不成立 <span style="color: black;">由于</span>第二个字符”b”&lt;”c”</span>
    <span style="color: black;">"abc"</span> &gt; <span style="color: black;">"ac"</span>; <span style="color: black;">//不成立 <span style="color: black;">由于</span>第二个字符”b”&lt;”c”</span>
    <span style="color: black;">"abc"</span> &gt; <span style="color: black;">"b"</span>; <span style="color: black;">//不成立, <span style="color: black;">由于</span>第1个字符”a”&lt;”b”</span>
    <span style="color: black;">"abcDFAFABBBBbbb"</span> &lt; <span style="color: black;">"b"</span>; <span style="color: black;">//成立</span>
    <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>运算用来判断一件事情是对的,还是错的,<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>运算符只能操作布尔型数值,处理后的结果值<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>运算符两边最好留有空格,代码可读性强;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">and或&amp;&amp; <span style="color: black;">规律</span>与 当两边操作数都为true时, 返回true, 否则返回false;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">or或|| <span style="color: black;">规律</span>或 当两边操作数都为false时, 返回false, 否则返回true;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">not或! <span style="color: black;">规律</span>非 当操作数为true时返回false, 否则返回true;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">xor <span style="color: black;">规律</span>异或 当<span style="color: black;">上下</span>两边的操作数<span style="color: black;">仅有</span>一个为true时返回true, 否则返回false</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">即一个操作数为true,另一个为false时返回true, 否则返回false</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">重视</span>:and和or<span style="color: black;">规律</span>运算符存在短路问题, 即后面的表达式不<span style="color: black;">必定</span>执行</p>$username=<span style="color: black;">"admin"</span>;
    $password=<span style="color: black;">"123456"</span>;
    $email=<span style="color: black;">""</span>;
    <span style="color: black;">if</span>($username==<span style="color: black;">"admin"</span> &amp;&amp; $password=<span style="color: black;">"123456"</span>){
    <span style="color: black;">echo</span> <span style="color: black;">"用户名和<span style="color: black;">秘码</span>输入是正确的&lt;br&gt;"</span>;
    }
    <span style="color: black;">if</span>($username==<span style="color: black;">""</span> || $password==<span style="color: black;">""</span> || $email==<span style="color: black;">""</span>){
    <span style="color: black;">echo</span> <span style="color: black;">"所有的值一个都<span style="color: black;">不可</span>为空&lt;br&gt;"</span>;
    }<span style="color: black;">if</span>((<span style="color: black;">isset</span>($email) &amp;&amp; !<span style="color: black;">empty</span>($email)) || (<span style="color: black;">isset</span>($phone) &amp;&amp; !<span style="color: black;">empty</span>($phone))){
    <span style="color: black;">echo</span> <span style="color: black;">"有一种联系方式"</span>;
    }<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">位运算符:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">任何信息在计算机中都是以二进制的形式<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;">将操作字符的ASCII值,浮点数<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;">无</span>借位和进位。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$a &amp; $b And(按位与) 将把$a和$b中都为1的位设为1。 1&amp;1=1 1&amp;0=0 0&amp;1=0 0&amp;0=0</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$a | $b Or(按位或) 将把$a<span style="color: black;">或</span>$b中为1的位设为1。 1|1=1 1|0=1 0|1=1 0|0=0</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$a ^ $b Xor(按位异或) 将把$a和$b中<span style="color: black;">区别</span>的位设为 1。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">~ $a Not(按位非) 将$a中为0的位设为1,反之<span style="color: black;">也</span>然。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$a &lt;&lt; $b Shift left(左移) 将$a中的位向左移动$b次(每一次移动都<span style="color: black;">暗示</span>"乘以2"),右边移空的部分补上0。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$a &gt;&gt; $b Shift right(右移) 将$a中的位向右移动$b次(每一次移动都<span style="color: black;">暗示</span>"除以2"),左边移空的部分补上0。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">重视</span>:&amp; | 不存在在短路问题,即位运算符的右边的值都会参与运算;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">其他运算符: ? : `` @ =&gt; -&gt; instanceof</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">?: 三元运算符<span style="color: black;">能够</span><span style="color: black;">供给</span>简单的<span style="color: black;">规律</span>判断,类似于<span style="color: black;">要求</span>语句if...else...</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(expr1)?(expr2):(expr3)</p>$a&lt;$b?$c=<span style="color: black;">1</span>:$c=<span style="color: black;">0</span>
    $action=(<span style="color: black;">empty</span>($_POST[<span style="color: black;">action</span>]))?<span style="color: black;">default</span>:$_POST[<span style="color: black;">action</span>];
    $action=(<span style="color: black;">isset</span>($_POST[<span style="color: black;">action</span>]))?$_POST[<span style="color: black;">action</span>]:<span style="color: black;">default</span>;
    $page=(<span style="color: black;">isset</span>($_GET[<span style="color: black;">page</span>]))?$_GET[<span style="color: black;">page</span>]:<span style="color: black;">1</span>; <span style="color: black;">//<span style="color: black;">经过</span>三元运算符判断分页变量的值;<span style="color: black;">倘若</span>变量存在,则直接输出变量值,否则,为变量赋值1</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">注意:三元运算符输入时要切换英文模式下(shift),否则程序会<span style="color: black;">显现</span>错误</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">`` 执行运算符,php将尝试反引号的内容<span style="color: black;">做为</span>操作系统命令执行,并将其输出信息返回,与函数shell_exec()等同</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$output=`ls-al`; linux操作系统下</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">@ 错误<span style="color: black;">掌控</span>运算符</p>$my_file=<span style="color: black;">@file(<span style="color: black;">non_existent_file</span>)</span>;
    @$num=<span style="color: black;">100</span>/<span style="color: black;">0</span>;
    echo <span style="color: black;">""</span>;
    <span style="color: black;">@header(<span style="color: black;">"Location:http://www.lampuser.com/"</span>)</span>; <span style="color: black;">运用</span>头发送函数前面<span style="color: black;">不可</span>有任何输出<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><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>错误, 只是抑制输出。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">instanceof 类型运算符</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">其格式: 实例化对象 instanceof 类名</p><span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">A</span> </span>{ }
    $thing = <span style="color: black;">new</span> A;
    <span style="color: black;">if</span> ($thing <span style="color: black;">instanceof</span> A) {
    <span style="color: black;">echo</span> <span style="color: black;">A</span>;
    }<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">隐式转换问题</p><span style="color: black;"><span style="color: black;">&lt;?php</span>
      $city = <span style="color: black;">"荆州"</span>;
      <span style="color: black;">if</span>($city == <span style="color: black;">0</span>){
      <span style="color: black;">unset</span>($city);
      }<span style="color: black;">echo</span> $city; <span style="color: black;">//荆州</span>
      <span style="color: black;">?&gt;</span></span><span style="color: black;"><span style="color: black;">&lt;?php</span>
      $city = <span style="color: black;">"荆州"</span>;
      <span style="color: black;">if</span>($city == <span style="color: black;">0</span>){ <span style="color: black;">//<span style="color: black;">这儿</span>存在隐式转换</span>
      <span style="color: black;">unset</span>($city);
      }
      <span style="color: black;">echo</span> $city; <span style="color: black;">//Notice: Undefined variable: city</span>
      <span style="color: black;">?&gt;</span></span>




4lqedz 发表于 2024-11-9 02:47:29

这夸赞甜到心里,让我感觉温暖无比。

7wu1wm0 发表于 2024-11-9 10:00:58

这夸赞甜到心里,让我感觉温暖无比。

qzmjef 发表于 2024-11-12 08:12:39

你的话语如春风拂面,温暖了我的心房,真的很感谢。
页: [1]
查看完整版本: PHP 运算符和表达式