PHP How to check Data type in PHP – PHP gettype()

How to check Data type in PHP – PHP gettype()

How to check Datatype in PHP - php gettype()

To check data type of any variable, PHP provide a function gettype().

PHP gettype() function returns the data type of the variable value passed as argument and returns a string which is a clear data type of the passed value, like: integer, double, string etc.

I am trying to demonstrate almost all the possible cases with the below examples. If I missed something please mention. I am taking a variable $value which will passed as an argument in gettype() function to check its data type.

Check Integer data type in PHP

Taking $value = ‘Hello’ which is a string type

Taking $value = ‘1234’ which is a string type containing numbers

Taking $value = 1234 which is an integer type (numeric)

Taking $value = 56.02 and 29.00 which are both float(double) type but one has value after decimal however the other has not.

Taking $value = true which is a boolean type (can be tested with $value = false)

Taking $value = null which is a NULL type (can be $value = NULL)

Taking $value = [10, 20, 30] which is an array

Taking $value = new MyClass() which is an object

Taking $value as a resource to text file text.txt in the same directory, in the Read mode.

Share your thoughts

Notify of