PHP get type of variable
In this article, we will see about the php get type of variable. We can use “gettype()” function to get a type of variable. gettype() function is a variable handling function used to display the type of data.
Syntax:
gettype(data) : string
where: data : can be a data value, variable name or constant name
Data types:
$playerscore = 10; // it is holding integer type of data
$playerspeed = 3.5; // it is holding double type of data
$playername = “john” ; // it is holding string type of data
$isgone = false ; // it is holding string type of data
Example:
<?php
echo gettype("Hello world"); //string
$playerscore = 10;
echo gettype($playerscore) // integer
?>
- In the above code, we get a string data type using “gettype()” function by passing “Hello world”. “Hello world” is a string, so it returns a string.
- Then we created a variable “playerscore” and assigned the value in integer “10”.
- Then we print gettype() function with the passing variable “playerscore“. 10 is an integer, so it returns an integer.