PHP Types: Strings, Ints, Floats, Bools, Null, and Arrays
Lesson 4 of 43 in the PHP 8 learning path.
What are the core PHP types and how do I inspect them?
Every value in a grade book has a type. A lesson title is a string, a seat count is an int, a class average is a float, a section flag is a bool, a missing note is null, and the list of tracks is an array. get_debug_type() reports those names in the form PHP uses in type errors: string, int, float, bool, null, and array. gettype() is the older reporter and still says double for a float and integer for an int, which is why the sample prints both for the average.
Loose comparison with == converts operands before it decides. In this file, 24 == "24" is true and 24 === "24" is false. null == false is also true, which will mark a missing score as a failing zero if you use the loose operator by accident. Use === when the type is part of the decision, and strict_types for function calls.
Syntax & Parameters
get_debug_type(mixed $value): string
Example
<?php
declare(strict_types=1);
/**
* @return array<string, mixed>
*/
function gradeBookValues(): array
{
return [
'title' => 'Grade book',
'seats' => 24,
'average' => 88.5,
'open' => true,
'note' => null,
'tracks' => ['web', 'data'],
];
}
function describe(string $label, mixed $value): string
{
return $label . '=' . get_debug_type($value);
}
$values = gradeBookValues();
foreach ($values as $label => $value) {
echo describe($label, $value) . "\n";
}
echo 'average_gettype=' . gettype($values['average']) . "\n";
echo 'loose=' . (24 == '24' ? 'equal' : 'not-equal') . "\n";
echo 'strict=' . (24 === '24' ? 'equal' : 'not-equal') . "\n";
echo 'null_loose=' . (null == false ? 'equal' : 'not-equal') . "\n";
echo 'zero_word=' . (0 == 'foo' ? 'equal' : 'not-equal') . "\n";
Expected Output
title=string seats=int average=float open=bool note=null tracks=array average_gettype=double loose=equal strict=not-equal null_loose=equal zero_word=not-equal
Line-by-Line Walkthrough
- declare(strict_types=1) is on, and the comparisons below still use == and === because strict_types does not change operators.
- gradeBookValues() returns one array that holds a string, an int, a float, a bool, null, and a nested array.
- describe() calls get_debug_type() so each label is printed with the type name PHP 8 uses in error messages.
- The foreach walks the array by key and prints title=string, seats=int, average=float, open=bool, note=null, and tracks=array.
- gettype() on the average prints double, which is the older name for the same float value.
- 24 == "24" is equal because loose comparison converts the numeric string to an int before the check.
- 24 === "24" is not-equal because === requires the same type and the same value.
- null == false is equal, so a missing note would pass a loose check that was meant to test a real boolean.
- 0 == "foo" is not-equal on PHP 8, which closed the old coercion that treated some non-numeric strings as zero.
- The printed lines are the inspection result a debugger would show before any value is saved to the grade book.
Common Pitfalls & Security
Type confusion changes a grade or an authorization flag without throwing. Loose comparison is convenient for numeric strings and dangerous for null, false, and zero. Inspect the value with get_debug_type() when a bug report says the field "looks right" on screen. The screen has already cast it to text.
- == will say that 24 and "24" are equal. A seat count that arrived as text from $_POST is still a string until you validate it.
- null == false and 0 == false are both true. A missing score and a real zero need === or a dedicated null check before they share a branch.
- gettype() reports double and integer. Matching those strings in new code fights the names get_debug_type() and the type system already use.
- count(null) throws in PHP 8. A missing roster should be an empty array or an explicit null check, not a value you count blindly.
- (string) casts are fine at a boundary you control. Casting a request body and then concatenating it into SQL is still SQL injection (SQLi).
- Unsafe anti-pattern (do not ship): if ($score == null) to mean "no score was entered." Use $score === null.
Best Practices & Modern Standards
Prefer get_debug_type() when you log a value on PHP 8.3 or newer, and prefer === when a branch depends on null, false, or a distinction between 24 and "24". Declare parameter types so a string seat count fails at the function call once the file uses strict_types, instead of failing later as a wrong average.
Model a missing note as null or as an omitted array key, and test it with === null or isset(). Keep averages in floats only when a fraction is real. Seat counts and point totals belong in ints so rounding policy stays in one place.
Real-World Use
A grade-book import row contains a title, a seat count, an average, an open flag, an optional note, and a list of tracks. The loader prints get_debug_type() for each column while the instructor checks the file. A seat count that shows up as string is rejected or cast only after a numeric check, and a missing note stays null instead of becoming the boolean false that == would accept.
Frequently Asked Questions
Why does gettype() say double for 88.5?
gettype() keeps the historical name double. get_debug_type() says float, which matches float parameters and TypeError text. Prefer get_debug_type() in logs and teaching output.
Does declare(strict_types=1) make == strict?
No. strict_types changes scalar checks on calls and returns in that file. The operators == and === behave the same either way. Use === when the type is part of the decision.
Is 0 equal to the string foo in PHP 8?
No. PHP 8 compares a number and a non-numeric string as strings, so 0 == "foo" is false. 0 == "0" remains true under ==. === is still the clear choice.
What type is a missing array key?
Reading a missing key warns and yields null on current PHP if you suppress nothing. Test with isset() or null coalescing before you treat the value as a string or an int.