hoogie Posted May 11, 2011 Share Posted May 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236069-probably-easy-question-about-operators/ Share on other sites More sharing options...
fugix Posted May 11, 2011 Share Posted May 11, 2011 to asnwer your first question....it looks like it would return a boolean response yes Quote Link to comment https://forums.phpfreaks.com/topic/236069-probably-easy-question-about-operators/#findComment-1213597 Share on other sites More sharing options...
gizmola Posted May 11, 2011 Share Posted May 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236069-probably-easy-question-about-operators/#findComment-1213602 Share on other sites More sharing options...
hoogie Posted May 11, 2011 Author Share Posted May 11, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/236069-probably-easy-question-about-operators/#findComment-1213802 Share on other sites More sharing options...
hoogie Posted May 11, 2011 Author Share Posted May 11, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/236069-probably-easy-question-about-operators/#findComment-1213814 Share on other sites More sharing options...
gizmola Posted May 11, 2011 Share Posted May 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236069-probably-easy-question-about-operators/#findComment-1214050 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.