The documentation is clear, imo:
E.g. for sort()
:
This function sorts an array. Elements will be arranged from lowest to highest when this function has completed.
And it takes a parameter to define how the values are treated:
Sorting type flags:
- SORT_REGULAR - compare items normally (don't change types) <- this is the default
- SORT_NUMERIC - compare items numerically
- SORT_STRING - compare items as strings
- SORT_LOCALE_STRING - compare items as strings, based on the current locale.
But it also has a warning:
Warning
Be careful when sorting arrays with mixed types values because sort() can produce unpredictable results.
Now for max()
:
Note: PHP will evaluate a non-numeric string as 0 if compared to integer , but still return the string if it's seen as the numerically highest value. If multiple arguments evaluate to 0, max() will return a numeric 0 if given, else the alphabetical highest string value will be returned.
and
When given a string it will be cast as an integer when comparing.
Update:
To answer your question: If you want to make absolutly sure that the sorting is correct, you know that the strings always contains numbers and you don't trust the string comparison, then specify to sort them numerically:
sort($array, SORT_NUMERIC);