What will be the output of the PHP code snippet below?



==========================
$v = 1;
$m = 2;
$l = 3;

if( $l > $m > $v){
    echo "yes";
}else{
    echo "no";
}
=========================

Ans:

Since 3 > 2 > 1, one might be fooled into thinking the output would be “yes”.

In fact, though, the output will be “no”.

Here’s why:


First, $l > $m will be evaluated which yields a boolean value of 1 or true. Comparing that boolean value to the integer value 1 (i.e., bool(1) > $v) will result in NULL, so the output will be “no”.

Download PHP Extensions URL .

How would you sort an array of strings to their natural case-insensitive order, while maintaing their original index association?

For example, the following array:

array(
                '0' => 'z1',
                '1' => 'Z10',
                '2' => 'z12',
                '3' => 'Z2',
                '4' => 'z3',
)
After sorting, should become:

array(
                '0' => 'z1',
                '3' => 'Z2',
                '4' => 'z3',
                '1' => 'Z10',
                '2' => 'z12',
)

The trick to solving this problem is to use three special flags with the standard asort() library function:

asort($arr, SORT_STRING|SORT_FLAG_CASE|SORT_NATURAL)

The function asort() is a variant of the standard function sort() that preserves the index association. The three flags used above SORT_STRING, SORT_FLAG_CASE and SORT_NATURAL forces the sort function to treat the items as strings, sort in a case-insensitive way and maintain natural order respectively.

Note: Using the natcasesort() function would not be a correct answer, since it would not maintain the original index association of the elements of the array.