PHP interview questions: language core, OOP, PDO and security

Reviewed by Mark Dickie · Last updated

5 questions on this page you can answer and see marked on the spot. Go to the first one

PHP is a server-side scripting language that runs most of the web's CMS and framework code, including WordPress, Laravel and Symfony. An interview usually checks four things: how loose and strict typing behave, how classes, interfaces and traits fit together, how you talk to a database safely with PDO, and whether you can spot injection and session flaws. Know PHP 8 features such as match, enums, readonly properties and named arguments, and you cover most of what gets asked.

What does a PHP interview test?

AreaConcrete things to be ready for
Types and comparison== versus ===, declare(strict_types=1), union types, null coalescing with ??
OOPAbstract classes versus interfaces, traits and conflict resolution, late static binding with static::
Database accessPDO prepared statements, bound parameters, transactions with beginTransaction()
Securitypassword_hash() and password_verify(), htmlspecialchars() for XSS, session fixation and session_regenerate_id()
ToolingComposer autoloading with PSR-4, composer.lock, namespaces

How should I prepare for PHP interview questions?

  1. Write a small script that compares values with == and === (try 0 == "a" on PHP 8) and explain each result.
  2. Rewrite a switch as a match expression and note what happens when no arm matches.
  3. Build a login form with PDO prepared statements and password_hash(), then try to break it with a quote in the input.
  4. Create a backed enum and a class with readonly properties, and use both through constructor promotion.
  5. Set up a PSR-4 autoloader in composer.json and run composer dump-autoload.

Then take the quiz below and read the explanation for every answer you miss.

Key facts

  • Tarmac has 91 PHP interview questions on this topic, 10 of them on this page, at difficulty 1–4 of 5.
  • Tarmac tracked 74 job postings asking for PHP in November 2026.
  • Roles asking for PHP advertise a median base salary of A$116,438, across 29 job postings as of November 2026.
  • Tarmac last reviewed these PHP interview questions on 5 December 2026.

At a glance

Questions10 shown · 91 in the bank
Difficulty1–4 of 5
FormatsTrue / false, Ordering, Find the bug, Multiple choice, Short answer, Fill in the blank, Code output, Multiple answer, Flashcard

What you'll review

  1. php psr standards
  2. php orm
  3. php pdo
  4. php composer autoload

Practice questions

Try one before you open the answer. Pick an option and press Check; it's marked on the spot.

PSR-4 is the PHP-FIG standard that describes autoloading classes from file paths, mapping a namespace prefix to a base directory.#

Options

Show answer

PSR-4 is the PHP-FIG autoloading standard. It maps a namespace prefix to a base directory, and the remaining parts of the class name become subdirectories and a .php filename. Composer uses it through the autoload.psr-4 section of composer.json.

Why:

PSR-4 (Autoloader) says how a fully qualified class name maps to a file path. A namespace prefix maps to a base directory, and the rest of the class name becomes the path under that directory, ending in .php. Composer supports it through the autoload.psr-4 key in composer.json.

In Composer, the composer.lock file is where you declare your project's dependencies and their version constraints, and composer.json records the exact installed versions.#

Options

Show answer

The statement is false. Composer's composer.json is where you declare dependencies and version constraints. Composer generates composer.lock, which records the exact resolved versions so every environment can install the same ones.

Why:

The statement has the two files backwards. You declare dependencies and version constraints in composer.json. Composer writes composer.lock, which records the exact versions it resolved and installed, so composer install can reproduce the same versions on another machine.

Order the steps to run a parameterized query with PDO in PHP.#

Put these in order

Show answer

A PDO parameterized query runs in this order: create the PDO connection, call prepare() with placeholders, call execute() with the parameter values, then fetch() the rows. Each step depends on the one before it. The statement comes from the connection, and there are rows to fetch only after the query executes.

Why:

prepare() is a method of the PDO object, so the connection has to exist first. execute() runs on the statement that prepare() returns, and fetch() only returns rows after the query has run. Each step needs the result of the step before it, so only one order works.

This PHP 8 PDO code is supposed to fetch a user by email safely, but it is open to SQL injection. Which line has the bug?#

<?php
$pdo = new PDO('mysql:host=localhost;dbname=app', 'user', 'pass');
$email = $_GET['email'];
$stmt = $pdo->prepare("SELECT * FROM users WHERE email = '$email'");
$stmt->execute();
$user = $stmt->fetch(PDO::FETCH_ASSOC);
Show answer

The bug is on line 4.

Why:

Line 4 puts $email straight into the SQL string. Calling prepare() does not protect a value that is already part of the query text, so a crafted email can change the query. The fix is to use a placeholder, WHERE email = ? or :email, and pass the value through execute([$email]) so it is bound as data and never read as SQL.

In a Composer project, composer.json contains:#

Options

Show answer

