PHP developer interview preparation

PHP Interview Questions and Answers

Review clear, practical answers on PHP fundamentals, object-oriented programming, web requests, databases, and application security.

28 questions with answers 6 topic areas PHP fundamentals to security
Start practising

Explore the question bank

Search the questions, filter by topic, and open each item to read its answer.

Showing 28 questions

1 What is PHP? PHP Basics

PHP is an open-source, general-purpose scripting language commonly used on web servers. The server executes PHP code and sends the generated response, such as HTML or JSON, to the client.

2 How does PHP handle a web request? PHP Basics

A web server passes a matching request to the PHP runtime. PHP runs the requested script, may read or update data, and returns output for the server to send to the browser. The browser receives the output, not the server-side PHP source.

3 What is the difference between echo and print? PHP Basics

Both write output. echo can output one or more expressions and has no return value; print outputs one expression and returns 1, so it can be used in an expression. In ordinary output statements, echo is commonly used.

4 What are common PHP data types? PHP Basics

Common types include integers, floats, strings, booleans, arrays, objects, callables, iterables, and null. PHP is dynamically typed by default, but type declarations and strict typing can make function contracts clearer.

5 What is the difference between == and ===? PHP Basics

The == operator compares values after type conversion where applicable. The === operator compares both value and type. Prefer strict comparisons when the type is expected to matter, such as checking an integer identifier against a string input.

6 How are isset(), empty(), and is_null() different? PHP Basics

isset() is false when a variable is missing or null. empty() is true for values considered empty, including 0, an empty string, false, and null. is_null() checks specifically whether a defined value is null.

7 How do you define constants in PHP? PHP Basics

Use const for a constant declared in class or file scope, or define() to define one at runtime. Constants cannot be reassigned after definition. Class constants are accessed with the scope-resolution operator, for example MyClass::LIMIT.

8 What are the main types of arrays in PHP? PHP Basics

PHP uses one array type that can act as an indexed list, an associative map with string or numeric keys, or a multidimensional structure containing other arrays. Choose keys that make the data easy to read and access.

9 What is the difference between include and require? PHP Basics

Both load and evaluate another PHP file. If an include fails, PHP emits a warning and execution can continue; if a require fails, PHP raises an error that stops normal execution. The _once variants prevent the same file from being loaded more than once.

10 What is the difference between GET and POST? Web and HTTP

GET puts parameters in the URL and is suited to retrieving or filtering data that can be bookmarked. POST sends data in the request body and is commonly used for submissions that change state. POST alone does not encrypt data; use HTTPS for transport security.

11 What is the difference between a session and a cookie? Web and HTTP

A cookie is stored by the client and sent with matching requests. A session stores data on the server and typically uses a cookie containing a session identifier to associate requests with that data. Protect session identifiers and configure cookie security attributes.

12 What are PHP superglobals? Web and HTTP

Superglobals are built-in arrays available in all scopes. Common examples include $_GET, $_POST, $_SERVER, $_FILES, $_COOKIE, $_SESSION, and $_ENV. Treat values from requests and files as untrusted input and validate them before use.

13 Are function arguments passed by value or by reference? Language Features

Arguments are passed by value by default. PHP uses copy-on-write for many values, so a physical copy is generally delayed until a change requires it. Prefixing a parameter with & passes it by reference, allowing the function to modify the caller’s variable.

14 What do parameter and return types do? Language Features

Type declarations document and enforce the values a function accepts and returns. They make mistakes easier to detect and APIs easier to understand. With declare(strict_types=1), scalar type coercion is stricter for calls made from that file.

15 What is the difference between a class and an object? Object-Oriented PHP

A class defines a type, including its properties and methods. An object is a specific instance created from that class, with its own state. For example, User can be a class and a particular registered account can be an object.

16 What is the difference between an interface and an abstract class? Object-Oriented PHP

An interface defines methods a class agrees to provide and supports multiple interface implementations. An abstract class can define both shared implementation and abstract methods, and a class can extend only one parent class. Use an interface for a capability contract and an abstract class for shared base behavior.

17 What is a trait in PHP? Object-Oriented PHP

A trait is a mechanism for reusing methods across otherwise unrelated classes. A class can use multiple traits, but traits do not replace a clear class hierarchy or an interface contract. Resolve method-name conflicts explicitly when combining traits.

18 What do public, protected, and private mean? Object-Oriented PHP

Public members are accessible from anywhere, protected members are accessible within the class and its subclasses, and private members are accessible only within the declaring class. Restrict visibility to protect invariants and expose a smaller, clearer API.

19 What is the difference between static and instance members? Object-Oriented PHP

Instance members belong to an object and can use that object’s state through $this. Static members belong to the class and are accessed with the :: operator; they do not have an instance context. Prefer instance behavior when it depends on object state.

20 What are namespaces and autoloading? Object-Oriented PHP

Namespaces organize symbols and help avoid naming collisions. Autoloading loads a class file when it is first needed instead of requiring files manually. Composer commonly configures PSR-4 autoloading through composer.json.

21 What is Composer? Tools and Errors

Composer is PHP’s dependency manager. It records project dependencies in composer.json, resolves and installs packages, tracks exact versions in composer.lock, and can generate an autoloader for project classes.

22 How should exceptions and errors be handled? Tools and Errors

Use try/catch for failures a program can handle meaningfully, and use finally for cleanup that must run either way. PHP exceptions and engine errors implement Throwable. Log useful diagnostic details on the server, but do not expose stack traces or secrets to users.

23 How do prepared statements help protect database queries? Security and Data

Prepared statements keep SQL structure separate from user-supplied values. Bind parameters with PDO or MySQLi instead of concatenating input into a query. This prevents values from being interpreted as SQL syntax.

24 How do you prevent SQL injection? Security and Data

Use parameterized queries for values, allowlist identifiers such as sort columns that cannot be bound, validate input for the expected domain, and give the database account only the permissions it needs. Escaping strings manually is not a substitute for prepared statements.

25 How should passwords be stored? Security and Data

Never store plaintext passwords or reversible encrypted passwords. Use password_hash() to create a salted password hash and password_verify() to check a login. Rehash with password_needs_rehash() when your password-hashing settings change.

26 How do you prevent cross-site scripting (XSS)? Security and Data

Escape untrusted values for the output context, such as using htmlspecialchars() for HTML text and quoted attributes. Keep data separate from markup, validate input, and avoid inserting untrusted strings as raw HTML or JavaScript.

27 What is a CSRF attack and how can it be mitigated? Security and Data

Cross-site request forgery tricks an authenticated browser into sending an unwanted state-changing request. Use unpredictable CSRF tokens and validate them on the server, check request intent where appropriate, and set secure SameSite cookie attributes as an additional layer.

28 How should file uploads be validated? Security and Data

Check upload errors and size limits, validate the actual file type rather than trusting its name or client-supplied MIME type, generate a safe server-side filename, and store files outside executable web paths when possible. Apply authorization checks before accepting uploads.

Continue with related interview preparation and PHP resources.