]> granicus.if.org Git - php/blob
4a750ccefa
[php] /
1 --TEST--
2 Test usort() function : usage variations - use built in functions as $cmp_function arg
3 --FILE--
4 <?php
5 /* Prototype  : bool usort(array $array_arg, string $cmp_function)
6  * Description: Sort an array by values using a user-defined comparison function
7  * Source code: ext/standard/array.c
8  */
9
10 /*
11  * Test usort() when comparison function is:
12  * 1. a built in comparison function
13  * 2. a language construct
14  */
15
16 echo "*** Testing usort() : usage variation ***\n";
17
18 // Initializing variables
19 $array_arg = array("b" => "Banana", "m" => "Mango", "a" => "apple",
20                    "p" => "Pineapple", "o" => "orange");
21
22 // Testing library functions as comparison function
23 echo "\n-- Testing usort() with built-in 'cmp_function': strcasecmp() --\n";
24 $temp_array1 = $array_arg;
25 var_dump( usort($temp_array1, 'strcasecmp') );
26 var_dump($temp_array1);
27
28 echo "\n-- Testing usort() with built-in 'cmp_function': strcmp() --\n";
29 $temp_array2 = $array_arg;
30 var_dump( usort($temp_array2, 'strcmp') );
31 var_dump($temp_array2);
32
33 ?>
34 --EXPECT--
35 *** Testing usort() : usage variation ***
36
37 -- Testing usort() with built-in 'cmp_function': strcasecmp() --
38 bool(true)
39 array(5) {
40   [0]=>
41   string(5) "apple"
42   [1]=>
43   string(6) "Banana"
44   [2]=>
45   string(5) "Mango"
46   [3]=>
47   string(6) "orange"
48   [4]=>
49   string(9) "Pineapple"
50 }
51
52 -- Testing usort() with built-in 'cmp_function': strcmp() --
53 bool(true)
54 array(5) {
55   [0]=>
56   string(6) "Banana"
57   [1]=>
58   string(5) "Mango"
59   [2]=>
60   string(9) "Pineapple"
61   [3]=>
62   string(5) "apple"
63   [4]=>
64   string(6) "orange"
65 }