Updated April 5, 2023
Introduction to PHP gettype()
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.
Syntax
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:
- Boolean.: Two values Only(true/False)
- Integer.: All integers
- Array: Array values
- Double: Decimal values
- Object: Type object
- Null: Null Data
- Resource
- Unknown Type
Working of gettype() Function
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')
- PHP takes the value and checks in the container for the data type mapping the Object since it is a type of String.
- A string is returned as the Output.
Examples of PHP gettype()
Given below are the examples of PHP gettype():
Example #1: Integer
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:
Example #2: Boolean
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:
Example #3: Array
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.
Example #4: Double
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:
Example #5: Object
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:
Example #6: Null
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:
Example #7: Resource
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:
Conclusion
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.
Recommended Articles
This is a guide to PHP gettype(). Here we discuss the introduction, gettype() function working along with examples, respectively. You may also have a look at the following articles to learn more –