xref: /PHP-8.0/ext/standard/tests/array/end.phpt (revision b5c7a83d)
1--TEST--
2Test end() function
3--SKIPIF--
4<?php
5if (PHP_INT_SIZE != 4) die("skip this test is for 32bit platform only");
6?>
7--INI--
8precision=14
9--FILE--
10<?php
11$arrays = array (
12  array( 0 ),
13  range(1, 100 ),
14  range('a', 'z', 2 ),
15  array("a" => "A", 2 => "B", "C" => 3, 4 => 4, "one" => 1, "" => NULL ),
16  array(1, array(1, 2 => 3 ), "one" => 1, "5" => 5 ),
17  array(-1, -2, -3, -4, "-0.005" => "neg0.005", 2.0 => "float2", "neg.9" => -.9 ),
18  array(1.0005, 2.000000, -3.000000, -4.9999999 ),
19  array(true, false),
20  array("PHP", "Web2.0", "SOA"),
21  array(1, array() ),
22  array(1, 2, "" ),
23  array(" "),
24  array(2147483647, 2147483648, -2147483647, -2147483648 ),
25  array(0x7FFFFFFF, -0x80000000, 017777777777, -020000000000 ),
26  array(-.6700000E+3, -4.10003E+3, 1e-5, -1E+5, 000002.00 )
27);
28/* loop through $arrays to print the last element of each sub-array */
29echo "*** Testing end() on different arrays ***\n";
30$counter = 1;
31foreach ($arrays as $sub_array){
32  echo "-- Iteration $counter --\n";
33  var_dump( end($sub_array) );
34  /* ensure that internal pointer is moved to last element */
35  var_dump( current($sub_array) );
36  $counter++;
37}
38
39/* checking for end() on sub-arrays */
40echo "\n*** Testing end() with sub-arrays ***\n";
41$test_array = array(1, array(1 => "one", "two" => 2, "" => "f") );
42var_dump( end($test_array) );
43var_dump( end($test_array[1]) );
44
45/* checking working of end() when array elements are deleted */
46echo "\n*** Testing end() when array elements are deleted ***\n";
47$array_test = array("a", "b", "d", 7, "u" => "U", -4, "-.008" => "neg.008");
48
49// remove first element from array
50echo "\n-- Remove first element from array --\n";
51unset($array_test[0]);
52var_dump( end($array_test) );
53
54// remove last element from array, rewind and check end()
55echo "\n-- Remove last element from array --\n";
56unset($array_test['-.008']);
57var_dump( end($array_test) );
58reset( $array_test );
59var_dump( end($array_test) );
60
61// remove any element  !first, !last, rewind and check end()
62echo "\n-- Remove any element from array apart from first and last element --\n";
63unset($array_test[7]);
64var_dump( end($array_test) );
65var_dump( reset($array_test) );
66var_dump( end($array_test) );
67
68/* Checking on OBJECTS type */
69echo "\n*** Testing end() on objects ***\n";
70class foo
71{
72  function __toString() {
73    return "Object";
74  }
75}
76class foo1
77{
78  function __toString() {
79    return "Object1";
80  }
81}
82
83$object1 = new foo(); //new object created
84$object2 = new foo1();
85
86$array_object = array();
87$array_object[0] = &$object1;
88$array_object[1] = &$object2;
89var_dump( end($array_object) );
90var_dump($array_object);
91
92/* Checking on RESOURCE type */
93echo "\n*** Testing end() on resource type ***\n";
94//file type resource
95$file_handle = fopen(__FILE__, "r");
96
97//directory type resource
98$dir_handle = opendir( __DIR__ );
99
100//store resources in array
101$resources = array($file_handle, $dir_handle);
102var_dump( end($resources) );
103var_dump( current($resources) );
104
105echo "Done\n";
106
107
108/* cleaning resource handles */
109fclose( $file_handle );  //file resource handle deleted
110closedir( $dir_handle );  //dir resource handle deleted
111
112?>
113--EXPECTF--
114*** Testing end() on different arrays ***
115-- Iteration 1 --
116int(0)
117int(0)
118-- Iteration 2 --
119int(100)
120int(100)
121-- Iteration 3 --
122string(1) "y"
123string(1) "y"
124-- Iteration 4 --
125NULL
126NULL
127-- Iteration 5 --
128int(5)
129int(5)
130-- Iteration 6 --
131float(-0.9)
132float(-0.9)
133-- Iteration 7 --
134float(-4.9999999)
135float(-4.9999999)
136-- Iteration 8 --
137bool(false)
138bool(false)
139-- Iteration 9 --
140string(3) "SOA"
141string(3) "SOA"
142-- Iteration 10 --
143array(0) {
144}
145array(0) {
146}
147-- Iteration 11 --
148string(0) ""
149string(0) ""
150-- Iteration 12 --
151string(1) " "
152string(1) " "
153-- Iteration 13 --
154float(-2147483648)
155float(-2147483648)
156-- Iteration 14 --
157float(-2147483648)
158float(-2147483648)
159-- Iteration 15 --
160float(2)
161float(2)
162
163*** Testing end() with sub-arrays ***
164array(3) {
165  [1]=>
166  string(3) "one"
167  ["two"]=>
168  int(2)
169  [""]=>
170  string(1) "f"
171}
172string(1) "f"
173
174*** Testing end() when array elements are deleted ***
175
176-- Remove first element from array --
177string(7) "neg.008"
178
179-- Remove last element from array --
180int(-4)
181int(-4)
182
183-- Remove any element from array apart from first and last element --
184int(-4)
185string(1) "b"
186int(-4)
187
188*** Testing end() on objects ***
189object(foo1)#%d (0) {
190}
191array(2) {
192  [0]=>
193  &object(foo)#%d (0) {
194  }
195  [1]=>
196  &object(foo1)#%d (0) {
197  }
198}
199
200*** Testing end() on resource type ***
201resource(%d) of type (stream)
202resource(%d) of type (stream)
203Done
204