01 · THE IDEAREAD → TRY → LEARN
What a type tells you
Every expression in PHP has a type, and the type decides which operations make sense. PHP is dynamically typed, so you do not have to declare a type for each variable. The type is worked out at runtime from the current value. This lesson covers the four scalar types, which hold a single simple value:
intfor whole numbers such as42or0floatfor numbers with a fractional part such as3.5stringfor text such as"hello"or"42"boolfortrueorfalse
null is a separate type that means "no value". Arrays and objects are compound types and are not scalars.
Inspecting a type
There are two main ways to ask about a type. get_debug_type() returns a readable name for the value, and var_dump() shows both the type and the value. When you only need a yes-or-no answer, use the is_int(), is_float(), is_string() and is_bool() family.
The example below is the worked example for this lesson. It walks over four values and prints the type name of each one.
$values = [42, 3.5, "42", true];
foreach ($values as $value) {
echo get_debug_type($value), "\n";
}int float string bool
The integer 42 and the string "42" look alike when printed, but they are different types. That difference affects how they behave in later operations.
Automatic conversion
When you use a value in an operation its type does not support, PHP tries to convert it to a type that works. This is called type juggling, and the result depends on the operator. The + operator wants numbers, so a numeric string is treated as a number. The . operator wants strings, so a number is turned into text.
$total = "5" + 3;
$text = "5" . 3;
var_dump($total);
var_dump($text);int(8) string(2) "53"
The first result is the integer 8. The second is the two-character string "53". Same inputs, different operators, different types out. If you want to know which type you ended up with, check it with var_dump() rather than assuming.
Casts and floating-point care
A type cast forces a conversion: (int), (float), (string) and (bool). Casting is explicit, so a reader can see that you meant to convert. Two details matter in practice:
- Casting a string that starts with digits to
intreads the leading number, so(int) "12abc"gives12. - The string
"0"casts tofalse. Many other non-empty strings cast totrue.
Floats are stored in binary, so some decimal fractions cannot be represented exactly. Adding 0.1 and 0.2 does not give exactly 0.3. Compare floats by checking that the difference is smaller than a small tolerance, not with ===.
$price = 0.1 + 0.2;
echo json_encode([
'same' => $price === 0.3,
'close' => abs($price - 0.3) < 0.00001,
'int' => (int) "12abc",
'bool' => (bool) "0",
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";{"same":false,"close":true,"int":12,"bool":false}
Common mistakes
- Assuming
"42"and42are interchangeable. They may compare or print the same way in some contexts, but they are different types. - Testing a value for truthiness when you really need a type check.
0,""andfalseare all falsey, yet they are three different kinds of value. - Using
gettype()output whereget_debug_type()names are expected. The older function returns names such asintegeranddouble, whileget_debug_type()returnsintandfloat.
Exercise contract
Write a function scalarKind($value) that returns one of five strings:
"int"for integers"float"for floats"string"for strings, including the empty string"bool"for booleans, includingfalse"other"for anything else, such asnullor an array
The function must look at the real type of the value, not at whether it is truthy. The tests include 0, "" and false as boundary inputs, so a solution built on if ($value) will fail them. Start with the is_* functions and handle each type in turn.
Recap
PHP has four scalar types: int, float, string and bool. Use get_debug_type() and var_dump() to see what you have, and the is_* functions to test it. Remember that operators may convert values automatically, and that casts make a conversion explicit. Treat float comparisons with a tolerance, and do not mistake a falsey value for a different type.
Variables
- Distinguish the basic scalar types
- Use values with the intended type