开发者

Can someone explain this line of code please? (Logic & Assignment operators)

I've seen the following lines of code and I know what they do, but I don't know how th开发者_JS百科e second line works (and hence how to apply it to another situation).

$user = User::model()->findByPk(123);
empty($user->profile) and $user->profile = new Profile();

The code tries to look up the User from the database, and if there isn't a profile, creates a new for use later on.

I have also seen code before that goes something like the following:

$variable1 = $variable2 = $variable3;

It did something a bit more complex than simple assigning three things to be the same, but I'm finding it impossible to search for this type of thing to find out any information about it, let alone find the original code that I came across. I think it originally had an 'and' in there somewhere. Does anyone know how to search for code that has more than one equals sign in it that wasn't just an if statement?

Sorry for the two questions in one (and vague at that) and the terrible title (I'll fix it up when I know what the names are, if it's anything like a tenary statement)).


Using Logical Operators to Skip Code: As php evaluates a line with the AND operator, if the fist part is false, the second part is not evaluated since it would not change the result.

So in this case, if empty() returns true, then php evaluates the right side. If empty() returns false, no more evaluation is done and the profile is not effected.

The php manual logical operators page has some illustrations of this.

Multiple Assignment Operators: The assignment operator assigns the right expression to the variable on the left.

$variable1 = $variable2 = $variable3;

In this case $variable2 is set to the value of $variable3 then $variable1 is set to the value of $variable2. The php manual assignment operators page covers this.


empty($user->profile) and $user->profile = new Profile();

in and statement, first element is evaluated first; if it's true, then second statement is evaluated, simple assignment should always evaluate to true, I presume. if the first element was false, second element is not evaluated.

You can read more about operator precedence in php docs.


It's basically the same as

if (empty($user->profile))
    $user->profile = new Profile();

Weird syntax indeed...


What you're seeing is used in many languages. Here is an article for using it in JavaScript... but it explains the concept well.

http://css.dzone.com/articles/guard-and-default-operators-ja


This is a fairly bizarre way to write this. With a PHP expression, if the first part evaluates false, the expression will stop rendering.

Like if I write:

if (x == 5 and y == 2)

that will test if x==5, then if it does, it will test if y==2. If x != 5, it will never test the y == 2. So the code above is using that fact to test whether $user->profile is empty. Then if it is, it runs the next part of the expression, which is the assignment of $user->profile = new Profile(); Basically the same as:

if (empty($user->profile))
    $user->profile = new Profile();

or

empty($user->profile) ? $user->profile = new Profile();

As far as your second question, the operator = is just an assignment, so it means that the value of $variable1 will be set to the value of $variable2, which will be set to the value of $variable3.

PHP does some very nice things with expressions, which are really helpful to learn. Check out this link for more info:

http://us.php.net/manual/en/language.expressions.php


$variable1 = $variable2 = $variable3;

Assignment (via the equal sign) in PHP has right to left precedence. Every expression in PHP has a return value. So the return value of the expression $variable3 is assigned to $variable2. Then the reurn value of the expression $variable2 = $variable3 is assigned to $variable1.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