Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP expression a || b = c

Tags:

php

I don't really understand the following expression, what it does and how it works.

a || b = c

I guess it check if a is true, and if it's not, it run b = c?

Exemple of application:

$id || $data['created'] = $now
like image 670
Chuck Avatar asked Feb 13 '23 16:02

Chuck


2 Answers

It's short 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

like image 119
Ja͢ck Avatar answered Feb 15 '23 11:02

Ja͢ck


In my understanding it means:

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

or in your case

$id OR the posibility to give $data['created'] the value of $now

like image 27
Friedrich Avatar answered Feb 15 '23 11:02

Friedrich