With the PSR-4 mapping "App\\": "src/", Composer looks for App\Http\Controllers\UserController in src/Http/Controllers/UserController.php. PSR-4 strips the namespace prefix App\ and turns the rest of the class name into a path under the mapped directory. The prefix itself does not appear as a folder, which is different from PSR-0.

Why:

PSR-4 maps a namespace prefix to a base directory. The prefix App\ is removed from the fully-qualified class name, and the rest (Http\Controllers\UserController) becomes a path under src/ with .php added. That gives src/Http/Controllers/UserController.php. Under PSR-4 the prefix is not repeated in the path, so src/App/... is wrong. That nested path is how the older PSR-0 standard would resolve it.

In Composer, what is the difference between the require and require-dev sections of composer.json, and how do you install a project without the require-dev packages (for example, in production)?#

Show answer

require lists the packages the application needs to run. require-dev lists packages you only need while developing or testing, such as PHPUnit or static analysers. To leave the dev packages out, run composer install --no-dev. In production this is usually combined with --optimize-autoloader.

Why:

require holds the runtime dependencies and require-dev holds the tools you only use in development. The --no-dev flag tells composer install to skip the require-dev packages, which makes production deployments smaller and safer.

To make PDO throw exceptions on SQL errors, set the error mode attribute: $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::_____);#

Show answer

To make PDO throw exceptions on SQL errors, set the error mode attribute: $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::**ERRMODE_EXCEPTION**);

Why:

PDO::ERRMODE_EXCEPTION tells PDO to throw a PDOException whenever an error occurs. It has been the default since PHP 8.0, and setting it explicitly is still common practice. The other modes are ERRMODE_SILENT and ERRMODE_WARNING, which only set error codes or raise warnings.

This PHP 8 snippet models the Identity Map pattern that ORMs such as Doctrine use. What does it print?#

<?php
class IdentityMap {
    private array $map = [];
    public int $queries = 0;

    public function find(int $id): object {
        if (!isset($this->map[$id])) {
            $this->queries++;
            $this->map[$id] = (object)['id' => $id, 'name' => "User$id"];
        }
        return $this->map[$id];
    }
}

$m = new IdentityMap();
$a = $m->find(1);
$b = $m->find(1);
$b->name = 'Changed';
$c = $m->find(2);

echo $a->name, ' ', var_export($a === $b, true), ' ', $m->queries;
Show answer
Changed true 2
Why:

The first find(1) misses the map, so it runs a "query" (count = 1) and caches the object. The second find(1) hits the cache and returns the same object handle, so $a === $b is true, and changing $b->name also changes what $a sees. find(2) misses again, so the count becomes 2. The snippet prints Changed true 2, which shows how an identity map guarantees one in-memory instance per row and avoids repeat queries.

A Composer 2 project's composer.json contains "autoload": {"psr-4": {"App\\": "src/"}}. You then run composer dump-autoload --optimize (-o) without --classmap-authoritative. Which statements are true?#

Options

Pick every one that applies.

Show answer

composer dump-autoload -o scans PSR-4 directories and builds a classmap of every compliant class. If a class is missing from that map, the loader still falls back to the normal PSR-4 lookup, unless --classmap-authoritative is set. Classes whose file paths break PSR-4 are left out of the map with a warning. Dev autoload rules are included unless you pass --no-dev.

Why:

The -o flag turns PSR-0 and PSR-4 rules into a classmap by scanning the mapped directories, so the statement that Composer writes every compliant class into autoload_classmap.php is true. Without --classmap-authoritative (-a), a classmap miss still falls through to the normal PSR-4 file lookup, which is why a class added after the dump keeps autoloading. Since Composer 1.10/2.x, classes that don't follow PSR-4 paths are left out of the map and Composer warns that they do not comply with PSR-4. autoload-dev rules are included unless you pass --no-dev, so the claim that dev classes are always excluded is false. The fallback lookup reads files from src/, so the claim that src/ can be missing at runtime is also false.

In Composer, what is the difference between the caret constraint ^0.3.2 and ^1.3.2, and why?#

Show answer

^1.3.2 means >=1.3.2 <2.0.0. ^0.3.2 means >=0.3.2 <0.4.0. Composer follows SemVer, which treats a 0.x major version as unstable, so any minor bump might break things. The caret therefore locks the left-most non-zero part of the version.

Why:

Caret ranges allow updates up to the next 'significant' release. For a pre-1.0 version, Composer treats the minor version as the breaking boundary. Many people expect ^0.3.2 to allow any 0.x release, and that is a common mistake.

Related interview questions

Job market

See php salaries and hiring demand from live job postings.

The other 81 questions

This page shows 10 and marks what you pick. That's as far as a page can go. A free account opens the other 81 and keeps every answer. What you miss comes back until it's right: after a day, then at longer gaps.

Start with this topic

Free · the whole bank · 100 answers per 30 days, each marked with written feedback

Interview booked? Practise the job ad instead

What moved, monthly

One email a month when the bulletin comes out: what moved in the markets we track, and the new question topics we published. Confirm your address to join. Unsubscribe any time.