Why does int become a string after reading from the database?

In my Yii structure project, when I read data using Active Record from a database whose column data type is numeric, I got a row type value using var_dump . I am very confused.

+4
source share
2 answers

Since php is a dynamically typed language, data types on interfaces are often cryptic.

Usually, when data is retrieved from the database, the driver does not try to convert each value to a php type that is most similar to the data type in mysql (or you can also say that php is not smart enough to do this automatically); You will have to do this conversion yourself. Fortunately, this is really easy to do:

 $yourvar = (int)$yourvar; 
+2
source

Yii CActiveRecord dynamically creates dynamic variables when we execute a query, and by default they are of string type. They use the _get and _set magic methods for dynamic creation, and they do not set their types, so they have a string type.

Since PHP is not a strongly typed language, so you donโ€™t have to worry about it, you can change its type.

0
source

Source: https://habr.com/ru/post/1385853/


All Articles