PHP : Logical Operators
Description
The standard logical operators and, or, not, and xor are supported by PHP. Logical operators first convert their operands to boolean values and then perform the respective comparison.
Here is the list of logical operators :
Operator | Name | Example | Result |
---|---|---|---|
&& | and | $x && $y | is true if both $x and $y are true. |
|| | or | $x || $y | is true if either $x or $y is true. |
xor | xor | $x xor $y | is true if either $x or $y are true, but not both. |
! | not | !$x | is true if $x is not true. |
and | and | $x and $y | is true if both $x and $y are true. |
or | or | $x or $y | is true if either $x or $y is true. |
PHP logical && operator
This above pictorial helps you to understand the concept of LOGICAL AND operation with an analogy of taps and water.
In case-1 of the picture, both of the taps are closed, so the water is not flowing down. Which explains that if both of conditions are FALSE or 0, the return is FALSE or 0.
In case-2 of the picture, one of the taps are closed, even then, the water is not flowing down. Which explains that even if any of conditions are FALSE or 0, the return is FALSE or 0.
case-3 of the picture resembles CASE -2.
In case-4 of the picture, both of the taps are open, so the water is flowing down. Which explains that if both of conditions are TRUE or 1, the return is TRUE or 1.
So we can conclude that if and only if, both of the conditions are TRUE or 1, LOGICAL AND operations returns TRUE or 1.
PHP logical || operator
The above pictorial helps you to understand the concept of LOGICAL OR operation with an analogy of taps and water.
In case-1 of the picture, both of the taps are closed, so the water is not flowing down. Which explains that if both of conditions are FALSE or 0, the return is FALSE or 0.
In case-2 of the picture, one of the taps are closed, and we can see that the water is flowing down. Which explains that if any of conditions are TRUE or 1, the return is TRUE or 1.
case-3 of the picture resembles CASE -2.
In case-4 of the picture, both of the taps are open, so the water is flowing down. Which explains that if both of conditions are TRUE or 1, the return is TRUE or 1.
So we can conclude that in LOGICAL OR operation if any of the conditions are true, the output is TRUE or 1.
Example :
<?php
$a = true && false;
var_dump($a);
$b = false && true;
var_dump($b);
$c = true && true;
var_dump($c);
$d = false && false;
var_dump($d);
$a = true || false;
var_dump($a);
$b = false || true;
var_dump($b);
$c = true || true;
var_dump($c);
$d = false || false;
var_dump($d);
?>
Output :
bool(false) bool(false) bool(true) bool(false) bool(true) bool(true) bool(true) bool(false)
View the example in the browser
Previous: Comparison Operators
Next: Assignment Operators
PHP: Tips of the Day
How to Sort Multi-dimensional Array by Value?
Try a usort, If you are still on PHP 5.2 or earlier, you'll have to define a sorting function first:
Example:
function sortByOrder($a, $b) { return $a['order'] - $b['order']; } usort($myArray, 'sortByOrder');
Starting in PHP 5.3, you can use an anonymous function:
usort($myArray, function($a, $b) { return $a['order'] - $b['order']; });
And finally with PHP 7 you can use the spaceship operator:
usort($myArray, function($a, $b) { return $a['order'] <=> $b['order']; });
To extend this to multi-dimensional sorting, reference the second/third sorting elements if the first is zero - best explained below. You can also use this for sorting on sub-elements.
usort($myArray, function($a, $b) { $retval = $a['order'] <=> $b['order']; if ($retval == 0) { $retval = $a['suborder'] <=> $b['suborder']; if ($retval == 0) { $retval = $a['details']['subsuborder'] <=> $b['details']['subsuborder']; } } return $retval; });
If you need to retain key associations, use uasort() - see comparison of array sorting functions in the manual
Ref : https://bit.ly/3i77vCC
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework