www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons PHP Logical and Comparison Operators Bookmark and Share
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
Lesson 28
Lesson 29
Lesson 30
Lesson 31
Lesson 32
Lesson 33
Lesson 34
Lesson 35
Lesson 36
Lesson 37
Lesson 38
Lesson 39
Lesson 40
Lesson 41
Lesson 42
Lesson 43
Lesson 44
Apart from arithmetic operators, PHP also provides Comparison operators and Logical operators.

Comparison Operators
Comparison operators are used to make comparisons. They are used in control statements and loops etc. Control statements like if...else, switch and loops are discussed in the coming lessons.
  1. == is used to check the equality of its two operands e.g. 6==9 returns false.
  2. != is used to check the inequality of its two operands e.g. 6!=9 returns true.
  3. > is used to check whether the value on the left hand side of the > operator is greater than the value on its right e.g. 2 > 5 returns false.
  4. < is used to check whether the value on the left hand side of the < operator is less than the value on its right e.g. 2 < 5 returns true.
  5. >= is used to check whether the value on the left hand side of the > operator is greater than or equal to the value on its right e.g. 2 >= 5 returns false.
  6. <= is used to check whether the value on the left hand side of the < operator is less than or equal to the value on its right e.g. 2 <= 2 returns true.
Logical operators
PHP logical operators are often used in conjunction with PHP comparison operators in control statements and loops. PHP supports the following logical operators:

and, or, !, & &, ||

The and operator and && have the same meanings, but the precedence of the and is lower than &&. On the other hand the operator or and || also behave in the same way, but or has lower precedence than ||. The usage of && and || operators is preferred over their or / and counterrparts. Their usage is illustrated below:

The and (&&) operator
The and (&&) operator takes two boolean type operands or two expressions that return booleans. It returs true if both of its operands are true and false if either or both of its operands evaluate to false.

$a = true;
$b= = true;
if($a and $b) {// returns true ...can also be written as $a && $b;
...
}

The or (||) operator
The or (||) operator takes two boolean type operands or two expressions that return booleans. It returs true if either or both of its operands are true and false only if both of its operands evaluate to false.

$a = true;
$b= = false;
if($a or $b) {// returns true ...can also be written as $a || $b;
...
}

The ! operator
The ! is also known as the NOT operator. It takes a single boolean type operand or expression that returns a boolean. It returs true if the operand is false and false if the operand is true. In other words it simply negates its operand.

$a = true;
$b= = false;
if(!$a) // returns false
if(!$b) // returns true



Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.