Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deprecated param in example, missing multidimensional example #4390

Open
ludrob opened this issue Jan 14, 2025 · 2 comments
Open

deprecated param in example, missing multidimensional example #4390

ludrob opened this issue Jan 14, 2025 · 2 comments
Labels
good first issue Good for newcomers

Comments

@ludrob
Copy link

ludrob commented Jan 14, 2025

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)  
@Girgias Girgias added the good first issue Good for newcomers label Jan 14, 2025
@Girgias
Copy link
Member

Girgias commented Jan 14, 2025

I agree that more examples would be good, could you provide a PR?

@ludrob
Copy link
Author

ludrob commented Jan 14, 2025

Sure, see #4394

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants