-
Notifications
You must be signed in to change notification settings - Fork 830
Closed
Labels
good first issueGood for newcomersGood for newcomers
Description
From manual page: https://php.net/function.empty
- 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.
- 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
Labels
good first issueGood for newcomersGood for newcomers