Share on Google+Share on Google+

PHP Logical Operator

Advertisement
In this tutorial we will study about logical operator, logical operator are used when we need to check the equality between values. Examples in this tutorial will make it more clear.

Logical Operators:

This logical operators  are used when we need to check more than one condition, like if more than one condition has to be true then we need to use and (&&) and suppose if any one condition could be true then we need to use or (||) operator. There are many more operators are available and each one has its own importance.

Only reason for keeping two variations of "and" and "or" is that they operate at different precedences.

Example:

<?php

$a=12;

$b=0;

$c=22;

$d=22;

echo "\$a && \$b =";

var_dump ($a && $b);

echo "<br/>";

echo "\$c && \$d =";

var_dump ($c && $d);

echo "<br/>";

echo "\$a || \$b =";

var_dump ($a || $b);

echo "<br/>";

echo "\$c || \$d =";

var_dump ($c || $d);

echo "<br/>";

?>

Output:

$a && $b =bool(false)
$c && $d =bool(true)
$a || $b =bool(true)
$c || $d =bool(true)

Example:

<?php

$a=0;

$b=45;

var_dump ($a and $b);

echo "<br/>";

$a=10;

$b=45;

var_dump ($a and $b);

echo "<br/>";

$a=NULL;

$b=45;

var_dump ($a and $b);

echo "<br/>";

$a=10;

$b=45;

unset($b);

var_dump ($a and $b);

echo "<br/>";

$a=0;

$b=45;

var_dump ($a or $b);

echo "<br/>";

$a=10;

$b=45;

var_dump ($a or $b);

echo "<br/>";

$a=NULL;

$b=45;

var_dump ($a or $b);

echo "<br/>";

$a=10;

$b=45;

unset($b);

var_dump ($a or $b);

echo "<br/>";

 

Output:

bool(false)
bool(true)
bool(false)
bool(false)
bool(true)
bool(true)
bool(true)
bool(true)

Advertisements

Advertisement

Posted on: March 20, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.