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

PHP Data Types

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
$age = 30;
$id = -5;
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
$text = "Hello, world!";
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
$price = 19.99;
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
$isActive = true;
var_dump($isActive);
if ($isActive) {
    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
$cart = array("Apple", "Bread", "Milk");
$cart2 = ["Apple", "Bread", "Milk"];
var_dump($cart);
?>

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.