Variables & Data Types — PHP

A variable (a named container for a value) in PHP starts with $ and is filled with =, without writing its type (the kind of value). Upper and lower case in…

The price of Fried Rice, 15, appears in three places in your file: on the menu, on the receipt, and in the total. Tomorrow it goes up to 17, and you have to find all three, one by one. If you miss one, the total on the receipt is wrong.

It's easier to store the price once in a variable, a named container whose content can be used again anywhere. When you change the price in that one place, every place that uses it changes too. Picture Mrs. Tini's order book: every line has a label (menu, quantity, price) and what's written next to it (Fried Rice, 2, 15). The label in the book is the variable's name, and what's written next to it is the variable's value. Every value also has a type: text, a whole number, a decimal, yes or no, or nothing. The type decides what you can do with the value, such as add it up or join it to other text.

Making a variable

These are three variables for one order, and then echo writes the value of one of them:

<?php
$menu = "Fried Rice";
$quantity = 2;
$price = 15;

echo $menu;

Five basic types

Type Example at the stall Written as
string a menu item's name "Fried Rice"
int quantity, price 2, 15
float a 10% discount 0.1
bool spicy or not true, false
null a note not filled in yet null

Seeing the type: var_dump and gettype

echo only writes the value. To find out the type, use var_dump or gettype. Both are built-in functions of PHP, which means ready-made commands. You write the value to check inside brackets after the name, for example var_dump($menu).

<?php
$menu = "Fried Rice";
$discount = 0.1;
$spicy = true;
$note = null;

var_dump($menu);
var_dump($discount);
var_dump($spicy);
var_dump($note);
echo gettype($spicy);

The result in the terminal:

$ php stall.php
string(10) "Fried Rice"
float(0.1)
bool(true)
NULL
boolean

Building a sentence from variables

This code writes one sentence that uses two variables:

<?php
$name = "Budi";
$age = 25;

echo "My name is $name, I am $age years old.";
My name is Budi, I am 25 years old.

PHP changes types on its own

Before looking at the result, guess: does $x = "5" + 3; echo $x; print 8, 53, or an error?

In this code, $quantity holds the string "2". A number stored as a string is common in PHP. The reason is forms (the input boxes on a web page): what someone types into a form reaches the server as a string, even when it is a number.

<?php
$price = 15;
$quantity = "2"; // a string, say from a form

var_dump($price * $quantity);
var_dump("5" + 3);
var_dump("5" . 3);
int(30)
int(8)
string(2) "53"

⚠️ Small typos in variable names and values

TL;DR: A variable (a named container for a value) in PHP starts with $ and is filled with =, without writing its type (the kind of value). Upper and lower case in its name matter. The basic types are string (text in quotes), int (a whole number), float (a decimal with a dot), bool (true/false) and null (no value yet). var_dump writes the type and the value, gettype answers the type's name (string, integer, double, boolean, NULL). Inside double quotes, a variable such as $name is replaced by its value. In maths, PHP turns a string holding a number into a number, so "5" + 3 gives 8. To join text, use the dot.

⚡ A variable starts with $

Write:

<?php
$name = "Budi";
$age = 25;

echo "My name is $name, I am $age years old.";

Meaning: A variable is a named container for a value. Its name always starts with $, and = stores the value on the right in the variable on the left. Inside double quotes, $name and $age are replaced by their values. So the code writes My name is Budi, I am 25 years old.

Numbers such as ages and prices go without quotes: $age = 25;. Written as "25", it's a string (text), not a number.

⚡ See the type with var_dump

Write:

<?php
$menu = "Fried Rice";
$spicy = true;

var_dump($menu);
var_dump($spicy);
Guess first: what does echo true; write?

1. But echo false; and echo null; (null means no value yet) write nothing at all. That's why you inspect a value with var_dump.

Meaning: Every value has a type (its kind). var_dump writes the type, then the value: string(10) "Fried Rice" (text) and bool(true) (yes or no). The number in brackets after string is the length of the text, the space included.

⚡ + always does maths

Guess first: what does "5" + 3 give?

8, not 53 as in JavaScript. PHP turns a string (text) that holds a number into a number first.

Meaning: +, -, * and / always do maths. To join text, PHP uses the dot: "5" . 3 gives "53".

What you will learn