Current Location: Home> Latest Articles> Best Ways to Output Variable Types in PHP with Examples

Best Ways to Output Variable Types in PHP with Examples

M66 2025-11-03

How to Output Variable Types in PHP

In PHP, if you want to check the type of a variable, you can use the gettype() function. It returns a string representing the type of the variable.

Introduction to gettype() Function

The gettype() function can return the following type strings:

  • boolean: Boolean value
  • integer: Integer value
  • double: Floating-point number
  • string: String value
  • array: Array
  • object: Object

Example Code

$x = 10;
$y = "Hello";
$z = true;

echo "Type of $x: " . gettype($x) . "<br>";
echo "Type of $y: " . gettype($y) . "<br>";
echo "Type of $z: " . gettype($z) . "<br>";

Output

Type of $x: integer
Type of $y: string
Type of $z: boolean

Summary

By using the gettype() function, you can easily determine and output the type of a variable in PHP. This is very useful for debugging and data processing. Mastering this method helps developers manage variables and debug code more efficiently.