What does the double not operator do in PHP?
For example:
return !! $row;
What would the code above do?
What does the double not operator do in PHP?
For example:
return !! $row;
What would the code above do?
You could make functions that wrap the operators, or for simplicity just use the bc extension:
$operator = '+';
$operators = array(
'+' => 'bcadd',
'-' => 'bcsub',
'*' => 'bcmul',
'/' => 'bcdiv'
);
foreach($resultSet as $item){
$result = call_user_func($operators[$operator], $item[$this->orderField], 1);
echo $result;
}
There is no "better" but the more common one is ||
. They have different precedence and ||
would work like one would expect normally.
See also: Logical operators (the following example is taken from there):
// The result of the expression (false || true) is assigned to $e
// Acts like: ($e = (false || true))
$e = false || true;
// The constant false is assigned to $f and then true is ignored
// Acts like: (($f = false) or true)
$f = false or true;
It is an E4X operator.
From https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide:Processing_XML_with_E4X:
While the . operator accesses direct children of the given node, the .. operator accesses all children no matter how deeply nested:
They behave identically, to the point of producing identical byte code; they're equally efficient. That said, element not in list
is usually considered preferred. PEP8 doesn't have a specific recommendation on not ... in
vs. ... not in
, but it does for not ... is
vs. ... is not
, and it prefers the latter:
Use
is not
operator rather thannot ... is
. While both expressions are functionally identical, the former is more readable and preferred.
To show equivalence in performance, a quick byte code inspection:
>>> import dis
>>> dis.dis('not x in y')
1 0 LOAD_NAME 0 (x)
2 LOAD_NAME 1 (y)
4 COMPARE_OP 7 (not in)
6 RETURN_VALUE
>>> dis.dis('x not in y')
1 0 LOAD_NAME 0 (x)
2 LOAD_NAME 1 (y)
4 COMPARE_OP 7 (not in)
6 RETURN_VALUE
It's not the "double not operator", it's the not operator applied twice. The right
!
will result in a boolean, regardless of the operand. Then the left!
will negate that boolean.This means that for any true value (numbers other than zero, non-empty strings and arrays, etc.) you will get the boolean value
TRUE
, and for any false value (0, 0.0,NULL
, empty strings or empty arrays) you will get the boolean valueFALSE
.It is functionally equivalent to a cast to
boolean
: