← Back to PHP Course | Chapter 1: Introduction & Basics | Lesson 9 of 13

PHP Data Types

Data types are the different kinds of information PHP can hold, like whole numbers, decimals, words, and yes-or-no answers. It is like sorting toys into different boxes because a ball and a puzzle are not the same thing.
Syntax
php
$integer = 42;
$float = 3.14;
$string = "text";
$boolean = true;
$array = [item1, item2];
var_dump($variable);   // shows type and value

Integers

An integer holds a whole number with no decimal component, positive or negative — counts, IDs, ages.

PHP stores integers in a fixed-size binary format, which is why extremely large numbers can silently overflow into a float; for most everyday values this never matters.

Example: Integers

php
<?php
// Declare `$age`, set to `30`
$age = 30;
// Declare `$id`, set to `-5`
$id = -5;
// Print a detailed dump (with types) of `$age`
var_dump($age);
?>

Strings

A string is a sequence of characters — text — and must be wrapped in either single or double quotes so PHP knows where it starts and ends.

Strings are PHP's most-used type in web work, since HTML output, form data, and database results are all fundamentally text.

Example: Strings

php
<?php
// Declare `$text`, set to "Hello, world!"
$text = "Hello, world!";
// Print a detailed dump (with types) of `$text`
var_dump($text);
?>

Floats

A float (also called a double) stores a number with a decimal point, or one large/small enough to need exponential notation.

Floats are essential for anything involving fractional values — prices, measurements, averages — where an integer would lose precision.

Example: Floats

php
<?php
// Declare `$price`, set to `19.99`
$price = 19.99;
// Print a detailed dump (with types) of `$price`
var_dump($price);
?>

Booleans

A boolean holds exactly one of two values, true or false, and is the backbone of every conditional check in your code. Almost every if statement ultimately reduces its condition down to a boolean before deciding which branch to run.

Example: Booleans

php
<?php
// Declare `$isActive`, set to `true`
$isActive = true;
// Print a detailed dump (with types) of `$isActive`
var_dump($isActive);
// Check whether `$isActive`
if ($isActive) {
    // Print "Every if reduces to a boolean." to the output
    echo "Every if reduces to a boolean.";
}
?>

Arrays

An array stores multiple values under a single variable name, either as a numerically indexed list or as key-value pairs.

You can build one with the array() function or the shorter [] syntax, and arrays are how PHP represents everything from a shopping cart to a database result row.

Example: Arrays

php
<?php
// Declare `$cart`, set to `array("Apple", "Bread", "Milk")`
$cart = array("Apple", "Bread", "Milk");
// Declare `$cart2` as an array: `["Apple", "Bread", "Milk"]`
$cart2 = ["Apple", "Bread", "Milk"];
// Print a detailed dump (with types) of `$cart`
var_dump($cart);
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Storing a number in quotes, like $age = "25";, which makes it a string; var_dump shows string(2) rather than int(25).
  2. Comparing floats with ==, for example 0.1 + 0.2 == 0.3, which is false because of binary floating-point rounding.
  3. Assuming "false" (a non-empty string) is falsy, when only "" and "0" are false; if ("false") runs the block.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.