Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Weird Syntax - What is it called?

I've been following a video series about a PHP framework and I've come across a weird construct. I'd just like to find out what it's called so I can read up on it some more. In the video, the speaker gives this code

public function edit($id = NULL)
{
   $id == NULL || $this->data['user'] = $this->user_m->get($id);
   ...
}

He says, if the caller passes no id, then the $id will set to null, if an $id is passed, the user will be retrieved from the database and saved in the data array.

Thing is, this is a conditional statement, but it's not inside an if or while or something like that. I've never seen that before. I've searched for inline or, conditional outside a loop/if statement, weird syntax with no success. Like I said, I'd like to read up more about it and find out whether it's a general PHP feature or framework-specific.

Thanks.

like image 758
Sethmo011 Avatar asked Jul 04 '15 08:07

Sethmo011


2 Answers

The sytnax is called "short-circuit evaluation" and is an optimization technique used in most programming languages. The right expression of an || is only evaluated if the left expression is false, because -otherwise- the result does not depend on the value of the right expression (as the right expression of an && is only evaluated if the left one is true).

You could rewrite your example to ($id == NULL) || ($this->data['user'] = $this->user_m->get($id)); to make it more clear. The assignment expression is only evaluated, if $id is not NULL.

However, this line of code is a perfect example for code that is hard to maintain, because you need to spend way to much time thinking about what it does... :)

like image 174
Sebastian P. Avatar answered Oct 18 '22 02:10

Sebastian P.


To create the proper answer to this question, we must start by saying that there probably (I would say probably, because I'm still not 100% sure about that) an error in your function syntax:

public function edit($id == NULL)

should be:

public function edit($id = NULL)

Which would totally makes sense with the rest of the code, because it will set the local variable ID to NULL if it is not set, else it will set it.

That said, the rest, as said in the comments, is just an OR logical operation (Fine, see that in whatever way you want, I'm used to interprete it as a ternary operator, because it's slighty easier if someone doesn't know how it works), I just feel that I should point out that differently from many other languages, in PHP you can directly evaluate a variable in the second or third or whatever parameter of logical operations.

In a nutshell, your operations:

$id == NULL || $this->data['user'] = $this->user_m->get($id);

could be also written as:

if ($id !== null) {
   $this->data['user'] = $this->user_m->get($id);
}

Which is readable, of course, else, you could also do:

$id !== NULL && $this->data['user'] = $this->user_m->get($id);

Or even:

$this->data['user'] = $id !== NULL ? $this->user_m->get($id) : $this->data['user'];

The basic ternary operator is:

condition ? iftrue : iffalse

The "variation" you have is rather a sort of "use" of logical operations, called short-circuit evaluation, in fact, the OR logical operator evaluates the right hand operator ONLY IF THE LEFT HAND ONE IS FALSEY, hence:

$id == NULL || $this->data['user'] = $this->user_m->get($id);

$id == NULL <-- left hand operator

$this->data['user'] = $this->user_m->get($id) <-- right hand operator

taking what we said above, if $id == NULL is falsey, (that means, if $id is not null), then execute the right hand operator, so set $this->data['user'] to $this->user_m->get($id), otherwise do nothing at all.

like image 39
briosheje Avatar answered Oct 18 '22 00:10

briosheje