PHP ยท Chapter 7 of 44

PHP Data Types

PHP supports several data types for storing different kinds of values: String, Integer, Float (double), Boolean, Array, Object, NULL, and a couple of special types used internally. Understanding these helps you predict how PHP will behave with your data.

Because PHP is loosely typed, the same variable can hold different types over its lifetime, and PHP automatically converts between types when needed, a process called type juggling.

Syntax
gettype($variable);
var_dump($variable);

Common scalar types

String holds text like "Hello". Integer holds whole numbers like 25. Float holds decimal numbers like 9.99. Boolean holds true or false.

Checking a variable's type

The gettype() function returns a variable's current type as a string, and var_dump() shows both the type and the value, which is very useful for debugging.

Example 1 (php)
<?php
  $a = "Hello";
  $b = 25;
  $c = 9.99;
  $d = true;
  echo gettype($a) . " " . gettype($b) . " " . gettype($c) . " " . gettype($d);
?>
Output
string integer double boolean

gettype() reports the current data type of each variable.

Example 2 (php)
<?php
  $x = 10;
  var_dump($x);
?>
Output
int(10)

var_dump() prints both the type and value, which is helpful when debugging.

Key points

  • PHP's main scalar types are String, Integer, Float and Boolean.
  • Arrays, Objects and NULL are also valid PHP data types.
  • gettype() returns the type of a variable as a string.
  • PHP automatically converts between types when needed (type juggling).
๐Ÿ’ก Note: PHP calls its double-precision floating point type 'float' or 'double' interchangeably.

๐Ÿ“ Quick Quiz

1. Which function returns a variable's data type?

2. What does var_dump() show?

3. Is PHP a strongly typed language?