Q1. What data types does PHP have?
Scalar: int, float, string, bool. Compound: array, object, callable, iterable. Special: null and resource. PHP 8 type declarations also have mixed, void, never and union types.
Q2. What is type juggling?
PHP converts types automatically when an operation needs it. That is convenient, but can hide bugs.
<?php
var_dump("5" + "5");
var_dump("5" . "5");
var_dump("abc" == 0);
int(10)
string(2) "55"
bool(false)
Q3. What does declare(strict_types=1); do?
It makes function calls in that file check argument and return types strictly, with no automatic conversion (except int to float). A wrong type throws a TypeError. It applies per file, and does not change operators like +.
Q4. Which values are falsy?
false, 0, 0.0, "", "0", [] and null. Everything else is truthy, including "false" and "0.0".
<?php
var_dump((bool) "0.0");
var_dump((bool) []);
var_dump((bool) "false");
bool(true)
bool(false)
bool(true)
Q5. What is the difference between null, isset(), empty() and is_null()?
null is a value meaning “no value”. isset() is false for null or a missing variable. is_null() is true for null (and warns for an undefined variable). empty() is true for any falsy value or a missing variable.
Q6. What happens when an integer goes past PHP_INT_MAX?
It becomes a float, and precision is lost.
<?php
var_dump(PHP_INT_MAX + 1);
float(9.223372036854776E+18)
Q7. Why does 0.1 + 0.2 == 0.3 give false?
Floats are stored in binary, and some decimals cannot be stored exactly. Compare with a tolerance (abs($a - $b) < 1e-9), and store money as integers (cents) or use BCMath.
Q8. How do you convert types?
With casts ((int) "42", (string) 5, (bool) 1), or functions (intval(), floatval(), strval()). For user input, validate first with filter_var().
Q9. What is the scope of a variable?
Variables created inside a function are local. Variables outside are global, and a function cannot see them unless you use global, $GLOBALS, or (better) pass them as parameters. Static variables keep their value between calls. Constants and superglobals are available everywhere.
Q10. What is the difference between a variable and a constant?
A variable can change and starts with $. A constant is set once, has no $, and is available in every scope.
Q11. What are the magic constants?
__LINE__, __FILE__, __DIR__, __FUNCTION__, __CLASS__, __METHOD__ and __NAMESPACE__. Their value depends on where they are used.
Q12. What does the spaceship operator do?
<=> returns -1, 0 or 1 depending on whether the left side is smaller, equal or bigger. It is the standard tool for sorting callbacks.
<?php
echo (3 <=> 7) . (5 <=> 5) . (9 <=> 2);
-101
Q13. What is the difference between pass by value and pass by reference?
By default, a function receives a copy. With &$param, it works on the original variable. Objects are handled by a handle, so changing an object’s properties inside a function changes the same object.
Q14. Write the output.
<?php
$a = 5;
$b = &$a;
$b++;
$c = $a;
$c++;
echo "$a $b $c";
6 6 7
$b is a reference to $a, so both are 6. $c is a copy, so it becomes 7 without changing $a.
Q15. What does the output of var_dump("10" == "1e1") show, and why?
true. Both strings are numeric, so PHP compares them as numbers: 10 == 10. That is the kind of surprise that === avoids.
<?php
var_dump("10" == "1e1");
var_dump("10" === "1e1");
bool(true)
bool(false)