PHP gettype()

WBOY
Release: 2024-08-29 12:48:46
Original
443 people have browsed it

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 Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax

The syntax for gettype() function is:

String gettype(variable_name)
Copy after login

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')
Copy after login
  • 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>
Copy after login

This sample PHP code returns Integer as the Output as the given variable is of the type Integer.

Output:

PHP gettype()

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>
Copy after login

This sample PHP code returns Boolean as the Output as the given variable is of the type Boolean.

Output:
PHP gettype()

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>
Copy after login

This sample PHP code returns Array as the Output as the given variable is of the Type Array.

Output:
PHP gettype()

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>
Copy after login

This sample PHP code returns Double as the Output as the given variable is of the type Double.

Output:

PHP gettype()

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>
Copy after login

This sample PHP code returns Object as the Output as the given variable is of the type Object.

Output:

PHP gettype()

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>
Copy after login

This sample PHP code returns Null as the Output as the given variable is of the Type Null.

Output:

PHP gettype()

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>
Copy after login

This sample PHP code returns Resource as the Output as the given variable is of the Type Resource.

Output:

PHP gettype()

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>
Copy after login

A sample Array element with some random data type is created and looped in with the gettype function in the above code.

Output:

PHP gettype()

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.

The above is the detailed content of PHP gettype(). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!