Does PHP support conjunction and disjunction natively?
PHP supports short-circuit evaluation, a little different from JavaScript's conjunction. We often see the example (even if it isn't good practice) of using short-circuit evaluation to test the result of a MySQL query in PHP:
// mysql_query() returns false, so the OR condition (die()) is executed.
$result = mysql_query("some faulty query") || die("Error");
Note that short-circuit evaluation works when in PHP when there is an expression to be evaluated on either side of the boolean operator, which would produce a return value. It then executes the right side only if the left side is false. This is different from JavaScript:
Simply doing:
$a || $b
would return a boolean value TRUE
or FALSE
if either is truthy or both are falsy. It would NOT return the value of $b
if $a
was falsy:
$a = FALSE;
$b = "I'm b";
echo $a || $b;
// Prints "1", not "I'm b"
So to answer the question, PHP will do a boolean comparison of the two values and return the result. It will not return the first truthy value of the two.
More idiomatically in PHP (if there is such a thing as idiomatic PHP) would be to use a ternary operation:
$c = $a ? $a : $b;
// PHP 5.3 and later supports
$c = $a ?: $b;
echo $a ?: $b;
// "I'm b"
Update for PHP 7
PHP 7 introduces the ??
null coalescing operator which can act as a closer approximation to conjunction. It's especially helpful because it doesn't require you to check isset()
on the left operand's array keys.
$a = null;
$b = 123;
$c = $a ?? $b;
// $c is 123;
php: I need the OPPOSITE of this please...?
What you're looking for is DeMorgan's law:
The negation of a conjunction is the disjunction of the negations.
The negation of a disjunction is the conjunction of the negations.
Try:
if ( stristr($fruit, 'apples') === FALSE && stristr($fruit,'oranges') === FALSE && stristr($fruit,'bananas') === FALSE) {
// some code
}
Since stristr
only returns a string or FALSE, this code can be simplifed to:
if ( !stristr($fruit, 'apples') && !stristr($fruit,'oranges') && !stristr($fruit,'bananas')) {
// some code
}
Execute all functions in a logical operation
According to this comment on the PHP manual
Evaluation of logical expressions is stopped as soon as the result is known.
If you don't want this, you can replace the and-operator by min() and the or-operator by max().
So this code may be of use to you, as you can see all functions are called in each case and, yes, you can use this with more than two functions:-
function a()
{
echo "function a<br/>\n";
return true;
}
function b()
{
echo "function b<br/>\n";
return false;
}
function c()
{
echo "function c<br/>\n";
return false;
}
echo min(a(), b(), c());
echo "<br/>";
echo max(a(), b(), c());
echo "<br/>";
Output:-
function a
function b
function c
(false)
function a
function b
function c
(true)
Demo
MySQL query with multiple OR conditions and a single AND condition
Use parentheses
SELECT *
FROM `nam_order_items`
WHERE (item_status = 'Glass Bending - Andy'
OR item_status = 'Glass Bending - Bren'
OR item_status = 'Glass Bending - Gary'
OR item_status = 'Glass Bending - James'
OR item_status = 'Glass Bending - Oscar')
AND (`backing_cut` = 0 OR `backing_cut` = 1 OR `backing_bent` = 0 OR `backing_bent` = 1)
you could also use value in array as
SELECT *
FROM `nam_order_items`
WHERE `item_status` IN ('Glass Bending - Andy','Glass Bending - Bren','Glass Bending - Gary','Glass Bending - James','Glass Bending - Oscar')
AND (`backing_cut` = 0 OR `backing_cut` = 1 OR `backing_bent` = 0 OR `backing_bent` = 1)
you could then build your array using php and simply do something like:
$options = array('Glass Bending - Andy','Glass Bending - Bren','Glass Bending - Gary','Glass Bending - James','Glass Bending - Oscar');
$sql = "SELECT * FROM `nam_order_items` WHERE `item_status` IN ({implode(',', $options}) AND (`backing_cut` = 0 OR `backing_cut` = 1 OR `backing_bent` = 0 OR `backing_bent` = 1)";
Edited to add option for backing cut = 1 or 0
Related Topics
Make Text Between Asterisks Bold
Regular Expression for Checking Website Url
Do I Need to Escape My Variables If I Use MySQLi Prepared Statements
How to Convert an Iso8601 Date to Another Format in PHP
PHP Variable Variables with Array Key
Count of Duplicate Elements in an Array in PHP
Htaccess Rule to Parse PHP in HTML Files Not Working
How to Use MySQLi_Fetch_Object with a Prepared Statement
Pdo Returning Error "Could Not Find Driver" with a Known Working Dsn
Phpstorm 2020.2 - PHP Built-In Functions Are Not Recognized
Ssl Error Can Not Change to Tls
Extending Session Timeout in PHP via the .Htaccess
MySQL Select a Piece of a String and Order by That Piece
What Is the Fastest Way to Find the Occurrence of a String in Another String
Why Does (0 == 'Hello') Return True in PHP