Search code examples
phpmysqlvariablesvariable-types

PHP change variable to corresponding type


I'm making a website in PHP with different config files depending on the company thats using it at that moment.

Every company has +/- 15 config files with all the same variables but ofcourse, different values. Now every company has to be able to easily change the settings so we are saving the settings in a MySQL database. The database has 3 columns 'company_id', 'setting' and 'value'.

When i'm reading the config items from the database, the variable type is always a 'string'. So ofcourse, the if statements "if($setting->value === false)" fails...

Is there an easy way to change the type of the variable corresponding the returned value from the database?


Solution

  • If you want to store arbitrary types in a single database column, you'll have to serialise them into a string representation, from which you can unserialise the value again. PHP offers serialize and unserialize for this purpose, but you can also use a language-neutral format like JSON, XML or YAML.