Repox
Repox

Reputation: 15475

gettype() returns the type based on the variable type - how do I get the type based on the content?

I'm trying to make a simple settings script, which basically send a setting name and a setting value to my database.

This is pretty simple actually - setting and getting this, requires little code and task is done.

But I would like the data to be returned with the correct type. Using gettype() returns string every time - this is logical since the content returned from my database is a string.

This effect can be achieved by using json_decode() on my content. This returns the type based on the content and it works like expected.

But do I have a better option?

I was considering assigning a type to my database table, but this just increases the complexity of setting and getting the data.

Upvotes: 0

Views: 812

Answers (1)

netfire
netfire

Reputation: 158

You can use the any of the following php functions to check if a variable matches the formatting of a specific variable type:

is_numeric, is_bool, is_null, is_float, is_int, is_string, is_object, is_array

Upvotes: 1

Related Questions