CopyPastehas never been so tasty!

Comparison Operators in PHP

by anonymous

  • 0
  • 0
  • 0
78 views

Orignal Source:http://codingvilla.com/comparison-operators-php-single-article-591.aspx

Comparison Operators in PHP:

 

Comparison operators are used to compare values of variables and make decision on the base of compared values. Comparison operations are used inside condition statements

 

Comparison Operators in used in PHP and their symbols

 

Equal(==) 

Equal (===)

Not equal ( !=)

Less than(<)

Greater than(>)

Less than or equal to(=<)

Greater than or equal to(>=)

 

Equal Operator(==)

 

Equal operator(==) only check values are equal or  not values type.

 

Code 1:

 

<?php

$var1="php";

$var2="php";

 echo  $result = ($var1==$var2)? "value equal":"value not equal";

  ?>

 

Output 1:

 

value equal

 

Explanation:

 

In the above code $var1 and $var2 both have string type values both are compared using equal operator (==) then output message is values are equal. If values are not equal then second statement is printed value are not equal.

 

Equal Operator (===)

 

Equal operator checks the both equality and type of the variables.

 

Code 2:

 

<?php

$var3="047";

$var4=047;

 echo  $result = ($var3===$var4)? "value equal":"value not equal";

 echo "<br/>";

  ?>

 

Output 2:

 

value not equal

 

Explanation:

 

In the above code $var3 and $var4 both have different type values one is string type and second is integer type  both are compared using equal operator(===)   then output message is values are not  equal. The reason is variables are not of same type. If first condition is true first message will be the output or second message will be the output.

 

Not equal Operator ( !=)

 

Code 3:

 

<?php

$var3=55;

$var4=047;

 echo  $result = ($var3!=$var4)? "value not equal":"value  equal";

 echo "<br/>";

  ?>

 

Output 3:

 

value not equal

 

Explanation:

 

In the above code $var3 and $var4 both have same type values integer type. Both are compared using Not equal operator (!=) and hence output message will be values are not  equal. If first condition is true then first message will be the output otherwise second message will be the output.

 

Less than (<)

 

Code 4:

 

<?php

$var3=78;

$var4=47;

 echo  $result = ($var4<$var3)? "Less value":"Not less value";

 echo "<br/>";

  ?>

 

Output 4:

 

Less value

 

Explanation:

 

In the above code $var3 and $var4 both have same type values integer type. Both are compared using less operator (<).If the $var4 is less than $var3 then the message less value will be printed otherwise message not less than will be printed. If first condition is true then first message will be the output otherwise second message will be the output.

 

Greater than(<)

 

Code 5:

 

<?php

$var3=86;

$var4=97;

 echo  $result = ($var4>$var3)? "greater ":"not greater";

 echo "<br/>";

  ?>

 

Output 5:

 

greater

 

Explanation:

 

In the above code $var3 and $var4 both have same type values integer type. Both are compared using greater operator (>).If the $var4 is greater than $var3 then first message will be printed greater if not than not greater message will be printed. If condition is true then first message will be the output otherwise second message will be the output.

 

Less than or equal to operator (=<)

 

Code 7:

 

<?php

$var3=86;

$var4=97;

 echo  $result = ($var3<=$var4)? "less than or equal ":"not less than..";

 echo "<br/>";

  ?>

 

Output 7:

 

less than or equal

 

Explanation:

 

In the above code $var3 and $var4 both have same type values integer type. Both are compared using less than or equal operator (<=).If the $var3 is less than or equal to $var4 then first message will be printed less than or equal, if not then not less than message will be printed. If condition is true first message will be the output otherwise second message will be the output.

 

Greater than or equal to operator (=<)

 

Code 8:

 

<?php

$var3=97;

$var4=56;

 echo  $result = ($var3>=$var4)? "greater than or equal ":"not greater than or equal";

 echo "<br/>";

  ?>

 

Output 8: 

 

greater than or equal

 

Explanation:

 

In the above code $var3 and $var4 both have same type values integer type. Both are compared using greater than or equal operator (>=).If the $var3 is greater than or equal to $var4 then first message will be printed greater than or equal, if not then not greater than or equal will be printed. If condition is true then first message will be the output otherwise second message will be the output.

 

Add A Comment: