How to check data types in PHP, Gettype()
Is there any way to check the type of data, variables, etc. in PHP? Below you will learn how to check the data type.
※ Learn about PHP data types
The simplest way to do this is to use the gettype() function. gettype() returns the type of data with a built-in function. First, php has the following types.
unknown type
NULL
string
integer
array
object
bollean
...
There are various types as above. See below for an example of using getType.
※ See an example of the PHP gettype() function
This example shows the type of the variable $myStr shown below.
$myStr1 = 'abc'
$myStr2 = 100
$myStr3 = true
$myStr4 = NULL
echo gettype($myStr1);
echo gettype($myStr2);
echo gettype($myStr3);
echo gettype($myStr4);
When you execute the above code, you will see the following.
'string'
'integer'
'boolean'
'NULL'
So far, I've learned how to check the type in PHP. There is also a function to check the type, but there is also a function to specify and set the type. You can use the settype () function.