PHP comes out with the function of gettype, which is used to get the type of a given variable. Whatever the variables we have to get the type of that, we use the function gettype() in PHP. Everything, every variable defined in PHP, has some types, and by knowing the type of that variable, we can define the process or handle the functioning of that particular variable easily. The PHP gettype() function is used for the same. This version came after PHP version 4.0+ stable release and is now widely used for programming purpose.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
The syntax for gettype() function is:
String gettype(variable_name)
This accepts a single parameter as the input for which the type is needed to find out.
The return type for the function is the possible data type that the input function persists.
Some of the return values are:
gettype() takes a parameter as an argument whose data type we need to find out, after taking out it takes with the defined data types to match for the particular type of Data the user has given as Input. A table is defined in the internal memory space that is the data structure for a given Type. The type is matched, and the result is returned as the output.
The matched data type is returned as the Output for the particular function.
Code:
Gettype('PHP')
Given below are the examples of PHP gettype():
This gives the type Integer for a given variable inside the function.
Code:
<!DOCTYPE html> <html> <body> <?php $a = 3; echo gettype($a) . "<br>"; $a = 4; echo gettype($a) . "<br>"; $a = 5; echo gettype($a) . "<br>"; $a = 6; echo gettype($a) . "<br>"; ?> </body> </html>
This sample PHP code returns Integer as the Output as the given variable is of the type Integer.
Output:
This gives the type Boolean for a given variable inside the function.
Code:
<!DOCTYPE html> <html> <body> <?php $a = false; echo gettype($a) . "<br>"; $a = true; echo gettype($a) . "<br>"; ?> </body> </html>
This sample PHP code returns Boolean as the Output as the given variable is of the type Boolean.
Output:
This gives the type array for a given variable inside the function.
Code:
<!DOCTYPE html> <html> <body> <?php $a = array(); echo gettype($a) . "<br>"; ?> </body> </html>
This sample PHP code returns Array as the Output as the given variable is of the Type Array.
Output:
We can also put the values inside an array and evaluate the result.
This gives the type Double for a given variable inside the function.
Code:
<!DOCTYPE html> <html> <body> <?php $b = 3.2; echo gettype($b) . "<br>"; $b = 6.2; echo gettype($b) . "<br>"; $b = 8.2; echo gettype($b) . "<br>"; ?> </body> </html>
This sample PHP code returns Double as the Output as the given variable is of the type Double.
Output:
This gives the type Object for a given variable inside the function.
Code:
<!DOCTYPE html> <html> <body> <?php $var6 = new stdClass; echo gettype($var6) ?> </body> </html>
This sample PHP code returns Object as the Output as the given variable is of the type Object.
Output:
This gives the type Null for a given variable inside the function.
Code:
<!DOCTYPE html> <html> <body> <?php $f = NULL; echo gettype($f) . "<br>"; $f = NULL; echo gettype($f) . "<br>"; ?> </body> </html>
This sample PHP code returns Null as the Output as the given variable is of the Type Null.
Output:
This gives the type Resource for a given variable inside the function.
Code:
<!DOCTYPE html> <html> <body> <?php $f = tmpfile(); echo gettype($f) . "<br>"; ?> </body> </html>
This sample PHP code returns Resource as the Output as the given variable is of the Type Resource.
Output:
We can also check the type of a variable by creating an Array with Random Datasets.
A foreach loop in PHP works like that.
Code:
<!DOCTYPE html> <html> <body> <?php $arr = array(210, 2.39, new stdClass, "hello,PHP", true); foreach ($arr as $value) { echo gettype($value). "<br>" ; } ?> </body> </html>
A sample Array element with some random data type is created and looped in with the gettype function in the above code.
Output:
From the above article, we saw the use of Function gettype in PHP. We tried to see how the gettype() function works in PHP and what are is use at the programming level from various example and classification. We also saw the internal working and the advantages of having the type of data we define for various programming purposes. Also, the syntax and examples helped us to see much precisely over the function.
The above is the detailed content of PHP gettype(). For more information, please follow other related articles on the PHP Chinese website!