|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #46239 wish: compound logical operators (nand, xor, etc)
Submitted: 2008-10-06 06:20 UTC Modified: 2017-09-26 21:24 UTC
Avg. Score:4.4 ± 1.4
Reproduced:6 of 7 (85.7%)
Same Version:1 (16.7%)
Same OS:2 (33.3%)
From: php at richardneill dot org Assigned:
Status: Suspended Package: *General Issues
PHP Version: 6CVS-2008-10-06 (CVS) OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2008-10-06 06:20 UTC] php at richardneill dot org
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,...){
    if ($a){ $count++ ;}
    if ($b){ $count++ ;}
    if ($c){ $count++ ;}
    return ($count == 1);

function xnor($a,$b,$c,...){
    if ($a){ $count++ ;}
    if ($b){ $count++ ;}
    if ($c){ $count++ ;}
    return ($count != 1);

function majority($a,$b,$c,...){
    if ($a){ $count++ ;}
    if ($b){ $count++ ;}
    if ($c){ $count++ ;}
    return ($count > $number_of_args/2);

#and any others I've missed out.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2017-09-26 21:24 UTC]
-Status: Open +Status: Suspended -Package: Feature/Change Request +Package: *General Issues
 [2017-09-26 21:24 UTC]
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 <> 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).
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sat Oct 16 11:03:33 2021 UTC