1--TEST--
2Test uasort() function : usage variations - 'cmp_function' with reference argument
3--FILE--
4<?php
5/* Prototype  : bool uasort(array $array_arg, string $cmp_function)
6 * Description: Sort an array with a user-defined comparison function and maintain index association
7 * Source code: ext/standard/array.c
8*/
9
10/* Testing uasort() functionality with comparison function having arguments as reference
11 */
12
13echo "*** Testing uasort() : 'cmp_function' with reference arguments ***\n";
14
15// comparison function
16/* Prototype : int cmp(mixed &$value1, mixed &$value2)
17 * Parameters : $value1 and $value2 - values received by reference
18 * Return value : 0 - if both values are same
19 *                1 - if value1 is greater than value2
20 *               -1 - if value1 is less than value2
21 * Description : compares value1 and value2
22 */
23function cmp(&$value1, &$value2)
24{
25  if($value1 == $value2) {
26    return 0;
27  }
28  else if($value1 > $value2) {
29    return 1;
30  }
31  else
32    return -1;
33}
34
35// Int array with default keys
36$int_values = array(1, 8, 9, 3, 2, 6, 7);
37echo "-- Passing integer values to 'cmp_function' --\n";
38var_dump( uasort($int_values, 'cmp') );
39var_dump($int_values);
40
41// String array with default keys
42$string_values = array("Mango", "Apple", "Orange", "Banana");
43echo "-- Passing string values to 'cmp_function' --\n";
44var_dump( uasort($string_values, 'cmp') );
45var_dump($string_values);
46
47echo "Done"
48?>
49--EXPECTF--
50*** Testing uasort() : 'cmp_function' with reference arguments ***
51-- Passing integer values to 'cmp_function' --
52bool(true)
53array(7) {
54  [0]=>
55  int(1)
56  [4]=>
57  int(2)
58  [3]=>
59  int(3)
60  [5]=>
61  int(6)
62  [6]=>
63  int(7)
64  [1]=>
65  int(8)
66  [2]=>
67  int(9)
68}
69-- Passing string values to 'cmp_function' --
70bool(true)
71array(4) {
72  [1]=>
73  string(5) "Apple"
74  [3]=>
75  string(6) "Banana"
76  [0]=>
77  string(5) "Mango"
78  [2]=>
79  string(6) "Orange"
80}
81Done
82