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;
- A variable's name always starts with a dollar sign
$, and the$is written every time the variable is used. =stores the value on the right in the variable on the left. You can replace the value at any time:$quantity = 3;.- After the
$, a name starts with a letter or an underscore_, may hold digits, and has no spaces:$unitPriceor$unit_price. - Upper and lower case are different:
$menuand$Menuare two separate variables. - There's no
letorconstas in JavaScript, and the type isn't written: PHP decides the type from the value you store.
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 |
- A
stringis text, written inside quotes. Anintis a whole number, and afloatis a decimal number. Aboolhas only two values,true(yes) orfalse(no).nullmeans "no value yet". - Decimals in code use a dot:
0.1, not0,1as some countries write it. - A number inside quotes,
"15", is a string, not an int. - Arrays, the type for many values at once, come in the Array lesson.
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
var_dumpwrites the type, then the value. For a string, the number in brackets is its length: "Fried Rice" has 10 characters, the space included.gettypegives the type's name as a string, using the old names:"string","integer","double"(for float),"boolean","NULL". Sogettype("Budi")answersstringandgettype(true)answersboolean.- PHP 8 also has
get_debug_type, which uses the modern names:int,float,bool,null.
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.
- Inside double quotes,
$nameand$ageare replaced by their values. This replacing is called interpolation. Inside single quotes it doesn't happen (more in the String lesson). - Another way: join pieces of text with a dot (
.).echo "My name is " . $name . ", I am " . $age . " years old.";gives the same result.
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"
+,-,*and/always do maths. A string that holds a number is turned into a number first. This automatic change of type is called type juggling. So"5" + 3gives8, not53as in JavaScript.- To join text, PHP uses a different symbol, the dot:
"5" . 3gives"53". - Text that isn't a number can't be used in maths: at
"Rice" + 1, PHP stops with the errorUncaught TypeError: Unsupported operand types: string + int. - Text that starts with a number, such as
"5 apples" + 3, still gives8. PHP only gives the warningWarning: A non-numeric value encountered. So input like "2 portions" is still accepted and counted as 2.
⚠️ Small typos in variable names and values
- Forgetting the
$.name = "Budi";ends inParse error: syntax error, unexpected token "=". - Upper and lower case. The variable is
$name, but you wrote$Name: PHP givesWarning: Undefined variable $Name, then writes nothing in that place. - A number inside quotes.
$age = "25";makes a string, andvar_dumpwritesstring(2) "25". Write ages and prices without quotes:$age = 25;. - Checking a value with echo.
echo true;writes1, whileecho false;andecho null;write nothing at all. To inspect a value, usevar_dump.
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".