Data Types
A toolbox has separate trays for screws, nails, and bolts. They're all small metal things, but you'd never use one where you need another.
Every value in PHP has a type: text, a whole number, a decimal, true or false, and so on. The type decides what you can do with the value. You can multiply two numbers, but multiplying two names makes no sense.
In PHP, you don't write the type when you create a variable. PHP looks at the value and works it out.
The toolbox
Here are the types you'll use most, with PHP's name for each:
| Type | PHP name | Examples |
|---|---|---|
| Text | string | "Hello", 'Maria', "" |
| Whole number | int | 42, -7, 0 |
| Decimal number | float | 3.14, -0.5, 2.0 |
| True or false | bool | true, false |
| No value at all | null | null |
| A list of values | array | [1, 2, 3] |
Arrays get their own lessons, starting with Indexed Arrays. This lesson covers the first five.
Checking a type
echo shows a value, but not its type. For that, PHP has two handy tools:
get_debug_type($value)gives the type's name as a string.var_dump($value)prints the type and the value. It's the best tool for looking inside a variable while you're debugging.
<?php
var_dump("Maria");
var_dump(17);
var_dump(1.75);
var_dump(true);
var_dump(null);
echo get_debug_type(17), "\n";string(5) "Maria"
int(17)
float(1.75)
bool(true)
NULL
intFor a string, var_dump also shows its length: "Maria" has 5 characters.
Strings
A string is text, in single or double quotes, as you saw in Your First PHP Script. A string can hold digits too, but "42" in quotes is text, not a number. You'll learn much more in Working with Strings.
Integers and floats
PHP has two kinds of numbers:
- An int (integer) is a whole number, with no decimal point:
10,-3. - A float (floating-point number) has a decimal point:
10.5,-3.0.
Dividing with / gives a float whenever the answer isn't whole. For whole-number division, use intdiv(), and for the remainder, %:
<?php
var_dump(10 / 2);
var_dump(10 / 4);
var_dump(intdiv(10, 4));
var_dump(10 % 4);int(5)
float(2.5)
int(2)
int(2)You can write long numbers with underscores to make them readable. PHP ignores them: 1_000_000 is one million.
A float surprise. Computers store decimals in binary, so some simple-looking decimals can't be stored exactly. It's the same in almost every programming language:
<?php
var_dump(0.1 + 0.2);
var_dump(0.1 + 0.2 == 0.3);float(0.30000000000000004)
bool(false)The tiny error is harmless for most things, but it matters for money. That's why shops and banks usually count in whole cents (an int), not in pesos with decimals.
Booleans
A bool is either true or false, nothing else. You'll get them from comparisons, like $age >= 18, and use them to make decisions in Making Decisions.
Watch out when you echo a boolean. true prints as 1, and false prints as nothing at all:
<?php
$isOpen = true;
$isFull = false;
echo "Open: $isOpen\n";
echo "Full: $isFull\n";
var_dump($isFull);Open: 1
Full:
bool(false)That's a good reason to use var_dump whenever you're not sure what a variable holds.
Null
null means "no value". A variable holds null when you set it that way on purpose, for example because something hasn't happened yet:
<?php
$winner = null;
var_dump($winner);
var_dump(is_null($winner));NULL
bool(true)You can check for each type with a matching function: is_string(), is_int(), is_float(), is_bool(), and is_null(). Each one returns true or false.
A variable's type can change
Since the type comes from the value, putting a new kind of value in a variable changes its type:
<?php
$answer = 42;
echo get_debug_type($answer), "\n";
$answer = "forty-two";
echo get_debug_type($answer), "\n";int
stringThat's allowed, but it's usually confusing. Keep each variable to one type. Later, in Parameters, Types, and Return Values, you'll see how to make PHP check types for you.
Try it
A student's record, in several types. Predict every line, including the types and the lengths.
Running PHP here
Running PHP right in the browser is coming to Syntaxia soon. For now, you can read and edit the code here. If you have PHP installed on your computer (see Setting Up), save it as index.php and run it with php index.php.
Check your prediction
string(3) "Ana"
int(17)
float(160)
Enrolled: 1
NULL
float
string$height * 100 is float(160): a float that happens to be whole is still a float. 7 / 2 isn't whole, so it's a float too, and "16" in quotes is a string, even though it looks like a number.
Try it yourself
- Change
$isEnrolledtofalse. What does the Enrolled line print now? - Use
var_dumpon8 / 2and on9 / 2. Why are the types different? - Set
$club = "Robotics";andvar_dumpit again. What's its length?
Check your understanding
Up next
You know the types. Now let's calculate and compare with them, in Operators and Expressions.
