[PHP] anyone care to explain the logic behind this output ....
THIS CODE php -r ' $a = array(0, 1); $b = array(1 = 0, 0 = 1); var_dump($a $b); // true var_dump($a $b); // true var_dump($b $a); var_dump($b $a); echo \n\$a:\n; var_dump((bool)$a, (int)$a, (string)$a, intval($a), strval($a)); echo \n\$b:\n; var_dump((bool)$b, (int)$b, (string)$b, intval($b), strval($b)); ' OUTPUTS (on php5.0.4): bool(true) bool(true) bool(true) bool(true) $a: bool(true) int(1) string(5) Array $b: bool(true) int(1) string(5) Array WHICH MEANS THAT: one the one hand $a is greater than AND less than $b but on the other hand casting $a OR $b to either a boolean, integer or string results in the exact same value. ie: php -r ' $a = array(0, 1); $b = array(1 = 0, 0 = 1); var_dump( ((($a $b) === ($b $a)) === ((int)$a === (int)$b)) ); // WTF IT'S TRUE ' weird? I think so - but then again I'd never test that array $a is greater than array $b because this is meaningless to me (in what way is $a greater - how is this quantified, what 'rules' determine 'greatness' in this context?) PS - changing the $b array to something else (anything else as far as i can tell) causes the weirdness to not occur - which gives me the impression this could be a bug, anyone else get this impression? for instance try changing the second line of the code above to (merely switching the order or the defined array elements): $b = array(0 = 1, 1 = 0); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] anyone care to explain the logic behind this output ....
On 17/02/06, Jochem Maas [EMAIL PROTECTED] wrote: THIS CODE php -r ' $a = array(0, 1); $b = array(1 = 0, 0 = 1); var_dump($a $b); // true var_dump($a $b); // true var_dump($b $a); var_dump($b $a); [...] OUTPUTS (on php5.0.4): bool(true) bool(true) bool(true) bool(true) [...] weird? I think so - but then again I'd never test that array $a is greater than array $b because this is meaningless to me (in what way is $a greater - how is this quantified, what 'rules' determine 'greatness' in this context?) The rules are simple enough, and listed in the documentation here: http://www.php.net/manual/en/language.operators.comparison.php#AEN4390 But if you apply those comparison rules to your four expressions, you'd expect to see bool(true) bool(false) bool(true) bool(false) What you need to know to explain your results is that internally, PHP doesn't do a greater-than comparison, it converts them into less-than-or-equals by reversing the values. So your expressions become: $a $b $b = $a $b $a $a = $b Now if you apply the comparison rules to your arrays using those rewritten operations, you get true every time. Fun eh? -robin
Re: [PHP] anyone care to explain the logic behind this output ....
Robin Vickery wrote: On 17/02/06, Jochem Maas [EMAIL PROTECTED] wrote: THIS CODE php -r ' $a = array(0, 1); $b = array(1 = 0, 0 = 1); var_dump($a $b); // true var_dump($a $b); // true var_dump($b $a); var_dump($b $a); [...] OUTPUTS (on php5.0.4): bool(true) bool(true) bool(true) bool(true) [...] weird? I think so - but then again I'd never test that array $a is greater than array $b because this is meaningless to me (in what way is $a greater - how is this quantified, what 'rules' determine 'greatness' in this context?) The rules are simple enough, and listed in the documentation here: http://www.php.net/manual/en/language.operators.comparison.php#AEN4390 But if you apply those comparison rules to your four expressions, you'd expect to see bool(true) bool(false) bool(true) bool(false) exactly. What you need to know to explain your results is that internally, PHP doesn't do a greater-than comparison, it converts them into less-than-or-equals by reversing the values. So your expressions I learnt this on internals - which was what trigger me to investigate this... the thing I really couldn't grok was how php was auto-casting the arrays when doing the LT/GT comparison (well apparently it wasn't casting them at all!) thanks to you and David for helping to clear up my confusion (and for pointing out what I had missed - or forgotten about - in the manual) rgds, Jochem become: $a $b $b = $a $b $a $a = $b Now if you apply the comparison rules to your arrays using those rewritten operations, you get true every time. Fun eh? -robin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] anyone care to explain the logic behind this output ....
(Don't you hate it when people forget to post back to the list...) The secret is actually hidden in the docs, http://php.net/manual/en/language.operators.comparison.php When comparing $a $b, you compare the first value of $a (0) to the value with the same key in $b (1). 0 1 -- true When you compare $b $a, the first value of $b is 0, with a key of 1. The value in $a is 1, 0 1 -- true The discussion on php-dev, where you found this example, reveals that PHP makes the assumption that $a $b == $b $a. This means that they can reuse the less than function for greater than calls. In this case the assumption isn't actually valid. $a $b should be 0 1 -- false. David Jochem Maas wrote: THIS CODE php -r ' $a = array(0, 1); $b = array(1 = 0, 0 = 1); var_dump($a $b); // true var_dump($a $b); // true var_dump($b $a); var_dump($b $a); echo \n\$a:\n; var_dump((bool)$a, (int)$a, (string)$a, intval($a), strval($a)); echo \n\$b:\n; var_dump((bool)$b, (int)$b, (string)$b, intval($b), strval($b)); ' OUTPUTS (on php5.0.4): bool(true) bool(true) bool(true) bool(true) $a: bool(true) int(1) string(5) Array $b: bool(true) int(1) string(5) Array WHICH MEANS THAT: one the one hand $a is greater than AND less than $b but on the other hand casting $a OR $b to either a boolean, integer or string results in the exact same value. ie: php -r ' $a = array(0, 1); $b = array(1 = 0, 0 = 1); var_dump( ((($a $b) === ($b $a)) === ((int)$a === (int)$b)) ); // WTF IT'S TRUE ' weird? I think so - but then again I'd never test that array $a is greater than array $b because this is meaningless to me (in what way is $a greater - how is this quantified, what 'rules' determine 'greatness' in this context?) PS - changing the $b array to something else (anything else as far as i can tell) causes the weirdness to not occur - which gives me the impression this could be a bug, anyone else get this impression? for instance try changing the second line of the code above to (merely switching the order or the defined array elements): $b = array(0 = 1, 1 = 0); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php