midnite Posted June 6, 2008 Share Posted June 6, 2008 let's see http://us2.php.net/manual/en/language.oper...tors.precedencethe assignment operator (=) is far below the not operator (!). But why we can work like this: function func () { return false;}$var = false;if (!$var = func()) { echo 'it is true';} and it act as if: function func () { return false;}$var = false;if (!($var = func())) { echo 'it is true';} Link to comment Share on other sites More sharing options...
Synook Posted June 6, 2008 Share Posted June 6, 2008 Because = is assignment, not comparison. This statement "if (!$var = func())" is actually just saying "if (!func() && $var = func())", same as the second one. Link to comment Share on other sites More sharing options...
Jesdisciple Posted June 6, 2008 Share Posted June 6, 2008 Kind of... But I think it would be better to compare it to this, because func() only gets called once and "!func() && $var = func()" means that func() must be true and false: $var = func();if(!$var){ echo 'it is true';} Link to comment Share on other sites More sharing options...
midnite Posted June 6, 2008 Author Share Posted June 6, 2008 Thanks you guys!i understand what you are saying. And also i have been using if (!$var = func()) quite often and it always work as what i wish: if (! ($var = func()) ). Yet when i accidentally came across the precedence table, i was confused.In that table, isn't the assignment operator sits at the fifth last and the NOT operator is high above at the sixth top? According to their precedences, isn't if (!$var = func()) should be interrupted as if ( (!$var) = func())? Although it is not logical and not syntactically correct, it is what the precedence table means.Is it because of the associativity? i never understand this term.Thanks =) Link to comment Share on other sites More sharing options...
Jesdisciple Posted June 6, 2008 Share Posted June 6, 2008 Associativity indicates where the parser will look for a value to apply the operator to. So the (first) value should be to the right of a ! or the left of a *. But I don't think that has anything to do with !$var = func(). Operators on the same line have equal precedence, in which case their associativity decides which order to evaluate them in.I agree that this is a very confusing construct given the precedence, but the key to it is that true = func() is nonsense. I suppose a strict interpretation of precedence would dictate that the assignment must be enclosed in parentheses, but the loose interpretation saves us four key-presses so I won't complain. Link to comment Share on other sites More sharing options...
Synook Posted June 8, 2008 Share Posted June 8, 2008 Think about while ($row = mysql_fetch_array($result)) What is that testing? Link to comment Share on other sites More sharing options...
midnite Posted June 8, 2008 Author Share Posted June 8, 2008 see this: # cat sth.c#include <stdio.h>int main () { int a = 3; if (!a = 0) printf ("in: %d\n", a); printf ("out: %d\n", a); return 0;} it is not valid in c if the parentheses are omitted: # gcc sth.csth.c: In function `main':sth.c:5: error: invalid lvalue in assignment Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.