Advanced Php Interview Questions 2025 Interview Questions & Answers

25 questions available

Q1:

Why is PHP considered partially thread-safe and how does PHP-FPM solve concurrency?

Mid

Answer

PHP itself is thread-safe only when compiled with ZTS, but most extensions are not thread-safe. Hence Apache’’s mod_php rarely uses multi-threaded mode. PHP-FPM solves concurrency by: Running multiple worker processes, not threads. Each request is isolated inside one worker. No shared memory ? no race conditions. It uses a master process to manage pools, recycling, and spawning.
Q2:

How does PHP's garbage collector detect circular references?

Mid

Answer

PHP uses a root-buffer algorithm: Roots represent zvals which are possible garbage. The GC scans for zvals with refcount > 0 but not referenced externally. If two objects reference each other but no external reference exists ? GC marks them collectible. The GC root buffer triggers after threshold (~10k zvals).
Q3:

What is the difference between opcache.interned_strings_buffer and opcache.memory_consumption?

Mid

Answer

interned_strings_buffer: Memory reserved for unique string instances. All repeated strings across requests share the same memory. memory_consumption: Total memory for storing: compiled opcode arrays function metadata class metadata cached results Both reduce parse/compile time significantly.
Q4:

How does PHP convert array keys internally?

Mid

Answer

PHP arrays are ordered hash tables. Key conversion rules: Numeric strings ('10') become integers. Floats are truncated to integers. true becomes 1 false becomes 0 null becomes an empty string.
Q5:

Explain the lifecycle of a PHP request inside PHP-FPM.

Mid

Answer

Nginx/Apache hands off request to PHP-FPM via FastCGI. PHP-FPM picks an idle worker. Worker loads script, executes opcodes from OPCache. Output returned via FastCGI. Worker is cleaned (variables destroyed). Worker reused or recycled based on max_requests.
Q6:

How does PHP handle asynchronous operations when it is synchronous by default?

Mid

Answer

PHP itself is synchronous, but async implementations use: Event loops (ReactPHP, Amp) Fibers (PHP 8.1+) Non-blocking I/O via streams Parallel extension for multi-threading These simulate async behavior.
Q7:

What exactly happens during autoloading?

Mid

Answer

When an undefined class is used: PHP triggers the spl_autoload_register chain. Each autoloader tries to map class ? file path. Files are included dynamically. For composer, classmap + PSR-4 resolution is applied.
Q8:

How does session locking work in PHP?

Mid

Answer

By default, PHP locks the session file: First request acquires exclusive lock. Other requests for same session block. Lock is released on session_write_close(). This prevents race conditions like lost updates.
Q9:

Difference between $GLOBALS and global keyword?

Mid

Answer

$GLOBALS is a superglobal associative array holding all global variables. global imports variable references from global scope to local scope.
Q10:

What happens if you clone an object with private properties?

Mid

Answer

PHP performs a shallow copy: All properties copied. Private properties remain bound to original class. __clone() is executed afterward if defined.
Q11:

Explain the internal structure of an array used as a queue vs hash map.

Mid

Answer

PHP arrays are always ordered hash tables, so: Queue (push/pop) works but may cause reindexing. Hash map uses string/int keys ? direct hash lookup.
Q12:

Can PHP perform tail-call optimization?

Mid

Answer

No. PHP does not support TCO; recursion depth is limited (~100-200 by default).
Q13:

Explain the difference between require vs include vs require_once.

Mid

Answer

include: Warning on failure, script continues. require: Fatal error on failure. _once: Prevents duplicate file inclusion using path lookup + hash table.
Q14:

What is the internal difference between isset() and array_key_exists()?

Mid

Answer

isset() is faster; it checks: Key exists AND value is not null. array_key_exists() checks: Key exists, even if value is null.
Q15:

How does PHP handle type juggling during comparisons?

Mid

Answer

Loose comparison (==): "0" == false ? true "123abc" == 123 ? true Strict comparison (===) avoids conversion.
Q16:

How does SPL DoublyLinkedList differ from array performance-wise?

Mid

Answer

SPL: O(1) insert/delete Arrays: O(n) due to reindexing and copying
Q17:

How does Composer resolve dependency conflicts?

Mid

Answer

Composer uses: SAT (Boolean satisfiability solver) Backtracking algorithm Dependency constraint graph If version ranges conflict ? install fails.
Q18:

When does PHP trigger "copy on write"?

Mid

Answer

Variables share memory until modification: $a = [1,2,3]; $b = $a; // shared $b[0] = 10; // array copy created now
Q19:

How does PHP implement interface method inheritance?

Mid

Answer

PHP ensures: All interface methods must be implemented Visibility must be public Abstract classes may partially implement them
Q20:

What causes a fatal error: “Cannot redeclare class”?

Mid

Answer

Class is included twice without using: Namespaces require_once Composer autoloading Class maps
Q21:

Explain the difference between error, exception, and throwable.

Mid

Answer

PHP 7+: Exception ? catchable Error (fatal) ? also catchable Both inherit from Throwable
Q22:

Why does yield significantly reduce memory usage?

Mid

Answer

Generators produce values lazily: Only one element exists in memory at a time. Large datasets don't need full arrays.
Q23:

How does PHP handle file uploads internally?

Mid

Answer

Files uploaded via multipart/form-data. PHP stores uploads temporarily in /tmp. Moves to target using move_uploaded_file() which verifies: upload origin temp filename reference
Q24:

Why does json_encode() sometimes return NULL even with valid data?

Mid

Answer

Reasons: UTF-8 invalid byte sequence Too deep recursion Numeric overflow NAN/INF values without using flags
Q25:

What happens during a fatal error inside a destructor?

Mid

Answer

PHP halts destructor execution Remaining destructors may still run Output buffer may flush partially Shutdown functions continue