PHP interview questions

Types and comparisons, arrays, OOP, Composer, PHP 8 features, security and the “headers already sent” class of bugs.

Readiness: 0 of 38 nailed

How to use this page: 1. Read a question2. Type your answer3. Check with AI4. Open the model answer5. Timed drill when you’re 70% ready

Types and comparisons

FoundationConcept

What is the difference between isset(), empty() and is_null()?

FoundationPredict the output

What does this print?

$settings = ['count' => 0];
echo $settings['count'] ?: 10;
echo ' ';
echo $settings['count'] ?? 10;
JuniorConcept

What does declare(strict_types=1) do?

Common errors

JuniorIt’s broken: why?

“Warning: Cannot modify header information – headers already sent.” What causes it?

Mid-levelIt’s broken: why?

A CSV export script fails with “Allowed memory size exhausted” on large data. How do you fix it properly?

header('Content-Type: text/csv');
$out = fopen('php://output', 'w');
$stmt = $pdo->query('SELECT id, email FROM users', PDO::FETCH_ASSOC);
foreach ($stmt as $row) {      // one row at a time
  fputcsv($out, $row);
}
fclose($out);

// or generators for your own data sources
function rows(PDO $pdo): Generator { /* yield $row; */ }
JuniorIt’s broken: why?

Times saved by your PHP app are off by 5 hours. What do you check?

Arrays

FoundationWrite it by hand

From an array of users, get the emails of active users, sorted alphabetically.

Mid-levelPredict the output

What is the final array?

$items = [1, 2, 3];
foreach ($items as &$item) {}
foreach ($items as $item) {}
print_r($items);
JuniorPredict the output

What is the result?

print_r(array_merge([1, 2], [3, 4]));
print_r([1, 2] + [3, 4]);
JuniorConcept

sort, asort, ksort and usort: what’s the difference?

OOP

JuniorConcept

What is the difference between an abstract class, an interface and a trait?

SeniorConcept

Explain SOLID with a PHP example you’d actually see.

Mid-levelPredict the output

What does this print?

class A {
  public static function name() { return 'A'; }
  public static function viaSelf() { return self::name(); }
  public static function viaStatic() { return static::name(); }
}
class B extends A {
  public static function name() { return 'B'; }
}
echo B::viaSelf() . ' ' . B::viaStatic();
JuniorConcept

Name some magic methods and when you’d use them.

Mid-levelReal-world scenario

How would you make code that sends notifications easy to switch between email, SMS and Slack?

Mid-levelConcept

What happens if two traits define the same method name?

Tooling

JuniorConcept

What is Composer and what does PSR-4 autoloading do?

FoundationConcept

What problem do namespaces solve?

JuniorConcept

What are PSRs? Name a few.

JuniorConcept

What do ^2.3 and ~2.3 mean in composer.json?

Modern PHP

JuniorConcept

Name some PHP 8 features you use.

JuniorWrite it by hand

Replace these string constants with a PHP 8.1 enum.

Mid-levelConcept

What are readonly properties and why use immutable objects?

Security

JuniorWrite it by hand

Fetch a user by email safely using PDO.

JuniorConcept

How do you store user passwords in PHP?

Mid-levelConcept

Why is unserialize() on user input dangerous?

FoundationWrite it by hand

How do you safely print user input in HTML in plain PHP?

Mid-levelIt’s broken: why?

What is wrong with include $_GET['page'] . '.php';?

Web basics

JuniorConcept

What is the difference between a session and a cookie?

JuniorWrite it by hand

Return JSON from a plain PHP API endpoint correctly.

Runtime

SeniorConcept

How does a PHP request actually run on a server (Nginx + PHP-FPM)? What is OPcache?

JuniorConcept

What happens when a PHP request is served, from browser to response?

Functions

JuniorPredict the output

What does this print?

$rate = 10;
$show = function () use ($rate) { return $rate; };
$rate = 20;
echo $show();
Mid-levelWrite it by hand

What is a generator, and why does it save memory?

Errors

JuniorConcept

Exceptions vs errors in PHP 8: what is Throwable?

Performance

Mid-levelReal-world scenario

How would you optimise a slow PHP script?

Leadership

SeniorHow to reply

How would you help a junior developer stuck on a tricky PHP bug?

Also prepare