Re: anyone care to explain the logic behind this output ....

From: Date: Fri, 17 Feb 2006 16:11:11 +0000
Subject: Re: anyone care to explain the logic behind this output ....
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-230641@lists.php.net to get a copy of this message
On 17/02/06, Jochem Maas <jochem@iamjochem.com> 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

« previous php.general (#230641) next »