简体   繁体   中英

PHP is casting all array values to strings

I have a problem with arrays.

On an old server all the data in arrays were with good types: a string was a string, a bool was a bool and an int was an int.

Now after a server change, all array values are "casting" to string. I cannot compare them with === against my integers because they are "string"

This is how it looks with laravel dd on the old server, versus the new one .

Is there any php setting that causes this that should be fixed?

Looking at the screenshots it looks like the following is happening.

  • null value is changed to empty string
  • integer values are now strings

Database

  • Check the column types in the database are these still int and not varchar ?
  • Since PHP 5.3.3 there is a json_encode param called JSON_NUMERIC_CHECK is this used ?

Maybe some framework is updated and using json_encode/json_decode with/without this param. This will result in integer values as strings.

echo json_encode(array('event_id' => '603'));
echo json_encode(array('event_id' => '603'), JSON_NUMERIC_CHECK);

//and the output:

{"event_id":"603"}
{"event_id":603}

On laravel, you could just cast the string to become integer. For example:

return (int)$your_variable->your_column

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM