Asked  7 Months ago    Answers:  5   Viewed   33 times

What does the double not operator do in PHP?

For example:

return !! $row;

What would the code above do?

 Answers

56

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 value FALSE.

It is functionally equivalent to a cast to boolean:

return (bool)$row;
Wednesday, March 31, 2021
 
Hugo
answered 7 Months ago
88

It evaluates to the left operand if the left operand is truthy, and the right operand otherwise.

In pseudocode,

foo = bar ?: baz;

roughly resolves to

foo = bar ? bar : baz;

or

if (bar) {
    foo = bar;
} else {
    foo = baz;
}

with the difference that bar will only be evaluated once.

You can also use this to do a "self-check" of foo as demonstrated in the code example you posted:

foo = foo ?: bar;

This will assign bar to foo if foo is null or falsey, else it will leave foo unchanged.

Some more examples:

<?php
    var_dump(5 ?: 0); // 5
    var_dump(false ?: 0); // 0
    var_dump(null ?: 'foo'); // 'foo'
    var_dump(true ?: 123); // true
    var_dump('rock' ?: 'roll'); // 'rock'
?>

By the way, it's called the Elvis operator.

Elvis operator

Wednesday, March 31, 2021
 
van_folmert
answered 7 Months ago
67

You can see examples about this behaviour in Logical Operators

Also you can read artical about Short-circuit evaluation

The short-circuit expression x Sand y (using Sand to denote the short-circuit variety) is equivalent to the conditional expression if x then y else false; the expression x Sor y is equivalent to if x then true else y.

In php.

return x() and y();

equal to

if (x())
  return (bool)y();
else
  return false;

return x() or y();

equal to

if (x())
  return true;
else
  return (bool)y();

So, deal is not just in precedence.

Wednesday, March 31, 2021
 
iftheshoefritz
answered 7 Months ago
20

In fact, Zip is included by default. It is a bug that the line is still included.

Saturday, May 29, 2021
 
joostvandriel
answered 5 Months ago
51

== means equality, so the conditional reads as:

If pre-incremented $x equals 10, echo $x

Single = is assignment, where a variable is set to contain a value:

$word = 'hello';
$number = 5;
// etc.

echo "I said $word $number times!";

Regarding the increment opperators:

You'll see things like ++$x and $i-- as you learn PHP (and/or other languages). These are increment/decrement operators. Where they're positioned in relation to the variable they're operating on is important.

If they're placed before the variable, like ++$x, it's a pre-increment/decrement. This means the operation is performed before anything else can be done to the variable. If it's placed after, like $x++, it's a post-increment/decrement, and it means that the operation is performed afterward.

It's easiest to see in an example script:

$x = 5;

echo ++$x; // 6
echo $x++; // ALSO 6
echo $x; // NOW 7
Saturday, August 7, 2021
 
EurekA
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :