Home » PHP

PHP gettype() function with example

PHP gettype() function: Here, we are going to learn about the gettype() function in PHP programming with example, which is used to get the type of the data or datatype.
Submitted by Bhanu Sharma, on September 15, 2019

PHP gettype() function

In PHP, we have a library function gettype() to identify the type of data. The function is primarily used to sanity check the type of data being input in a variable. The function can identify the data into the following data types:

  • Integer
  • Double
  • String
  • Array
  • Object
  • Boolean
  • Resource
  • Null
  • Unknown

Syntax:

    gettype($var)

Where, $var is a variable containing data that needs to be checked.

Example 1:

<?php
// Creating variables with random datatype
$var1 = 1;
$var2 = 1.1;
$var3 = NULL;
$var4 = "example";
$var5 = false;

// using gettype() function to 
// get the data type of the variable
echo gettype($var1)."\n";
echo gettype($var2)."\n";
echo gettype($var3)."\n";
echo gettype($var4)."\n";
echo gettype($var5)."\n";
?>

Output

integer
double
NULL
string
boolean

Example 2:

<?php
	// creating an array with random datatypes
	$arr = array(21, 1.99, new stdClass, "hello, World", false);
	
	// getting the type of each element of array
	// foreach  is used to initialize array 
	// as independent values
	foreach ($arr as $value) {
		// getting and printing the type
		echo gettype($value), "\n";
    }
?>

Output

integer
double
object
string
boolean





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.