Introduction
A program must remember things: a name, a price, a count, a yes or no answer. PHP keeps them in variables. A variable is a named box that holds one value. PHP is a dynamically typed language, which means you do not write the type when you create a variable. PHP looks at the value and decides the type itself, and the same variable can later hold a different type. This is easy for beginners, but it also means you must know the types well, because PHP converts between them quietly.
How it works
A variable name starts with $, then a letter or underscore, then letters, digits and underscores. Names are case sensitive: $Age and $age are two different variables. You create a variable by giving it a value with =.
PHP has four simple types you will use all the time:
- int: a whole number such as
21or-5. - float: a number with a decimal point such as
19.99. - string: text such as
"Ada". - bool:
trueorfalse.
There is also null, which means "no value", and array, which you will meet in a later lesson. The function gettype($x) returns the type name, and var_dump($x) prints the type together with the value. It is the best tool for checking what a variable really holds.
When types are mixed, PHP converts them. The expression "5" + 3 gives the integer 8, because the text "5" looks like a number. You can also convert on purpose with a cast: (int) "12abc" gives 12, and (int) 3.99 gives 3, because a cast to int cuts off the decimals and does not round.
Division needs care. The / operator gives an int only when the result is exact, and otherwise a float, so 7 / 2 is 3.5. If you want whole-number division, use intdiv(7, 2), which gives 3. The operator % gives the remainder.
Floats are stored in binary, so some decimals are not exact. The test 0.1 + 0.2 == 0.3 is false. When you compare floats, check that the difference is very small instead.
A constant is a value that never changes. You create one with const MAX = 100; and use it without a dollar sign. In this course integers are 32-bit, so the largest int is 2147483647. Keep numbers below that.
Example
<?php
$age = 21;
$price = 19.99;
$name = "Ada";
$active = true;
$nothing = null;
echo gettype($age), " ", gettype($price), " ", gettype($name), " ", gettype($active), " ", gettype($nothing), "\n";
echo $age + $price, "\n";
var_dump($age, $price, $name, $active, $nothing);
$count = "5" + 3;
var_dump($count);
var_dump(7 / 2);
var_dump(intdiv(7, 2));
var_dump((int) "12abc");
var_dump(0.1 + 0.2 == 0.3);
const MAX = 100;
echo MAX, "\n";Line by line
- Five variables are created: an int, a float, a string, a bool and null.
gettypeprints the type name of each. PHP calls a floatdouble.$age + $pricemixes an int and a float, and the result is a float.var_dumpprints each type with its value."5" + 3converts the text to a number, so the result is int 8.7 / 2is a float, whileintdiv(7, 2)is a whole number.(int) "12abc"reads the digits at the start and stops at the letters.- The comparison of
0.1 + 0.2with0.3is false because of binary rounding.
Expected result
integer double string boolean NULL
40.99
int(21)
float(19.99)
string(3) "Ada"
bool(true)
NULL
int(8)
float(3.5)
int(3)
int(12)
bool(false)
100
In the real world
Real programs read prices, ages and dates from forms and files, and everything arrives as text first. Knowing when PHP converts a string to a number protects you from wrong totals. Shops store money as whole cents in an int to avoid float errors, and only show it with two decimals when printing.
Common mistakes
- Forgetting the dollar sign on a variable name.
- Mixing up
=and==. A single=stores a value; double==compares values. - Trusting float equality, as in
0.1 + 0.2 == 0.3. - Expecting
(int) 3.99to give 4. It gives 3 because a cast cuts the decimals.
Best practices
- Use clear names such as
$totalPriceinstead of$tp. - Use
var_dumpwhen you are not sure what a variable holds. - Use
===instead of==when the type matters. - Store money in whole cents when exact results matter.
Summary
A variable starts with $ and holds a value of type int, float, string, bool or null. PHP chooses the type from the value and converts types when needed. Casts such as (int) change a type on purpose. The / operator can give a float, intdiv gives a whole number, and floats are not always exact. Use const for fixed values.
Quick check
Key takeaways
- Variables start with a dollar sign and PHP chooses their type from the value.
- Casts and automatic conversion change types; the slash can give a float and intdiv gives a whole number.
- Floats are not always exact, so compare them with care.
Practice problems for this lesson
Was this lesson clear?
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.