PHP学习笔记3-逻辑运算符

时间:2021-03-27 08:23:55

逻辑运算符图解:

PHP学习笔记3-逻辑运算符

逻辑且&&:

 <?php
 /**
  * Created by PhpStorm.
  * User: Administrator
  * Date: 2015/6/26
  * Time: 18:55
  */
 function traceNum(){
     for($i=0;$i<=100;$i++){
         if($i%2==0 && $i%3==0){ //%:求余;输出能同时被2和3整除的数;
             echo $i."<br/>";
         }
     }
 }
 traceNum();

逻辑或||:

 <?php
 /**
  * Created by PhpStorm.
  * User: Administrator
  * Date: 2015/6/26
  * Time: 18:55
  */
 function traceNum2(){
     for($j=0;$j<=100;$j++){
         if($j%2==0 || $j%3==0){//输出能被2整除或被3整除的数;
             echo $j."<br/>";
         }
     }
 }
 traceNum2();

逻辑非!:

 <?php
 /**
  * Created by PhpStorm.
  * User: Administrator
  * Date: 2015/6/26
  * Time: 18:55
  */
 function traceNum3(){
     for($m=0;$m<=100;$m++){
         if($m%2!=0){//输出奇数;
             echo $m."<br/>";
         }
     }
 }
 traceNum3();