Re: array "sort()" question, weird php behavior ?



The results you get is correct, given PHP's type conversion rules:

When comparing a boolean to a number, PHP converts the number to a
boolean. -1 becomes true. The order of elements with the same value
after a quicksort is undefine, hence true could come before -1.

When comparing a number to a string, PHP converts the string to a
number. "test" becomes 0, which is bigger than -1 and less than 30.

.



Relevant Pages

  • Re: TRUE and FALSE are treated differently
    ... No -- you can't echo a boolean in PHP. ... the boolean is implicitly cast to a string. ... If you look up the manual on type casting, you'll find that FALSE is cast to an empty string, and TRUE is cast to a non-empty string. ...
    (comp.lang.php)
  • Re: if question
    ... >> performed before comparing. ... > and boolean is the simplest type. ... Why convert the string to boolean? ...
    (microsoft.public.scripting.jscript)
  • Re: array "sort()" question, weird php behavior ?
    ... > When comparing a boolean to a number, PHP converts the number to a ... > When comparing a number to a string, PHP converts the string to a ...
    (comp.lang.php)
  • Re: if question
    ... > (which is trivial if it's already a boolean). ... > the boolean value true (the empty string converts to false, ... > performed before comparing. ... conversions in COMPARISONS. ...
    (microsoft.public.scripting.jscript)
  • Re: Arithmetic error involving 0
    ... IIRC there are some odd issues in PHP when comparing 0 with a string. ... you change it to do a strict type check with!== it solves the ...
    (comp.lang.php)