unknown os platform
Free Beginner PHP Tutorials - Comparison operators | Whats-online.info
whats-online-info

Free Beginner PHP Tutorials - Comparison operators


Free
2016:09:26 / Science and Tutorials

Comparison operators in PHP allows to compare values. See example:-





Comparison operators - PHP


The PHP comparison operators as the name suggest are used to compare two values either a number or a string.

We will use var_dump() to dump information about a variable.

Equal comparison operator (==)


Compares two values and returns true if the two values are equal.

Example:

<?php   $x=100;   $y="100";  var_dump($x == $y); ?>

Output: bool(true);
Returns true because the values are equal.


Identical comparison operator (===)


Compares two values and returns true if the two values are equal and are of the same data type.

Example:

<?php     $x=100;   $y="100";  var_dump($x === $y); ?>

Output: bool(false);
Returns false because the values are not identical.
$x is an integer value while $y is a string value.


Identical comparison operator (!==) 

Compares two values and returns true if the two values are not equal and are not of the same data type.

Example:

<?php      $x=100;   $y="100";  var_dump($x !== $y); ?>

Output: bool(true);
Returns true because the values are not identical.
$x is an integer value while $y is a string value.


Not equal comparison operator (!=)

Compares two values and returns true if the two values are not equal.

Example:

<?php    $x=80;   $y="100";  var_dump($x != $y); ?>

Output: bool(true);
Returns true because the values are not equal.


Not equal comparison operator (<>)

Compares two values and returns true if the two values are not equal.

Example:

<?php     $x=80;   $y=100;  var_dump($x <> $y); ?>

Output: bool(true);
Returns true because the two values are not equal.

Greater than comparison operator (>)

Compares two values and returns true if the left value is greater than the right value.

Example:

<?php     $x=100;   $y=80;  var_dump($x > $y); ?>

Output: bool(true);

Returns true because the $x value is greater than  $y value.

Less than comparison operator (<) 

Compares two values and returns true if the left value is less than the right value.

Example:

<?php      $x=80;   $y=100;  var_dump($x < $y); ?>

Output: bool(true);
Returns true because the $x value is less than  $y value.

Greater than or equal to comparison operator (>=)

Compares two values and returns true if the left value is Greater than or equal to the right value.

Example:

<?php    //expression 1    $x=100;   $y=100;  var_dump($x >= $y); //expression 2    $x=120;   $y=100;  var_dump($x >= $y); ?> 

Output expression 1: bool(true);
Returns true because the two values are equal.

Output expression 2: bool(true);
Returns true because $x value is greater than $y value.

Less than or equal to comparison operator (<=) 

Compares two values and returns true if the left value is less than or equal to the right value.

Example:
<?php //expression 1 $x=100; $y=100; var_dump($x <= $y); //expression 2 $x=80; $y=100; var_dump($x <= $y); ?>

Output expression 1: bool(true);
Returns true because the two values are equal.

Output expression 2: bool(true);
Returns true because $x value is less than $y value.












You may also like:


Leave a comment












Subscribe to get replies direct to your mailbox

Submit comment