Jump to content

probably easy question about operators


hoogie

Recommended Posts

While going through an instructional book, I ran across some code that I didn't immediately understand.  I've stripped it down to the relevant bits:

 

 

function example($object_a, $object_b) {

  $compare = $object_a->id == $object_b->id;

  return another_function('argument 1') && $compare || another_function('argument 2');

}

 

 

 

My first confusion was this line:

$compare = $object_a->id == $object_b->id;

Does this set $compare to TRUE if the ids match and FALSE if they do not?

 

And the second confusion:

return another_function('argument 1') && $compare || another_function('argument 2');

What is returned if the ids match?  What is returned if they are different? 

 

When I try testing this out on my machine, it returns both functions whether or not the ids match, but I know that's not what's supposed to happen.  Can anyone break this down for me?

 

Thanks.

Link to comment
Share on other sites

1. Yes.

2. PHP short circuits evaluation, so as soon as it is able to determine the result it will stop executing.  For logical operators, it is evaluated going left to right.

2a.  In order for another_function(argument 2) not be evaluated, the combination of another_function() && $compare must evaluate to true, but I'm assuming that is never the case, if another_function(argument 2) is always being run.

Link to comment
Share on other sites

2a.  In order for another_function(argument 2) not be evaluated, the combination of another_function() && $compare must evaluate to true, but I'm assuming that is never the case, if another_function(argument 2) is always being run.

 

Ok, this makes sense.  I checked, and that function is NOT returning a boolean value, so that must be the problem.  Thanks for the explanation!

Link to comment
Share on other sites

One last question.  Is this considered 'good' code?:

return another_function('argument 1') && $compare || another_function('argument 2');

 

It seems unnecessarily obfuscated to me, but maybe that's because I'm just learning about it now.

 

If your employee wrote something like that, would you consider it good code because it's simple, or would you want something more readable?

Link to comment
Share on other sites

Because php typecasts, even when you return strings or numbers they will get typecast to something that works.  However, depending on this behavior is a bad idea, and the results of a typecast boolean is frequently not what you would expect it to be, so no that's not good maintainable code.

 

When people write books they often will put in extreme examples to illustrate the point they're trying to make.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.