You must read manual about empty
0.0000
is not a number it is a string.
質問
I came across this situation, perhaps a logical error in the code.
This is my code
foreach ($options as $option) {
if (($option['max_value'] > 0) && $total_value <= $option['max_value']) {
$selected_options[] = $option['option_id'];
continue;
}
if (($option['min_value'] > 0) && $total_value >= $option['min_value']) {
$selected_options[] = $option['option_id'];
}
}
This works fine. But if i replace(this is what i did earlier, so the logical error)
if(($option['min_value'] > 0 ) ..... ) with
if(!empty($option['min_value']) .... ) same for checking max_value.
But the issue is only while checking min_value
Its treating 0.0000 to be not empty so i'm not getting the expected output. Can somebody please explain me what is wrong??
$options values are coming from database, i have 2 rows
min_value = 0.0000 max_value = 20.0000
min_value = 21.0000 max_value = 0.0000
Thanks!!
Thank You all!! Its a lesson learnt!!!
解決
You must read manual about empty
0.0000
is not a number it is a string.
他のヒント
if(!empty($option['min_value']) .... )
will definitely treat it as non rempty.
According to the manual, empty() determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals FALSE.
0.0000
is a sequence of character which cannot be considered as empty.
because the value in the variable is extracted from database so the value will be treated as a string. And empty() will only return true if the value in the variable is 0.0 (float/numeric) or 0 (numeric) or '0' (string) so '0.0' (string) would be returned as false.
Just change type of float to int
empty((int)$model['product_cost']) this returns false even if the value is 0.0