1--TEST--
2Test array_unshift() function : usage variations - all possible values for 'var' argument
3--FILE--
4<?php
5/* Prototype  : int array_unshift(array $array, mixed $var [, mixed ...])
6 * Description: Pushes elements onto the beginning of the array
7 * Source code: ext/standard/array.c
8*/
9
10/*
11 * Testing array_unshift() by giving all the possible values for $var argument
12*/
13
14echo "*** Testing array_unshift() : all possible values for \$var argument ***\n";
15
16// array to be passed to $array argument
17$array = array('f' => "first", "s" => 'second', 1, 2.222);
18
19// get a class
20class classA
21{
22  public function __toString() {
23    return "Class A object";
24  }
25}
26
27// get a resource variable
28$fp = fopen(__FILE__, "r");
29
30// heredoc string
31$heredoc = <<<EOT
32hello world
33EOT;
34
35// get an unset variable
36$unset_var = 10;
37unset ($unset_var);
38
39// different types of values to be passed to $var argument
40$vars = array(
41
42       // int data
43/*1*/  0,
44       1,
45       12345,
46       -2345,
47
48       // float data
49/*5*/  10.5,
50       -10.5,
51       12.3456789000e10,
52       12.3456789000E-10,
53       .5,
54
55       // array data
56/*10*/ array(),
57       array(0),
58       array(1),
59       array(1, 2),
60       array('color' => 'red', 'item' => 'pen'),
61
62       // null data
63/*15*/ NULL,
64       null,
65
66       // boolean data
67/*17*/ true,
68       false,
69       TRUE,
70       FALSE,
71
72       // empty data
73/*21*/ "",
74       '',
75
76       // string data
77/*23*/ "string",
78       'string',
79       $heredoc,
80
81       // object data
82/*26*/ new classA(),
83
84       // undefined data
85       @$undefined_var,
86
87       // unset data
88       @$unset_var,
89
90       // resource variable
91/*29*/ $fp
92);
93
94// loop through each element of $vars to check the functionality of array_unshift()
95$iterator = 1;
96foreach($vars as $var) {
97  echo "-- Iteration $iterator --\n";
98  $temp_array = $array;
99
100  /* with default argument */
101  // returns element count in the resulting array after arguments are pushed to
102  // beginning of the given array
103  var_dump( array_unshift($temp_array, $var) );
104
105  // dump the resulting array
106  var_dump($temp_array);
107
108  /* with optional arguments */
109  // returns element count in the resulting array after arguments are pushed to
110  // beginning of the given array
111  $temp_array = $array;
112  var_dump( array_unshift($temp_array, $var, "hello", 'world') );
113
114  // dump the resulting array
115  var_dump($temp_array);
116  $iterator++;
117}
118
119// close the file resource used
120fclose($fp);
121
122echo "Done";
123?>
124--EXPECTF--
125*** Testing array_unshift() : all possible values for $var argument ***
126-- Iteration 1 --
127int(5)
128array(5) {
129  [0]=>
130  int(0)
131  ["f"]=>
132  string(5) "first"
133  ["s"]=>
134  string(6) "second"
135  [1]=>
136  int(1)
137  [2]=>
138  float(2.222)
139}
140int(7)
141array(7) {
142  [0]=>
143  int(0)
144  [1]=>
145  string(5) "hello"
146  [2]=>
147  string(5) "world"
148  ["f"]=>
149  string(5) "first"
150  ["s"]=>
151  string(6) "second"
152  [3]=>
153  int(1)
154  [4]=>
155  float(2.222)
156}
157-- Iteration 2 --
158int(5)
159array(5) {
160  [0]=>
161  int(1)
162  ["f"]=>
163  string(5) "first"
164  ["s"]=>
165  string(6) "second"
166  [1]=>
167  int(1)
168  [2]=>
169  float(2.222)
170}
171int(7)
172array(7) {
173  [0]=>
174  int(1)
175  [1]=>
176  string(5) "hello"
177  [2]=>
178  string(5) "world"
179  ["f"]=>
180  string(5) "first"
181  ["s"]=>
182  string(6) "second"
183  [3]=>
184  int(1)
185  [4]=>
186  float(2.222)
187}
188-- Iteration 3 --
189int(5)
190array(5) {
191  [0]=>
192  int(12345)
193  ["f"]=>
194  string(5) "first"
195  ["s"]=>
196  string(6) "second"
197  [1]=>
198  int(1)
199  [2]=>
200  float(2.222)
201}
202int(7)
203array(7) {
204  [0]=>
205  int(12345)
206  [1]=>
207  string(5) "hello"
208  [2]=>
209  string(5) "world"
210  ["f"]=>
211  string(5) "first"
212  ["s"]=>
213  string(6) "second"
214  [3]=>
215  int(1)
216  [4]=>
217  float(2.222)
218}
219-- Iteration 4 --
220int(5)
221array(5) {
222  [0]=>
223  int(-2345)
224  ["f"]=>
225  string(5) "first"
226  ["s"]=>
227  string(6) "second"
228  [1]=>
229  int(1)
230  [2]=>
231  float(2.222)
232}
233int(7)
234array(7) {
235  [0]=>
236  int(-2345)
237  [1]=>
238  string(5) "hello"
239  [2]=>
240  string(5) "world"
241  ["f"]=>
242  string(5) "first"
243  ["s"]=>
244  string(6) "second"
245  [3]=>
246  int(1)
247  [4]=>
248  float(2.222)
249}
250-- Iteration 5 --
251int(5)
252array(5) {
253  [0]=>
254  float(10.5)
255  ["f"]=>
256  string(5) "first"
257  ["s"]=>
258  string(6) "second"
259  [1]=>
260  int(1)
261  [2]=>
262  float(2.222)
263}
264int(7)
265array(7) {
266  [0]=>
267  float(10.5)
268  [1]=>
269  string(5) "hello"
270  [2]=>
271  string(5) "world"
272  ["f"]=>
273  string(5) "first"
274  ["s"]=>
275  string(6) "second"
276  [3]=>
277  int(1)
278  [4]=>
279  float(2.222)
280}
281-- Iteration 6 --
282int(5)
283array(5) {
284  [0]=>
285  float(-10.5)
286  ["f"]=>
287  string(5) "first"
288  ["s"]=>
289  string(6) "second"
290  [1]=>
291  int(1)
292  [2]=>
293  float(2.222)
294}
295int(7)
296array(7) {
297  [0]=>
298  float(-10.5)
299  [1]=>
300  string(5) "hello"
301  [2]=>
302  string(5) "world"
303  ["f"]=>
304  string(5) "first"
305  ["s"]=>
306  string(6) "second"
307  [3]=>
308  int(1)
309  [4]=>
310  float(2.222)
311}
312-- Iteration 7 --
313int(5)
314array(5) {
315  [0]=>
316  float(123456789000)
317  ["f"]=>
318  string(5) "first"
319  ["s"]=>
320  string(6) "second"
321  [1]=>
322  int(1)
323  [2]=>
324  float(2.222)
325}
326int(7)
327array(7) {
328  [0]=>
329  float(123456789000)
330  [1]=>
331  string(5) "hello"
332  [2]=>
333  string(5) "world"
334  ["f"]=>
335  string(5) "first"
336  ["s"]=>
337  string(6) "second"
338  [3]=>
339  int(1)
340  [4]=>
341  float(2.222)
342}
343-- Iteration 8 --
344int(5)
345array(5) {
346  [0]=>
347  float(1.23456789E-9)
348  ["f"]=>
349  string(5) "first"
350  ["s"]=>
351  string(6) "second"
352  [1]=>
353  int(1)
354  [2]=>
355  float(2.222)
356}
357int(7)
358array(7) {
359  [0]=>
360  float(1.23456789E-9)
361  [1]=>
362  string(5) "hello"
363  [2]=>
364  string(5) "world"
365  ["f"]=>
366  string(5) "first"
367  ["s"]=>
368  string(6) "second"
369  [3]=>
370  int(1)
371  [4]=>
372  float(2.222)
373}
374-- Iteration 9 --
375int(5)
376array(5) {
377  [0]=>
378  float(0.5)
379  ["f"]=>
380  string(5) "first"
381  ["s"]=>
382  string(6) "second"
383  [1]=>
384  int(1)
385  [2]=>
386  float(2.222)
387}
388int(7)
389array(7) {
390  [0]=>
391  float(0.5)
392  [1]=>
393  string(5) "hello"
394  [2]=>
395  string(5) "world"
396  ["f"]=>
397  string(5) "first"
398  ["s"]=>
399  string(6) "second"
400  [3]=>
401  int(1)
402  [4]=>
403  float(2.222)
404}
405-- Iteration 10 --
406int(5)
407array(5) {
408  [0]=>
409  array(0) {
410  }
411  ["f"]=>
412  string(5) "first"
413  ["s"]=>
414  string(6) "second"
415  [1]=>
416  int(1)
417  [2]=>
418  float(2.222)
419}
420int(7)
421array(7) {
422  [0]=>
423  array(0) {
424  }
425  [1]=>
426  string(5) "hello"
427  [2]=>
428  string(5) "world"
429  ["f"]=>
430  string(5) "first"
431  ["s"]=>
432  string(6) "second"
433  [3]=>
434  int(1)
435  [4]=>
436  float(2.222)
437}
438-- Iteration 11 --
439int(5)
440array(5) {
441  [0]=>
442  array(1) {
443    [0]=>
444    int(0)
445  }
446  ["f"]=>
447  string(5) "first"
448  ["s"]=>
449  string(6) "second"
450  [1]=>
451  int(1)
452  [2]=>
453  float(2.222)
454}
455int(7)
456array(7) {
457  [0]=>
458  array(1) {
459    [0]=>
460    int(0)
461  }
462  [1]=>
463  string(5) "hello"
464  [2]=>
465  string(5) "world"
466  ["f"]=>
467  string(5) "first"
468  ["s"]=>
469  string(6) "second"
470  [3]=>
471  int(1)
472  [4]=>
473  float(2.222)
474}
475-- Iteration 12 --
476int(5)
477array(5) {
478  [0]=>
479  array(1) {
480    [0]=>
481    int(1)
482  }
483  ["f"]=>
484  string(5) "first"
485  ["s"]=>
486  string(6) "second"
487  [1]=>
488  int(1)
489  [2]=>
490  float(2.222)
491}
492int(7)
493array(7) {
494  [0]=>
495  array(1) {
496    [0]=>
497    int(1)
498  }
499  [1]=>
500  string(5) "hello"
501  [2]=>
502  string(5) "world"
503  ["f"]=>
504  string(5) "first"
505  ["s"]=>
506  string(6) "second"
507  [3]=>
508  int(1)
509  [4]=>
510  float(2.222)
511}
512-- Iteration 13 --
513int(5)
514array(5) {
515  [0]=>
516  array(2) {
517    [0]=>
518    int(1)
519    [1]=>
520    int(2)
521  }
522  ["f"]=>
523  string(5) "first"
524  ["s"]=>
525  string(6) "second"
526  [1]=>
527  int(1)
528  [2]=>
529  float(2.222)
530}
531int(7)
532array(7) {
533  [0]=>
534  array(2) {
535    [0]=>
536    int(1)
537    [1]=>
538    int(2)
539  }
540  [1]=>
541  string(5) "hello"
542  [2]=>
543  string(5) "world"
544  ["f"]=>
545  string(5) "first"
546  ["s"]=>
547  string(6) "second"
548  [3]=>
549  int(1)
550  [4]=>
551  float(2.222)
552}
553-- Iteration 14 --
554int(5)
555array(5) {
556  [0]=>
557  array(2) {
558    ["color"]=>
559    string(3) "red"
560    ["item"]=>
561    string(3) "pen"
562  }
563  ["f"]=>
564  string(5) "first"
565  ["s"]=>
566  string(6) "second"
567  [1]=>
568  int(1)
569  [2]=>
570  float(2.222)
571}
572int(7)
573array(7) {
574  [0]=>
575  array(2) {
576    ["color"]=>
577    string(3) "red"
578    ["item"]=>
579    string(3) "pen"
580  }
581  [1]=>
582  string(5) "hello"
583  [2]=>
584  string(5) "world"
585  ["f"]=>
586  string(5) "first"
587  ["s"]=>
588  string(6) "second"
589  [3]=>
590  int(1)
591  [4]=>
592  float(2.222)
593}
594-- Iteration 15 --
595int(5)
596array(5) {
597  [0]=>
598  NULL
599  ["f"]=>
600  string(5) "first"
601  ["s"]=>
602  string(6) "second"
603  [1]=>
604  int(1)
605  [2]=>
606  float(2.222)
607}
608int(7)
609array(7) {
610  [0]=>
611  NULL
612  [1]=>
613  string(5) "hello"
614  [2]=>
615  string(5) "world"
616  ["f"]=>
617  string(5) "first"
618  ["s"]=>
619  string(6) "second"
620  [3]=>
621  int(1)
622  [4]=>
623  float(2.222)
624}
625-- Iteration 16 --
626int(5)
627array(5) {
628  [0]=>
629  NULL
630  ["f"]=>
631  string(5) "first"
632  ["s"]=>
633  string(6) "second"
634  [1]=>
635  int(1)
636  [2]=>
637  float(2.222)
638}
639int(7)
640array(7) {
641  [0]=>
642  NULL
643  [1]=>
644  string(5) "hello"
645  [2]=>
646  string(5) "world"
647  ["f"]=>
648  string(5) "first"
649  ["s"]=>
650  string(6) "second"
651  [3]=>
652  int(1)
653  [4]=>
654  float(2.222)
655}
656-- Iteration 17 --
657int(5)
658array(5) {
659  [0]=>
660  bool(true)
661  ["f"]=>
662  string(5) "first"
663  ["s"]=>
664  string(6) "second"
665  [1]=>
666  int(1)
667  [2]=>
668  float(2.222)
669}
670int(7)
671array(7) {
672  [0]=>
673  bool(true)
674  [1]=>
675  string(5) "hello"
676  [2]=>
677  string(5) "world"
678  ["f"]=>
679  string(5) "first"
680  ["s"]=>
681  string(6) "second"
682  [3]=>
683  int(1)
684  [4]=>
685  float(2.222)
686}
687-- Iteration 18 --
688int(5)
689array(5) {
690  [0]=>
691  bool(false)
692  ["f"]=>
693  string(5) "first"
694  ["s"]=>
695  string(6) "second"
696  [1]=>
697  int(1)
698  [2]=>
699  float(2.222)
700}
701int(7)
702array(7) {
703  [0]=>
704  bool(false)
705  [1]=>
706  string(5) "hello"
707  [2]=>
708  string(5) "world"
709  ["f"]=>
710  string(5) "first"
711  ["s"]=>
712  string(6) "second"
713  [3]=>
714  int(1)
715  [4]=>
716  float(2.222)
717}
718-- Iteration 19 --
719int(5)
720array(5) {
721  [0]=>
722  bool(true)
723  ["f"]=>
724  string(5) "first"
725  ["s"]=>
726  string(6) "second"
727  [1]=>
728  int(1)
729  [2]=>
730  float(2.222)
731}
732int(7)
733array(7) {
734  [0]=>
735  bool(true)
736  [1]=>
737  string(5) "hello"
738  [2]=>
739  string(5) "world"
740  ["f"]=>
741  string(5) "first"
742  ["s"]=>
743  string(6) "second"
744  [3]=>
745  int(1)
746  [4]=>
747  float(2.222)
748}
749-- Iteration 20 --
750int(5)
751array(5) {
752  [0]=>
753  bool(false)
754  ["f"]=>
755  string(5) "first"
756  ["s"]=>
757  string(6) "second"
758  [1]=>
759  int(1)
760  [2]=>
761  float(2.222)
762}
763int(7)
764array(7) {
765  [0]=>
766  bool(false)
767  [1]=>
768  string(5) "hello"
769  [2]=>
770  string(5) "world"
771  ["f"]=>
772  string(5) "first"
773  ["s"]=>
774  string(6) "second"
775  [3]=>
776  int(1)
777  [4]=>
778  float(2.222)
779}
780-- Iteration 21 --
781int(5)
782array(5) {
783  [0]=>
784  string(0) ""
785  ["f"]=>
786  string(5) "first"
787  ["s"]=>
788  string(6) "second"
789  [1]=>
790  int(1)
791  [2]=>
792  float(2.222)
793}
794int(7)
795array(7) {
796  [0]=>
797  string(0) ""
798  [1]=>
799  string(5) "hello"
800  [2]=>
801  string(5) "world"
802  ["f"]=>
803  string(5) "first"
804  ["s"]=>
805  string(6) "second"
806  [3]=>
807  int(1)
808  [4]=>
809  float(2.222)
810}
811-- Iteration 22 --
812int(5)
813array(5) {
814  [0]=>
815  string(0) ""
816  ["f"]=>
817  string(5) "first"
818  ["s"]=>
819  string(6) "second"
820  [1]=>
821  int(1)
822  [2]=>
823  float(2.222)
824}
825int(7)
826array(7) {
827  [0]=>
828  string(0) ""
829  [1]=>
830  string(5) "hello"
831  [2]=>
832  string(5) "world"
833  ["f"]=>
834  string(5) "first"
835  ["s"]=>
836  string(6) "second"
837  [3]=>
838  int(1)
839  [4]=>
840  float(2.222)
841}
842-- Iteration 23 --
843int(5)
844array(5) {
845  [0]=>
846  string(6) "string"
847  ["f"]=>
848  string(5) "first"
849  ["s"]=>
850  string(6) "second"
851  [1]=>
852  int(1)
853  [2]=>
854  float(2.222)
855}
856int(7)
857array(7) {
858  [0]=>
859  string(6) "string"
860  [1]=>
861  string(5) "hello"
862  [2]=>
863  string(5) "world"
864  ["f"]=>
865  string(5) "first"
866  ["s"]=>
867  string(6) "second"
868  [3]=>
869  int(1)
870  [4]=>
871  float(2.222)
872}
873-- Iteration 24 --
874int(5)
875array(5) {
876  [0]=>
877  string(6) "string"
878  ["f"]=>
879  string(5) "first"
880  ["s"]=>
881  string(6) "second"
882  [1]=>
883  int(1)
884  [2]=>
885  float(2.222)
886}
887int(7)
888array(7) {
889  [0]=>
890  string(6) "string"
891  [1]=>
892  string(5) "hello"
893  [2]=>
894  string(5) "world"
895  ["f"]=>
896  string(5) "first"
897  ["s"]=>
898  string(6) "second"
899  [3]=>
900  int(1)
901  [4]=>
902  float(2.222)
903}
904-- Iteration 25 --
905int(5)
906array(5) {
907  [0]=>
908  string(11) "hello world"
909  ["f"]=>
910  string(5) "first"
911  ["s"]=>
912  string(6) "second"
913  [1]=>
914  int(1)
915  [2]=>
916  float(2.222)
917}
918int(7)
919array(7) {
920  [0]=>
921  string(11) "hello world"
922  [1]=>
923  string(5) "hello"
924  [2]=>
925  string(5) "world"
926  ["f"]=>
927  string(5) "first"
928  ["s"]=>
929  string(6) "second"
930  [3]=>
931  int(1)
932  [4]=>
933  float(2.222)
934}
935-- Iteration 26 --
936int(5)
937array(5) {
938  [0]=>
939  object(classA)#%d (0) {
940  }
941  ["f"]=>
942  string(5) "first"
943  ["s"]=>
944  string(6) "second"
945  [1]=>
946  int(1)
947  [2]=>
948  float(2.222)
949}
950int(7)
951array(7) {
952  [0]=>
953  object(classA)#%d (0) {
954  }
955  [1]=>
956  string(5) "hello"
957  [2]=>
958  string(5) "world"
959  ["f"]=>
960  string(5) "first"
961  ["s"]=>
962  string(6) "second"
963  [3]=>
964  int(1)
965  [4]=>
966  float(2.222)
967}
968-- Iteration 27 --
969int(5)
970array(5) {
971  [0]=>
972  NULL
973  ["f"]=>
974  string(5) "first"
975  ["s"]=>
976  string(6) "second"
977  [1]=>
978  int(1)
979  [2]=>
980  float(2.222)
981}
982int(7)
983array(7) {
984  [0]=>
985  NULL
986  [1]=>
987  string(5) "hello"
988  [2]=>
989  string(5) "world"
990  ["f"]=>
991  string(5) "first"
992  ["s"]=>
993  string(6) "second"
994  [3]=>
995  int(1)
996  [4]=>
997  float(2.222)
998}
999-- Iteration 28 --
1000int(5)
1001array(5) {
1002  [0]=>
1003  NULL
1004  ["f"]=>
1005  string(5) "first"
1006  ["s"]=>
1007  string(6) "second"
1008  [1]=>
1009  int(1)
1010  [2]=>
1011  float(2.222)
1012}
1013int(7)
1014array(7) {
1015  [0]=>
1016  NULL
1017  [1]=>
1018  string(5) "hello"
1019  [2]=>
1020  string(5) "world"
1021  ["f"]=>
1022  string(5) "first"
1023  ["s"]=>
1024  string(6) "second"
1025  [3]=>
1026  int(1)
1027  [4]=>
1028  float(2.222)
1029}
1030-- Iteration 29 --
1031int(5)
1032array(5) {
1033  [0]=>
1034  resource(%d) of type (stream)
1035  ["f"]=>
1036  string(5) "first"
1037  ["s"]=>
1038  string(6) "second"
1039  [1]=>
1040  int(1)
1041  [2]=>
1042  float(2.222)
1043}
1044int(7)
1045array(7) {
1046  [0]=>
1047  resource(%d) of type (stream)
1048  [1]=>
1049  string(5) "hello"
1050  [2]=>
1051  string(5) "world"
1052  ["f"]=>
1053  string(5) "first"
1054  ["s"]=>
1055  string(6) "second"
1056  [3]=>
1057  int(1)
1058  [4]=>
1059  float(2.222)
1060}
1061Done
1062