1--TEST--
2Test usort() function : usage variations - string data
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 * Pass arrays of string data to usort() to test how it is re-ordered
12 */
13
14echo "*** Testing usort() : usage variation ***\n";
15
16function cmp_function($value1, $value2)
17{
18  if($value1 == $value2) {
19    return 0;
20  }
21  else if($value1 > $value2) {
22    return 1;
23  }
24  else {
25    return -1;
26  }
27}
28
29// Different heredoc strings to be sorted
30$empty_heredoc =<<<EOT
31EOT;
32
33$simple_heredoc1 =<<<EOT
34Heredoc
35EOT;
36
37$simple_heredoc2 =<<<EOT
38HEREDOC
39EOT;
40
41$multiline_heredoc =<<<EOT
42heredoc string\twith!@# and 123
43Test this!!!
44EOT;
45
46// Single quoted strings
47$single_quoted_values = array(
48  0 => ' ',  1 => 'test', 3 => 'Hello', 4 => 'HELLO',
49  5 => '',   6 => '\t',   7 => '0',     8 => '123Hello',
50  9 => '\'', 10 => '@#$%'
51);
52
53echo "\n-- Sorting Single Quoted String values --\n";
54var_dump( usort($single_quoted_values, 'cmp_function') );
55var_dump($single_quoted_values);
56
57// Double quoted strings
58$double_quoted_values = array(
59  0 => " ",  1 => "test", 3 => "Hello", 4 => "HELLO",
60  5 => "",   6 => "\t",   7 => "0",     8 => "123Hello",
61  9 => "\"", 10 => "@#$%"
62);
63
64echo "\n-- Sorting Double Quoted String values --\n";
65var_dump( usort($double_quoted_values, 'cmp_function') );
66var_dump($double_quoted_values);
67
68// Heredoc strings
69$heredoc_values = array(0 => $empty_heredoc,   1 => $simple_heredoc1,
70                        2 => $simple_heredoc2, 3 => $multiline_heredoc);
71
72echo "\n-- Sorting Heredoc String values --\n";
73var_dump( usort($heredoc_values, 'cmp_function') );
74var_dump($heredoc_values);
75?>
76===DONE===
77--EXPECTF--
78*** Testing usort() : usage variation ***
79
80-- Sorting Single Quoted String values --
81bool(true)
82array(10) {
83  [0]=>
84  string(0) ""
85  [1]=>
86  string(1) " "
87  [2]=>
88  string(1) "'"
89  [3]=>
90  string(1) "0"
91  [4]=>
92  string(8) "123Hello"
93  [5]=>
94  string(4) "@#$%"
95  [6]=>
96  string(5) "HELLO"
97  [7]=>
98  string(5) "Hello"
99  [8]=>
100  string(2) "\t"
101  [9]=>
102  string(4) "test"
103}
104
105-- Sorting Double Quoted String values --
106bool(true)
107array(10) {
108  [0]=>
109  string(0) ""
110  [1]=>
111  string(1) "	"
112  [2]=>
113  string(1) " "
114  [3]=>
115  string(1) """
116  [4]=>
117  string(1) "0"
118  [5]=>
119  string(8) "123Hello"
120  [6]=>
121  string(4) "@#$%"
122  [7]=>
123  string(5) "HELLO"
124  [8]=>
125  string(5) "Hello"
126  [9]=>
127  string(4) "test"
128}
129
130-- Sorting Heredoc String values --
131bool(true)
132array(4) {
133  [0]=>
134  string(0) ""
135  [1]=>
136  string(7) "HEREDOC"
137  [2]=>
138  string(7) "Heredoc"
139  [3]=>
140  string(%d) "heredoc string	with!@# and 123
141Test this!!!"
142}
143===DONE===