PHP Data Types

PHP supports several data types including strings, integers, floats, booleans, arrays and more.

Syntaxvar_dump($variable);

Variables can store data of different types. PHP supports the following primitive types:

  • String — a sequence of characters.
  • Integer — a whole number.
  • Float — a number with a decimal point.
  • Booleantrue or false.
  • Array — stores multiple values.
  • NULL — a variable with no value.
  • Object — an instance of a class.

The var_dump() function shows both the type and value of a variable.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Validate that a form's age field holds an integer before inserting it into a database.
  • Choose the right data type when defining a product price column to avoid precision errors.
  • Use gettype() to debug an unexpected value returned from a third-party API.

More examples

Detecting a variable's type

gettype() returns the current type of any variable as a human-readable string.

Example · php
<?php
$name  = "Alice";
$age   = 30;
$price = 9.99;
$active = true;

echo gettype($name);   // string
echo gettype($age);    // integer
echo gettype($price);  // double
echo gettype($active); // boolean

Type-checking with is_* functions

The is_string() and is_int() family of functions let you branch safely on type before operating on a value.

Example · php
<?php
$value = "42";

if (is_string($value)) {
    echo "It is a string.";
}
if (!is_int($value)) {
    echo "Not an integer."; 
}

var_dump for full type inspection

var_dump() prints each value's type and content, making it invaluable when debugging mixed-type structures.

Example · php
<?php
$data = ["id" => 1, "score" => 4.5, "active" => false];
var_dump($data);
// array(3) {
//   ["id"]     => int(1)
//   ["score"]  => float(4.5)
//   ["active"] => bool(false)
// }

Discussion

  • Be the first to comment on this lesson.