Req #46239 [Opn->Sus]: wish: compound logical operators (nand, xor, etc)
Edit report at https://bugs.php.net/bug.php?id=46239&edit=1
ID: 46239
Updated by: cmb@php.net
Reported by: php at richardneill dot org
Summary: wish: compound logical operators (nand, xor, etc)
-Status: Open
+Status: Suspended
Type: Feature/Change Request
-Package: Feature/Change Request
+Package: *General Issues
PHP Version: 6CVS-2008-10-06 (CVS)
Block user comment: N
Private report: N
New Comment:
Adding a bunch of functions to the core or ext/standard which can obviously be
implemented in userland should require the RFC process. Anybody is free to
propose a respective RFC (see <https://wiki.php.net/rfc/howto> on how to). In
the meantime I'm suspending this ticket.
Note that a particular controversial point in this case would be whether the
operations should short-circuit or not (the latter being unusual for logical
operators; the former being unusual for functions).
Previous Comments:
------------------------------------------------------------------------
[2008-10-06 06:20:30] php at richardneill dot org
Description:
------------
This is a request for a new set of functions, namely the more complex logical operations, NAND, NOR,
XOR, XNOR etc. I suggest implementing them as functions, rather than as operators, because they can
take a variable number of arguments. These would also be distinct from the bitwise operators.
Of course these are pretty trivial to implement oneself as and when needed, but it would make a
small increment of improvement to PHP ;-)
The other advantage is in code-readability. I think that example1 is more readable than example2:
$example1 = nand ($condition_1, $condition_2, $condition_3, $condition_4)
$example2 = (! ($condition_1 and $condition_2 and $condition3 and $condition_4)
Thanks for your time and consideration - Richard
Reproduce code:
---------------
A possible implementation:
function nand($a,$b,$c,....){
#cast everything to boolean
return !($a and $b and $c ...);
}
function nor($a,$b,$c,...){
#cast to boolean
return !($a or $b or $c or ...);
}
function new_and($a,$b,$c,...){
#note: this makes the syntax much clearer and shorter if
#there are very many inputs.
return ($a and $b and $c...);
}
#similarly, new_or()
function xor($a,$b,$c,...){
$count=0;
if ($a){ $count++ ;}
if ($b){ $count++ ;}
if ($c){ $count++ ;}
...
return ($count == 1);
}
function xnor($a,$b,$c,...){
$count=0;
if ($a){ $count++ ;}
if ($b){ $count++ ;}
if ($c){ $count++ ;}
...
return ($count != 1);
}
function majority($a,$b,$c,...){
$count=0;
if ($a){ $count++ ;}
if ($b){ $count++ ;}
if ($c){ $count++ ;}
..
return ($count > $number_of_args/2);
}
#and any others I've missed out.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=46239&edit=1
Thread (2 messages)