How to check Data type 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
1 2 | $value = 'Hello'; echo "'Hello' Is data type - ".gettype($value); |
Taking $value = ‘1234’ which is a string type containing numbers
1 2 | $value = '1234'; echo "'1234' Is data type - ".gettype($value); |
Taking $value = 1234 which is an integer type (numeric)
1 2 | $value = 1234; echo "1234 Is data type - ".gettype($value); |
Taking $value = 56.02 and 29.00 which are both float(double) type but one has value after decimal however the other has not.
1 2 3 4 5 | $value = 56.02; echo "56.02 Is data type - ".gettype($value); $value = 29.00; echo "29.00 Is data type - ".gettype($value); |
Taking $value = true which is a boolean type (can be tested with $value = false)
1 2 | $value = true; echo "true Is data type - ".gettype($value); |
Taking $value = null which is a NULL type (can be $value = NULL)
1 2 | $value = null; echo "null Is data type - ".gettype($value); |
Taking $value = [10, 20, 30] which is an array
1 2 | $value = [10, 20, 30]; echo "[10, 20, 30] Is data type - ".gettype($value); |
Taking $value = new MyClass() which is an object
1 2 3 4 5 | Class MyClass{ } $value = new MyClass(); echo "\$value Is data type - ".gettype($value).' , an instance of MyClass class'; |
Taking $value as a resource to text file text.txt in the same directory, in the Read mode.
1 2 3 | $value = fopen(__DIR__.'/text.txt','r'); echo "\$value Is data type - ".gettype($value).' , an resource of file text.txt'; fclose($value); |
hi iam new to php i have a doubt, how to find the datatype of the user input in php can anyone explain please