PHP expression a || b = c

I really do not understand the following expression: what it does and how it works.

a || b = c

I assume it checks to see if true, and if not, does it run b = c?

Application example:

$id || $data['created'] = $now
+4
source share
6 answers

This is a shorthand for:

($id == true) || (($data['created'] = $now) == true)

Factoring in short circuit logic and the fact that the result of the expression itself is ignored:

if (!$id) {
    $data['created'] = $now;
}

See also: Logical operators

+9
source

In my understanding, this means:

a OR the ability to give b the value of c.

or in your case

$id OR opportunity to give $data['created']meaning$now

+1
source

. , , . , a || b b, a ( true false, ) , a && b b, a false.

:

a true, b=c, c b.

a false, , c b, .

0

. , : ? a: b = c

$a=0;
$c=1;
($a) ? $a : $b=$c;
echo $b;

$data['created'] = ($id) ? $id : $now;

|| OR . ?: :

switch(true) {
    case $a : $a; break;
    default : $b=$c; break;
}

IF ELSEIF ELSE. " ", " " "" , IF.

0

, .

manual, :

, , . PHP ( ) , , , , , , PHP .

# 2 Undefined

$a = 1;
echo $a + $a++; // may print either 2 or 3

$i = 1;
$array[$i] = $i++; // may set either index 1 or 2

:

= , , PHP , : if (! $a = foo()), foo() $a,

0

|| means OR, so == true OR (b = c) == true. This last one is strange because you should not use '=' in an if statement.

Working example:

$a = false;
$b = 19;

if ($a == true || $b == 19)
{
    // continue here.
}

Although $ a is false, the if statement looks at the second part, and this statement is true. Thus, the statement may continue.

-2
source

Source: https://habr.com/ru/post/1545797/


All Articles