1--TEST--
2Test array_push() function
3--FILE--
4<?php
5
6/* Prototype: int array_push( array &array );
7 * Description: Push one or more elements onto the end of array
8 and returns the new number of elements in the array.
9 */
10
11$empty_array = array();
12$number = 5;
13$str = "abc";
14
15
16/* Various combinations of arrays to be used for the test */
17$mixed_array = array(
18  array(),
19  array( 1,2,3,4,5,6,7,8,9 ),
20  array( "One", "_Two", "Three", "Four", "Five" ),
21  array( 6, "six", 7, "seven", 8, "eight", 9, "nine" ),
22  array( "a" => "aaa", "A" => "AAA", "c" => "ccc", "d" => "ddd", "e" => "eee" ),
23  array( "1" => "one", "2" => "two", "3" => "three", "4" => "four", "5" => "five" ),
24  array( 1 => "one", 2 => "two", 3 => 7, 4 => "four", 5 => "five" ),
25  array( "f" => "fff", "1" => "one", 4 => 6, "" => "blank", 2.4 => "float", "F" => "FFF",
26         "blank" => "", 3.7 => 3.7, 5.4 => 7, 6 => 8.6, '5' => "Five", "4name" => "jonny", "a" => NULL, NULL => 3 ),
27  array( 12, "name", 'age', '45' ),
28  array( array("oNe", "tWo", 4), array(10, 20, 30, 40, 50), array() ),
29  array( "one" => 1, "one" => 2, "three" => 3, 3, 4, 3 => 33, 4 => 44, 5, 6,
30          5.4 => 54, 5.7 => 57, "5.4" => 554, "5.7" => 557 )
31);
32
33/* Error Conditions */
34echo "\n*** Testing Error Conditions ***\n";
35
36/* Zero argument  */
37var_dump( array_push() );
38
39/* Scalar argument */
40var_dump( array_push($number, 22) );
41
42/* String argument */
43var_dump( array_push($str, 22) );
44
45/* Invalid Number of arguments */
46var_dump( array_push($mixed_array[1],1,2) );
47
48/* Empty Array as argument */
49var_dump( array_push($empty_array, 2) );
50
51
52/* Loop to test normal functionality with different arrays inputs */
53echo "\n*** Testing with various array inputs ***\n";
54
55$counter = 1;
56foreach( $mixed_array as $sub_array )
57{
58 echo "\n-- Input Array for Iteration $counter is --\n";
59 print_r( $sub_array );
60 echo "\nOutput after push is :\n";
61 var_dump( array_push($sub_array, 22, "abc") );
62 $counter++;
63}
64
65/* Checking for return value and the new array formed from push operation */
66echo "\n*** Checking for return value and the new array formed from push operation ***\n";
67var_dump( array_push($mixed_array[2], 22, 33, "44") );
68var_dump( $mixed_array[2] );
69
70echo"\nDone";
71?>
72--EXPECTF--
73*** Testing Error Conditions ***
74
75Warning: array_push() expects at least 2 parameters, 0 given in %s on line %d
76NULL
77
78Warning: array_push() expects parameter 1 to be array, integer given in %s on line %d
79NULL
80
81Warning: array_push() expects parameter 1 to be array, string given in %s on line %d
82NULL
83int(11)
84int(1)
85
86*** Testing with various array inputs ***
87
88-- Input Array for Iteration 1 is --
89Array
90(
91)
92
93Output after push is :
94int(2)
95
96-- Input Array for Iteration 2 is --
97Array
98(
99    [0] => 1
100    [1] => 2
101    [2] => 3
102    [3] => 4
103    [4] => 5
104    [5] => 6
105    [6] => 7
106    [7] => 8
107    [8] => 9
108    [9] => 1
109    [10] => 2
110)
111
112Output after push is :
113int(13)
114
115-- Input Array for Iteration 3 is --
116Array
117(
118    [0] => One
119    [1] => _Two
120    [2] => Three
121    [3] => Four
122    [4] => Five
123)
124
125Output after push is :
126int(7)
127
128-- Input Array for Iteration 4 is --
129Array
130(
131    [0] => 6
132    [1] => six
133    [2] => 7
134    [3] => seven
135    [4] => 8
136    [5] => eight
137    [6] => 9
138    [7] => nine
139)
140
141Output after push is :
142int(10)
143
144-- Input Array for Iteration 5 is --
145Array
146(
147    [a] => aaa
148    [A] => AAA
149    [c] => ccc
150    [d] => ddd
151    [e] => eee
152)
153
154Output after push is :
155int(7)
156
157-- Input Array for Iteration 6 is --
158Array
159(
160    [1] => one
161    [2] => two
162    [3] => three
163    [4] => four
164    [5] => five
165)
166
167Output after push is :
168int(7)
169
170-- Input Array for Iteration 7 is --
171Array
172(
173    [1] => one
174    [2] => two
175    [3] => 7
176    [4] => four
177    [5] => five
178)
179
180Output after push is :
181int(7)
182
183-- Input Array for Iteration 8 is --
184Array
185(
186    [f] => fff
187    [1] => one
188    [4] => 6
189    [] => 3
190    [2] => float
191    [F] => FFF
192    [blank] =>
193    [3] => 3.7
194    [5] => Five
195    [6] => 8.6
196    [4name] => jonny
197    [a] =>
198)
199
200Output after push is :
201int(14)
202
203-- Input Array for Iteration 9 is --
204Array
205(
206    [0] => 12
207    [1] => name
208    [2] => age
209    [3] => 45
210)
211
212Output after push is :
213int(6)
214
215-- Input Array for Iteration 10 is --
216Array
217(
218    [0] => Array
219        (
220            [0] => oNe
221            [1] => tWo
222            [2] => 4
223        )
224
225    [1] => Array
226        (
227            [0] => 10
228            [1] => 20
229            [2] => 30
230            [3] => 40
231            [4] => 50
232        )
233
234    [2] => Array
235        (
236        )
237
238)
239
240Output after push is :
241int(5)
242
243-- Input Array for Iteration 11 is --
244Array
245(
246    [one] => 2
247    [three] => 3
248    [0] => 3
249    [1] => 4
250    [3] => 33
251    [4] => 44
252    [5] => 57
253    [6] => 6
254    [5.4] => 554
255    [5.7] => 557
256)
257
258Output after push is :
259int(12)
260
261*** Checking for return value and the new array formed from push operation ***
262int(8)
263array(8) {
264  [0]=>
265  string(3) "One"
266  [1]=>
267  string(4) "_Two"
268  [2]=>
269  string(5) "Three"
270  [3]=>
271  string(4) "Four"
272  [4]=>
273  string(4) "Five"
274  [5]=>
275  int(22)
276  [6]=>
277  int(33)
278  [7]=>
279  string(2) "44"
280}
281
282Done
283