PHP gettype() function with example

PHP gettype() function with example

PHP gettype() function: Here, we will find out about the gettype() function in PHP programming with an example, which is utilized to get the sort of the information or datatype.

PHP gettype() function

In PHP, we have a library function gettype() to recognize the kind of information. The function is essentially used to once-over to verify everything seems ok the kind of information being contributed to a variable. The function can recognize the information into the accompanying information types:

Integer

  • Twofold
  • String
  • Cluster
  • Item
  • Boolean
  • Asset
  • Invalid
  • Obscure
  • Syntax:
  • gettype($var)

Where $var is a variable containing information that should 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

Recent Posts

error: Alert: Content is protected!!