PHP - 不是运算符,还是其他任何别名?

if(!($whatever && what()) do_stuff...

Can this be replaced with something more intuitive like:

if(not($whatever && what()) do_stuff...

?

function not($OriginalCheck)
{
    return !$OriginalCheck;
}

function is($OriginalCheck)
{
    return !!$OriginalCheck;
}

should do exactly that :)

There are several ways to write checks:

  • if(!($whatever && what()) do_stuff...
  • if(!$whatever || !what()) do_stuff...
  • if(($whatever && what()) === false) do_stuff...
  • if((!$whatever || !what()) === true) do_stuff...
  • if($whatever === false || what() === false) === true) do_stuff...

all these ways are intuitive and known through out the programming world.

One option is to make the boolean expression more explicit:

if(($whatever && what()) == false) // do_stuff...

Or alternatively, by implementing a custom not():

function not($expr) {
    return $expr == false;
}

if(not($whatever && what())) // do_stuff...

No it can't. See http://www.php.net/manual/en/language.operators.logical.php for the language reference about logical operators, and navigate to find other aliases.

Note however that the precedence of && and || is not the same as and and or.

There is no alternative !, but it could be written less intuitive:

if ($whatever - 1) {
}

Should the question cause be that ! is too easy to overlook, but not more visible; then another alternative notation would be:

if (!!! $whatever) {

If this still looks to simple, just use:

if (~$whatever & 1) {

Binary operations always look professional ;)