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
$age = 30;
$id = -5;
var_dump($age);
?>
Login to try C/C++/Java/PHP code in the editor
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
$text = "Hello, world!";
var_dump($text);
?>
Login to try C/C++/Java/PHP code in the editor
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
$price = 19.99;
var_dump($price);
?>
Login to try C/C++/Java/PHP code in the editor
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
$isActive = true;
var_dump($isActive);
if ($isActive) {
echo "Every if reduces to a boolean.";
}
?>
Login to try C/C++/Java/PHP code in the editor
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
$cart = array("Apple", "Bread", "Milk");
$cart2 = ["Apple", "Bread", "Milk"];
var_dump($cart);
?>
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: