Why does int value become string after reading from database? Why does int value become string after reading from database? database database

Why does int value become string after reading from database?


Since php is a dynamically typed language, data types at interfaces often are kind of puzzling.

Usually when data is retrieved from the database, the driver doesn't bother to convert each value to the type in php which resembles the data type in mysql most (or you could also say, php isn't intelligent enough to do so automatically); you'd have to do that conversion yourself. Fortunately, it is really easy to do so:

$yourvar = (int)$yourvar;


Yii CActiveRecord creates model variables dynamically when we executes a query and by default they are of string type. They use _get & _set magic method to create dynamically, and they do not set their types so they are of string type.

As PHP is not strongly typed language so you should not worry about this, you can change their type.