PHP data types interview questions, with answers
PHP is loosely typed: a variable's type follows its value, and many operators convert their operands automatically. That makes PHP quick to write and easy to get subtly wrong, which is why interviews for PHP roles start with types and comparisons. The questions test whether you know when PHP converts a value, how PHP 8 changed comparisons between numbers and strings, and which operators to use for missing values.
The answers below cover the types, strict and loose comparison, type juggling, isset and empty, the null coalescing and spaceship operators, and number limits, with code run on PHP 8.5. Then take the free PHP diagnostic — ten questions across every PHP topic in the bank — to see which of these you can explain but not yet predict.
The questions, with answers
1.What are PHP's data types?
In short: Scalars (int, float, string, bool), compound types (array, object, callable, iterable), and the special types null and resource.
PHP has four scalar types: int, float, string and bool. The compound types are array, an ordered map that serves as both list and dictionary, object, and the pseudo-types callable and iterable used in declarations. The special types are null, the single value of a variable with no value, and resource, a handle to an external thing such as an open file. A variable has no fixed type; it takes the type of whatever it holds. get_debug_type, added in PHP 8, reports a value's type in the same spelling as type declarations, which gettype does not.
$vals = [7, 2.5, "a", false]; $types = array_map( "get_debug_type", $vals); echo implode(" ", $types); // int float string bool
2.What is the difference between == and === in PHP?
In short: == compares values after converting types where needed; === is true only when both the value and the type are the same.
== is loose comparison: if the operands have different types, PHP converts them following its comparison rules and then compares, so 0 == false is true because false converts to 0. === is strict comparison and never converts: the operands must have the same type and value, so 0 === false is false. String comparison with == is case-sensitive, so "abc" == "ABC" is false; strcasecmp compares ignoring case. The usual advice is to use === by default, and especially when checking return values that can be both 0 and false, and to use == only when a conversion is really wanted.
var_dump(0 == false); // bool(true) var_dump(0 === false); // bool(false) var_dump("abc" == "ABC"); // bool(false)
3.How does type juggling work in PHP 8 comparisons?
In short: Numeric strings compare as numbers; a number compared with a non-numeric string is converted to a string first, a PHP 8 change.
When == compares a number with a string, PHP first checks whether the string is numeric, meaning it looks like a number such as "42", "1.5" or "1e3". If it is, both are compared as numbers, which is why "1e3" == "1000" is true: both strings are numeric. If the string is not numeric, PHP 8 converts the number to a string and compares the two strings. Before PHP 8 the string was converted to a number instead, so "abc" == 0 was true, a long-standing source of security bugs; in PHP 8 it is false. Arithmetic still converts numeric strings to numbers.
var_dump("1e3" == "1000"); // bool(true) var_dump("abc" == 0); // bool(false)
4.What is the difference between isset, empty and is_null in PHP?
In short: isset is true when a variable exists and is not null; empty is true for missing or falsy values; is_null tests an existing value for null.
isset($x) returns true only if $x is defined and not null, and it works on missing array keys and properties without a warning, which is why it is used to check input. empty($x) returns true if $x is missing or falsy: null, false, 0, 0.0, "", "0" or an empty array. So isset($a["x"]) is false for a key whose value is null, and empty($a["y"]) is true for a key holding 0, as below. is_null is an ordinary function, so calling it on an undefined variable raises a warning. Because empty treats "0" as empty, it is a poor check for user input that may legitimately be 0.
$a = ["x" => null, "y" => 0]; var_dump(isset($a["x"])); // bool(false) var_dump(empty($a["y"])); // bool(true)
5.How do the ?? and ??= operators work in PHP?
In short: $a ?? $b gives $a unless it is null or undefined, without a warning; $a ??= $b assigns $b only when $a is null or undefined.
The null coalescing operator returns its left operand if that exists and is not null, and otherwise its right operand, and it does not warn about missing variables, keys or properties, so $cfg["host"] ?? "local" reads an optional setting in one expression. It can be chained, $a ?? $b ?? $c. The assignment form, added in PHP 7.4, $x ??= $default, assigns only when $x is null or unset, which suits filling in defaults: the existing port 8080 below is kept, while the missing host falls back to local.
$cfg = ["port" => 8080]; $h = $cfg["host"] ?? "local"; $cfg["port"] ??= 80; echo $h, " ", $cfg["port"]; // local 8080
6.What is the spaceship operator <=> in PHP?
In short: $a <=> $b returns -1, 0 or 1 when $a is less than, equal to or greater than $b, which is exactly what sort comparators need.
The combined comparison operator compares two values with PHP's usual comparison rules and returns an integer: -1 if the left is smaller, 0 if they are equal, and 1 if the left is larger. That is the contract usort and similar functions expect from a comparator, so a callback such as fn($a, $b) => $a->age <=> $b->age sorts by age, and swapping the operands sorts in reverse. It works on numbers, strings (compared in dictionary order, so "b" <=> "a" is 1) and arrays, which are compared element by element.
echo 1 <=> 2, " "; echo 2 <=> 2, " "; echo "b" <=> "a"; // -1 0 1
7.What happens when a PHP integer overflows, and how precise are floats?
In short: An int that exceeds PHP_INT_MAX becomes a float instead of wrapping; floats are IEEE 754 doubles, so decimal fractions are not exact.
PHP integers are 64-bit on 64-bit systems, with PHP_INT_MAX at 9223372036854775807. Arithmetic that goes past that limit does not wrap around as in C; the result silently becomes a float, which keeps the magnitude but loses precision for large values. Floats are IEEE 754 double-precision numbers, so fractions such as 0.1 cannot be represented exactly, and 0.1 + 0.2 == 0.3 is false. Compare floats with a tolerance, abs($a - $b) < 1e-9, and use integers for money, counting in the smallest unit such as paise, or the bcmath extension for arbitrary precision.
$big = PHP_INT_MAX; var_dump(is_float($big + 1)); // bool(true) var_dump(0.1 + 0.2 == 0.3); // bool(false)
How the diagnostic asks it
One question from the PHP bank, exactly as a sitting would show it. The bank has 4 on types & operators and 30 across PHP.
In PHP 8, what does this code print? (Each var_dump prints bool(true) or bool(false).)
var_dump(0 == "a"); var_dump("1" == "01"); var_dump(100 == "1e2");
- 1true, true, true
- 2false, false, true
- 3false, true, false
- 4false, true, truecorrect
PHP 8 changed loose comparison between numbers and strings. If the string is numeric, the comparison is numeric, otherwise the number is converted to a string and the two are compared as strings. "a" is not numeric, so 0 == "a" compares "0" with "a" and is false; in PHP 7 the string was converted to the number 0 and the result was true, which is the three-trues option. "1" == "01" compares two numeric strings numerically, 1 with 1, so it is true, and "1e2" is the numeric string 100, so 100 == "1e2" is true. false, false, true misses that numeric strings compare as numbers; false, true, false misses exponent notation.
Measure it
Reading answers tells you what’s true. A diagnostic tells you what you get wrong.
10 PHP questions across its topics, easy to hard, about fifteen minutes. You get a readiness figure with the arithmetic shown, the topics you missed named, and a practice set sized for today. Free: 1 diagnostic a month and 15 problems a day. No card.