How to check the data type of a variable in PHP?

Use var_dump() to see the type and value, gettype() to get the type name, or the is_ functions (like is_int(), is_string(), is_array()) to test a type.

<?php
$a = 343;
$b = "Hello";
$c = 3.14;
$d = [1, 2];

var_dump($a);
echo gettype($b) . "<br>";
var_dump(is_float($c));
echo gettype($d);
?>

Output:

Tutorials Class - Output Window

int(343)
string
bool(true)
array

Note: For a float, gettype() returns the word “double” (an old name). From PHP 8 you can use get_debug_type(), which returns “float”.

In PHP you do not write the data type when creating a variable, PHP decides it from the value. Learn more in PHP Data Types.


Learn more about the similar topics:
Tutorials
PHP Variables
Exercises & Assignments
Write a program to print “Hello World” using echo
Write a program to print “Hello PHP” using variable
Write a program to print a string using echo+variable.
Write a program to print two variables in single echo
Interview Questions & Answers
How to check if a variable is empty in PHP?
Is PHP a case sensitive language?
What are the different data types in PHP?
What is the difference between $var and $$var in PHP?
Which special characters are allowed in PHP variable names?
Can we print multiple variables in a single echo statement in PHP?
How to check the data type of a variable in PHP?