| 
 | 1 | +--TEST--  | 
 | 2 | +array_first()/array_last()  | 
 | 3 | +--FILE--  | 
 | 4 | +<?php  | 
 | 5 | +$str = "hello world";  | 
 | 6 | + | 
 | 7 | +$test_cases = [  | 
 | 8 | +    ["single element"],  | 
 | 9 | +    [&$str, 1],  | 
 | 10 | +    [1, &$str],  | 
 | 11 | +    [1 => 1, 0 => 0, 3 => 3, 2 => 2],  | 
 | 12 | +    [100 => []],  | 
 | 13 | +    [new stdClass, false],  | 
 | 14 | +    [true, new stdClass],  | 
 | 15 | +];  | 
 | 16 | + | 
 | 17 | +foreach ($test_cases as $test_case) {  | 
 | 18 | +    // Output the checked values  | 
 | 19 | +    echo "--- Testing: ", json_encode($test_case), " ---\n";  | 
 | 20 | +    echo "First: ", json_encode(array_first($test_case)), "\n";  | 
 | 21 | +    echo "Last: ", json_encode(array_last($test_case)), "\n";  | 
 | 22 | + | 
 | 23 | +    // Sanity check consistency with array_key_first()/array_key_last()  | 
 | 24 | +    if (array_first($test_case) !== $test_case[array_key_first($test_case)]) {  | 
 | 25 | +        throw new Error("Key first and value first inconsistency");  | 
 | 26 | +    }  | 
 | 27 | +    if (array_last($test_case) !== $test_case[array_key_last($test_case)]) {  | 
 | 28 | +        throw new Error("Key last and value last inconsistency");  | 
 | 29 | +    }  | 
 | 30 | +}  | 
 | 31 | +?>  | 
 | 32 | +--EXPECT--  | 
 | 33 | +--- Testing: ["single element"] ---  | 
 | 34 | +First: "single element"  | 
 | 35 | +Last: "single element"  | 
 | 36 | +--- Testing: ["hello world",1] ---  | 
 | 37 | +First: "hello world"  | 
 | 38 | +Last: 1  | 
 | 39 | +--- Testing: [1,"hello world"] ---  | 
 | 40 | +First: 1  | 
 | 41 | +Last: "hello world"  | 
 | 42 | +--- Testing: {"1":1,"0":0,"3":3,"2":2} ---  | 
 | 43 | +First: 1  | 
 | 44 | +Last: 2  | 
 | 45 | +--- Testing: {"100":[]} ---  | 
 | 46 | +First: []  | 
 | 47 | +Last: []  | 
 | 48 | +--- Testing: [{},false] ---  | 
 | 49 | +First: {}  | 
 | 50 | +Last: false  | 
 | 51 | +--- Testing: [true,{}] ---  | 
 | 52 | +First: true  | 
 | 53 | +Last: {}  | 
0 commit comments