gettype() function is a built-in function in PHP that returns the data type of a given variable. It can be used to check the data type of a variable and ensure that the variable is of the expected type.
The syntax of the
gettype() function is as follows:
string gettype(mixed $var)
The function takes a single parameter,
$var, which is the variable to get the type of. The function returns a string that represents the data type of the variable.
Here is an example of how to use the
gettype() function in PHP:
$var1 = "hello"; $var2 = 42; $var3 = true; $var4 = ; echo gettype($var1) . "\n"; // output: string echo gettype($var2) . "\n"; // output: integer echo gettype($var3) . "\n"; // output: boolean echo gettype($var4) . "\n"; // output: array
In this example, we define four variables with different data types:
$var1 is a string,
$var2 is an integer,
$var3 is a boolean, and
$var4 is an array. We then use the
gettype() function to get the type of each variable and output the result. The output shows the data type of each variable as "string", "integer", "boolean", and "array".
gettype() function is a useful tool for checking the data type of a variable in PHP. It can be used to ensure that a variable is of the expected type before performing operations on it, or to handle variables of different data types in a particular way. By using this function, developers can ensure that their code is working with the correct data types and avoid errors that may occur when working with mixed data types.