7. PHP Data Types

What you will learn

  • The main PHP data types
  • How to check and convert types
  • What null means
  • How truthy and falsy values behave

The types

Type Example Meaning
string "Hello" Text
int 42 Whole number
float 3.14 Decimal number
bool true, false Yes or no
array [1, 2, 3] A list or map of values
object an instance of a class Data with behaviour
null null No value

Unlike JavaScript, PHP separates whole numbers (int) from decimals (float).

Checking a type

gettype() returns the type name, and var_dump() shows type and value.

PHP code

<?php
echo gettype("Hi") . "<br>";
echo gettype(42) . "<br>";
echo gettype(3.14) . "<br>";
echo gettype(true) . "<br>";
echo gettype([1, 2]) . "<br>";
echo gettype(null);

What the browser shows

string
integer
double
boolean
array
NULL

gettype() uses older names: integer, double (for float), and boolean. Tests such as is_int(), is_float(), is_string(), is_bool(), is_array(), and is_null() return true or false.

Numbers

PHP code

<?php
var_dump(10);
var_dump(10.5);
var_dump(10 / 4);
var_dump(10 / 5);
var_dump(intdiv(10, 4));

What you should see (inside <pre>)

int(10)
float(10.5)
float(2.5)
int(2)
int(2)

Division gives an int when the result is exact, and a float otherwise. intdiv() always gives a whole-number quotient.

Booleans and truthiness

In a condition, these values count as false: false, 0, 0.0, "" (empty string), "0", [] (empty array), and null. Everything else is true.

PHP code

<?php
$tests = ["", "0", "0.0", "hello", 0, [], [0]];

foreach ($tests as $value) {
    echo var_export($value, true) . " is " . ($value ? "true" : "false") . "<br>";
}

What the browser shows

” is false
‘0’ is false
‘0.0’ is true
‘hello’ is true
0 is false
array (
) is false
array (
0 => 0,
) is true

Note that "0.0" is true, but "0" is false. This is a PHP quirk. When it matters, compare explicitly instead of relying on truthiness.

null

null means “no value”. A variable is null if you assign it, if it was never set, or after unset().

PHP code

<?php
$x = null;

echo is_null($x) ? "x is null. " : "x has a value. ";
echo isset($x) ? "isset: yes" : "isset: no";

Output

x is null. isset: no

isset() returns false for null as well as for undefined variables.

Type conversion (casting)

PHP converts types automatically in some situations (called type juggling). You can also convert on purpose with a cast in brackets.

PHP code

<?php
$text = "25";

var_dump((int) $text + 5);
var_dump((float) "3.7");
var_dump((string) 99);
var_dump((bool) "hello");
var_dump((int) "abc");
var_dump((int) 7.9);

What you should see (inside <pre>)

int(30)
float(3.7)
string(2) "99"
bool(true)
int(0)
int(7)

Casting a decimal to int cuts off the decimal part without rounding. Casting non-numeric text to int gives 0. Use round() when you want rounding.

Strict comparison

== converts types first, === does not. As in JavaScript, prefer ===.

PHP code

<?php
var_dump(5 == "5");
var_dump(5 === "5");
var_dump(0 == "abc");
var_dump(0 === "abc");

What you should see (inside <pre>)

bool(true)
bool(false)
bool(false)
bool(false)

Version note: in PHP 8, 0 == "abc" is false. In PHP 7 and earlier it was true, which caused real bugs. This is another reason to use === and a current PHP version.

Everything that arrives from a form or the address bar is a string, even if it looks like a number. Convert and check it before using it in calculations.

Common mistakes

  • Using == and being surprised by automatic conversion.
  • Assuming "0.0" is false.
  • Casting text to a number without checking it was a number.
  • Forgetting that float arithmetic can be slightly inexact, such as 0.1 + 0.2.
  • Trusting form data to be the type you expect.

Exercise

$qty = "3"; and $price = "4.5"; arrive as text. Convert them and print the total with two decimals, using number_format().

Show solution
<?php
$qty = "3";
$price = "4.5";

$total = (int) $qty * (float) $price;
echo number_format($total, 2);

The output is 13.50.

Knowledge check

1. Which two PHP types hold numbers?

int and float.

2. What does (int) "abc" give?

0

3. Which comparison operator checks type as well as value?

===

Summary

  • Main types: string, int, float, bool, array, object, and null.
  • Check with gettype() and var_dump(), and convert with casts.
  • Use ===, remember the falsy values, and treat outside data as text.
Scroll to Top