jimmy
jimmy

Reputation: 411

Specify data type in PHP array

I have an array which contains 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 this is a float and not a string when making the array? I'm sending this array to a web service and I'm afraid there might be some data type confusion. The $customer result is from a MySQL request.

Upvotes: 3

Views: 2419

Answers (3)

Fabien
Fabien

Reputation: 13456

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

or

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

Beware that I added single quotes around total_shipping_quotes. This is not mandatory but is considered better style than raw text ; it is slightly faster, too.

Upvotes: 2

user399666
user399666

Reputation: 19909

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

Upvotes: 1

Fabian Schmengler
Fabian Schmengler

Reputation: 24576

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

Note that I added quotation marks to the key because i am to 99.999 % sure that you don't have a constant named total_shipping_cost. PHP is gracious about that but with activated error reporting, this would have been a Notice: undefined constant

Upvotes: 6

Related Questions