PHP has these built-in types:
| Type | Example | Notes |
|---|---|---|
| String | "hello" |
Text |
| Integer | 42 |
Whole number |
| Float | 3.14 |
Decimal (also called double) |
| Boolean | true / false |
Two values only |
| Array | [1, 2, 3] |
Ordered collection |
| Object | new Product() |
Instance of a class |
| NULL | null |
"No value" |
Checking a type
PHP
<?php
$x = 42;
var_dump($x); // int(42)
echo gettype($x); // integer
if (is_int($x)) { echo "integer"; }
if (is_string($x)) { echo "string"; }
if (is_array($x)) { echo "array"; }
if (is_null($x)) { echo "null"; }var_dump() is the debugging workhorse — it prints the type and the
value, unlike echo, which would show true as 1 and null as
nothing at all.
Type juggling
PHP converts types automatically in many contexts. This is convenient and occasionally surprising:
PHP
<?php
echo "5" + 3; // 8 — string becomes int
echo "5" . 3; // "53" — . is concatenation
echo "abc" == 0; // false in PHP 8 (true in PHP 7 and earlier!)Loose vs strict comparison
== compares values after juggling types. === compares value and
type. Prefer === — it has far fewer surprises.
PHP
<?php
var_dump(0 == "0"); // true
var_dump(0 === "0"); // false ← different types
var_dump(null == false); // true
var_dump(null === false); // falseCasting
Force a type explicitly when you need to:
PHP
<?php
$s = "42abc";
$n = (int) $s; // 42
$f = (float) "3.5"; // 3.5
$b = (bool) ""; // false