$customer[total_shipping_cost] when i use var_dump ...">

Specify data type in PHP array

I have an array containing this

"postage_cost" => $customer[total_shipping_cost]

      

when i use var_dump

i get

["postage_cost"]=>
  string(5) "34.54"

      

How can I declare that it is a float and not a string when creating an array? I am posting this array to a web service and I am afraid there might be some data type confusion. The $ customer result is a MySQL query.

+3


source to share


3 answers


"postage_cost" => (float) $customer['total_shipping_cost']

      



Note that I have added quotes to the key because I am up to 99.999% sure that you do not have a constant with a name total_shipping_cost

. PHP is kind to this, but with error reporting enabled it would be Note: undefined constant

+6


source


"postage_cost" => $customer['total_shipping_cost'] + 0.0

      

or



"postage_cost" => (float) $customer['total_shipping_cost']

      

Beware of adding single quotes around total_shipping_quotes

. This is not required, but is considered better style than raw text; it's a little faster.

+2


source


"postage_cost" => floatval($customer[total_shipping_cost])

      

+1


source







All Articles