When should we use the php low precedence "or" operator instead of || operator?
Operator precedence in php - Stack Overflow
php - Operator precedence: multiple expressions with && and || - Stack Overflow
Precedence of cast operators in php - Stack Overflow
Videos
The book says that in below case, we can only use the or operator and not || operator, but I don't understand why:
mysql_select_db($database) or die("Unable to select database");Can someone please explain why you can't use || in above example?
&& depends of the evaluation of the right expression when left one is true, || doesn't. You could rewrite it to:
if(
$users == 'all' ||
($_POST['user'] == 1 && $users == 'admins') ||
($_POST['user'] == 0 && $users == 'mods')
)
And it'll be the same.
With no parenthesis, PHP will evaluate each expression from left to right, using PHP's operator precedence along the way. However, as in any logical check, throwing AND into the mix can make things confusing and a lot harder to read.
As your link https://www.php.net/manual/en/language.operators.precedence.php already explains, the table is ordered by precedence.
First, you've got in line 3 the type cast (https://www.php.net/manual/en/language.types.type-juggling.php#language.types.typecasting), than the 6th line (arithmetic operators).
Your code is equivalent to
Copy$x = ((float)'100') / 100;
or
Copy$y = (float)'100';
$x = $y / 100;
respectively.
According to the table you linked to, type casting has higher precedence (row 3) than multiplication and division operators (row 6). So it's treated as
Copy$x = ((float)'100') / 100;
It first casts '100' to float, then performs the division.
Hi everyone,
According to the php documentation (https://www.php.net/manual/en/language.operators.precedence.php) the associativity of the null coalescing operator is right. Is that correct?
<?php
$foo = null;$bar = null;$baz = 1;$qux = 2;
echo $foo ?? $bar ?? $baz ?? $qux; // outputs 1
?>
In this example it is solved from left to right, isn't it? Is the documentation incorrect or do I not understand associativity? Please help :-)