Skip to content

deprecated param in example, missing multidimensional example #4390

@ludrob

Description

@ludrob

From manual page: https://php.net/function.empty


  1. Examples contain deprecated params:
var_dump(empty($expected_array_got_string[0.5]));
// Output: Deprecated: Implicit conversion from float 0.5 to int loses precision in php-wasm run script on line 6

Using a float as an array key triggers a deprecation warning because implicit conversion from float to int is no longer fully supported. This example should be revised to use a valid integer key to avoid confusion and comply with current standards.

  1. Missing Example for Multidimensional Arrays
    The documentation could benefit from including examples of using empty() with multidimensional arrays, as handling nested keys is a common use case.

Consider the following example:

$multidimensional = [
    'some' => [
        'deep' => [
            'nested' => 'value'
        ]
    ]
];

// Correct usage to avoid warnings:
if (!empty($multidimensional['some']['deep']['nested'])) {
    $someVariable = $multidimensional['some']['deep']['nested'];
}

Most users might unnecessarily write verbose checks due to the fear of triggering warnings:

if (
    !empty($multidimensional) && 
    !empty($multidimensional['some']) && 
    !empty($multidimensional['some']['deep']) && 
    !empty($multidimensional['some']['deep']['nested'])
) {
    $someVariable = $multidimensional['some']['deep']['nested'];
}

To illustrate the safety of empty() with nested arrays, the documentation could include the following example:

var_dump(empty($multidimensional['some-undefined-key'])); // bool(true)
var_dump(empty($multidimensional['some']['deep']['unknown'])); // bool(true)
var_dump(empty($multidimensional['some']['deep']['nested']));  // bool(false)

output

bool(true)  
bool(true)  
bool(false)  

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions