PHP Interview Questions
In this page:
Interface vs. Abstract Class
Interfaces declare only method signatures with no implementation, forcing implementing classes to provide their own behavior; abstract classes can mix concrete methods with abstract ones, which is why interfaces suit unrelated classes sharing a contract while abstract classes suit closely related ones sharing code.
Example: Interface vs. Abstract Class
<?php
interface Shape {
function area();
}
abstract class Vehicle {
function honk() { echo "Beep\n"; }
abstract function drive();
}
echo "Interfaces: contract only. Abstract classes: can mix concrete + abstract methods.";
?>
Login to try C/C++/Java/PHP code in the editor
Dependency Inversion
Dependency inversion means code should depend on an interface or abstraction rather than a specific concrete class, so swapping one implementation for another (like switching database drivers) doesn't require touching the code that uses it.
Example: Dependency Inversion
<?php
interface Logger {
function log($msg);
}
class FileLogger implements Logger {
function log($msg) { echo "File log: $msg"; }
}
class App {
private $logger;
function __construct(Logger $logger) {
$this->logger = $logger;
}
function run() { $this->logger->log("App started"); }
}
(new App(new FileLogger()))->run();
?>
Login to try C/C++/Java/PHP code in the editor
Cookies vs. Sessions
Cookies live in the browser as plain text the user could inspect or tamper with, while sessions keep the actual data server-side and hand the browser only an opaque ID -- which is why sensitive data always belongs in a session, not a cookie.
Example: Cookies vs. Sessions
<?php
setcookie("theme", "dark"); // stored in browser, visible/tamperable
session_start();
$_SESSION['user_id'] = 5; // stored server-side, only ID given to browser
echo "Cookie: client-side. Session: server-side.";
?>
Login to try C/C++/Java/PHP code in the editor
Garbage Collection
PHP tracks how many references point to each value and frees memory once that count hits zero, with an additional cycle-collector that catches objects referencing each other in a loop that reference counting alone can't detect.
Example: Garbage Collection
<?php
class Node {
public $next;
}
$a = new Node();
$b = new Node();
$a->next = $b;
$b->next = $a; // circular reference
unset($a, $b);
gc_collect_cycles();
echo "Cycle collector reclaimed the circular reference";
?>
Login to try C/C++/Java/PHP code in the editor
SQL Injection Prevention
Prepared statements via PDO send the SQL structure and the user-supplied values to the database separately, so even a malicious input string can never be interpreted as part of the query itself -- the strongest defense against SQL injection.
Example: SQL Injection Prevention
<?php
$pdo = new PDO('sqlite::memory:');
$pdo->exec("CREATE TABLE users (id INTEGER, name TEXT)");
$stmt = $pdo->prepare("INSERT INTO users (id, name) VALUES (?, ?)");
$stmt->execute([1, "Robert'); DROP TABLE users;"]);
echo "Malicious input stored safely as plain data";
?>
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: