From my db I return an array and then want to clean up this array through unset: My returned array looks like this:
Array ( [standard_rate] => 25.00 [reduced_rate] => 20.00 [super_reduced_rate] => 15.00 [zero] => 0.00 [other] => 0.00 )
If standard_rate = 25, I want to get this array without the standard_rate so I do:
if(standard_rate == 25.00 ) unset($return[standard_rate]);
this is not working, or
if(standard_rate == '25.00' ) unset($return[standard_rate]);
this is not working. However when I do:
if(standard_rate <= 25.00 ) unset($return[standard_rate]);
it works. I also tried:
if(zero== 0 ) unset($return[zero]);
works as well. The value of standard_rate in my db is of type double(11,2) and has value 25.00
Why is it that
if(standard_rate == 25.00 )
is not working?
If you had errors being displayed, you would see "undefined constant standard_rate
, assuming 'standard_rate'
". A string. Clearly, "standard_rate" == 25.00
is false.
if( $return['standard_rate'] == 25) unset($return['standard_rate']);
It should be:
if($return["standard_rate"] == 25) unset($return["standard_rate"]);
Now let’s see what’s wrong with your code.
standard_rate
is an undefined constant which has the numerical value 0
and assumed string value "standard_rate"
, so...
if(standard_rate == 25.00)
will not work because 0 isn’t equal to 25 (numerical comparison).
if(standard_rate == '25.00')
will not work because the strings "standard_rate"
and "25.00"
aren’t equal (string comparison).
if(standard_rate <= 25.00)
will work because 0 is less than 25 (numerical comparison).