php.net |  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
Votes:7
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
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: php at richardneill dot org
New email:
PHP Version: OS:

 

 [2008-10-06 06:20 UTC] 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.



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-09-26 21:24 UTC] cmb@php.net
-Status: Open +Status: Suspended -Package: Feature/Change Request +Package: *General Issues
 [2017-09-26 21:24 UTC] cmb@php.net
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).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 11:01:30 2024 UTC