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