Data Types

PHP · Lesson 5 of 50 6 min read Updated

Every value in PHP has a type: a whole number, a piece of text, a yes-or-no answer and so on. The type decides what you can do with the value. You can multiply two numbers, but you cannot multiply two names.

In this lesson you will meet PHP's data types, learn how to check them, and see how PHP converts one type into another, which is a common source of beginner bugs.

The main types

PHP has a small set of built-in types. You will use these every day:

TypeHoldsExample
intWhole numbers72, -5, 0
floatNumbers with a decimal point1245.5, 0.18
stringText'Lucknow', "Hello"
booltrue or falsetrue
arrayA list or map of values['A1', 'B1']
nullNo value at allnull
objectAn instance of a classnew DateTime()

Types such as int and string, which hold one simple value, are called scalar types. Arrays and objects can hold many values. You may also see the word resource in older code: it was used for things like open files and database connections, and modern PHP has mostly replaced it with objects.

PHP
<?php
declare(strict_types=1);

$seats = 72;
$fare = 1245.50;
$station = 'Lucknow';
$isAc = true;
$coaches = ['A1', 'B1'];
$pnr = null;

var_dump($seats, $fare, $station, $isAc, $coaches, $pnr);
Output
int(72)
float(1245.5)
string(7) "Lucknow"
bool(true)
array(2) {
  [0]=>
  string(2) "A1"
  [1]=>
  string(2) "B1"
}
NULL

var_dump() shows every value with its type. Notice that the float 1245.50 is shown as 1245.5: the trailing zero is not stored. For prices you usually format the number when you print it, as you will see in Numbers & Math.

Checking a type

To get a type's name as text, use get_debug_type(). It returns the same short names you write in code (int, float, bool). The older gettype() returns longer, older names such as integer, double and boolean, which is why the newer function is preferred.

To ask a yes-or-no question about a type, use the is_ functions: is_int(), is_float(), is_string(), is_bool(), is_array() and is_null().

One special check is is_numeric(). It tells you whether a value is a number or a string that looks like a number. This is exactly what you need for form input, because everything that comes from a form arrives as a string.

PHP
<?php
declare(strict_types=1);

echo get_debug_type(19), "\n";
echo get_debug_type(19.5), "\n";
echo get_debug_type('19'), "\n";

var_dump(is_numeric('42'));
var_dump(is_numeric('4.5'));
var_dump(is_numeric('42abc'));
var_dump(is_numeric('1,000'));
Output
int
float
string
bool(true)
bool(true)
bool(false)
bool(false)

Note the last line. '1,000' is not numeric to PHP, because the comma is not part of a number. If users type commas, remove them before you check.

Booleans and truthy values

A bool is either true or false. Conditions in if statements need a boolean, but PHP will accept any value and convert it. Values that become false are called falsy. There are only a few:

  • false itself
  • 0 and 0.0
  • the empty string '' and the string '0'
  • an empty array []
  • null

Every other value is truthy, including strings like '0.0', 'false' and ' ' (a single space).

PHP
<?php
declare(strict_types=1);

var_dump((bool) 0);
var_dump((bool) '0');
var_dump((bool) []);
var_dump((bool) '0.0');
var_dump((bool) 'false');
var_dump((bool) ' ');
Output
bool(false)
bool(false)
bool(false)
bool(true)
bool(true)
bool(true)

The string '0' being falsy surprises many people. If a student scores 0 and the form sends '0', a plain truthiness check will treat it as "no answer". In if, else & elseif you will learn to write exact checks instead.

Automatic type conversion

PHP converts types automatically when an operator needs a different type. This is called type juggling. Arithmetic operators turn numeric strings into numbers, and the dot operator . turns numbers into strings:

PHP
<?php
declare(strict_types=1);

$fromForm = '10';
var_dump($fromForm + 5);
var_dump('2.5' * 2);

$a = 5;
$b = 5;
var_dump($a . $b);
Output
int(15)
float(5)
string(2) "55"

PHP 8 is stricter than older versions when a string is not a clean number:

  • '10 apples' + 5 gives 15, but also shows the warning "A non-numeric value encountered".
  • 'apples' + 5 throws a TypeError with the message "Unsupported operand types: string + int", which stops the script.

declare(strict_types=1); does not stop this juggling inside operators. It only affects values passed to functions, as you saw in PHP Syntax & echo.

Casting: converting on purpose

When you want a conversion, do it on purpose with a cast. Write the target type in brackets before the value: (int), (float), (string), (bool) or (array).

PHP
<?php
declare(strict_types=1);

var_dump((int) '42');
var_dump((int) 42.9);
var_dump((int) '12 apples');
var_dump((int) 'apples');
var_dump((float) '99.50');
var_dump((string) 3.0);
Output
int(42)
int(42)
int(12)
int(0)
float(99.5)
string(1) "3"

Casting never shows a warning, so it can hide bad input. (int) 42.9 cuts off the decimals rather than rounding, and (int) 'apples' silently becomes 0. Check input with is_numeric() or a validation filter first, then cast. You will build proper checks in Form Validation & Escaping.

Common mistakes

  • Treating '0' as "empty". '0' is falsy, so a score of zero can look like a missing value. Compare exactly instead.
  • Trusting casts on user input. (int) 'abc' is 0, not an error. Validate first.
  • Using gettype() and expecting float. It returns double. Use get_debug_type().
  • Forgetting that form data is always a string. Even a number typed into a form arrives as '25', not 25.

Key points

  • The main types are int, float, string, bool, array, null and object.
  • var_dump() shows a value with its type; get_debug_type() returns the type name.
  • Falsy values are false, 0, 0.0, '', '0', [] and null; everything else is truthy.
  • PHP juggles types in operators: numeric strings become numbers in maths, numbers become strings with ..
  • Casts such as (int) convert on purpose but never complain, so validate input first.

Exercise

A form sends three values as strings: $qty = '3', $price = '249.50' and $coupon = '0'. Convert the quantity to an int and the price to a float with casts, multiply them to get a total, and show the total with var_dump(). Then use var_dump() and a (bool) cast to show whether $coupon is truthy, and add a comment explaining the surprising result.

Show solution
PHP
<?php
declare(strict_types=1);

$qty = '3';
$price = '249.50';
$coupon = '0';

$total = (int) $qty * (float) $price;
var_dump($total);

// '0' is one of the falsy strings, so this is false even though a value was sent.
var_dump((bool) $coupon);
Output
float(748.5)
bool(false)

Multiplying an int by a float gives a float. The coupon string '0' is falsy, so check it with $coupon !== '' if you want to know whether anything was entered.

Quick Quiz

1. What does get_debug_type(3.5) return?

2. Which of these values is truthy?

3. What is the result of (int) '12 apples'?

4. What does 'apples' + 5 do in PHP 8?

Learn PHP with a mentor

Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call