PHP 逻辑运算符示例

PHP 逻辑运算符示例

尝试以下示例以了解所有的逻辑运算符。将以下PHP程序复制并粘贴到test.php文件中,并将其保存在您的PHP服务器的文档根目录中,然后使用任何浏览器浏览它。

<html>

   <head>
      <title>Logical Operators</title>
   </head>

   <body>

      <?php
         a = 42;b = 0;

         if( a &&b ) {
            echo "TEST1 : Both a and b are true<br/>";
         }else{
            echo "TEST1 : Either a or b is false<br/>";
         }

         if( a andb ) {
            echo "TEST2 : Both a and b are true<br/>";
         }else{
            echo "TEST2 : Either a or b is false<br/>";
         }

         if( a ||b ) {
            echo "TEST3 : Either a or b is true<br/>";
         }else{
            echo "TEST3 : Both a and b are false<br/>";
         }

         if( a orb ) {
            echo "TEST4 : Either a or b is true<br/>";
         }else {
            echo "TEST4 : Both a and b are false<br/>";
         }

         a = 10;b = 20;

         if( a ) {
            echo "TEST5 : a is true <br/>";
         }else {
            echo "TEST5 : a  is false<br/>";
         }

         if(b ) {
            echo "TEST6 : b is true <br/>";
         }else {
            echo "TEST6 : b  is false<br/>";
         }

         if( !a ) {
            echo "TEST7 : a is true <br/>";
         }else {
            echo "TEST7 : a  is false<br/>";
         }

         if( !b ) {
            echo "TEST8 : b is true <br/>";
         }else {
            echo "TEST8 : b  is false<br/>";
         }
      ?>

   </body>
</html>

这将产生以下结果 −

TEST1 : Either a or b is false
TEST2 : Either a or b is false
TEST3 : Either a or b is true
TEST4 : Either a or b is true
TEST5 : a is true 
TEST6 : b is true 
TEST7 : a is false
TEST8 : b is false

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程