Junior PHP Interview Questions
These questions cover the PHP fundamentals expected from an entry-level developer. Try answering each question before reading the explanation.
1. What is PHP?
PHP is a server-side scripting language commonly used to build dynamic websites and web applications. PHP code runs on the server and can generate HTML, JSON, files, and other responses.
2. What is the difference between echo and print?
Both can output text. echo can accept multiple arguments and has no return value. print accepts one argument and returns 1, although that return value is rarely useful.
echo 'Hello ', 'world';
print 'Hello world';
3. What is the difference between == and ===?
== compares values after type juggling where applicable. === compares both value and type.
var_dump(5 == '5'); // true
var_dump(5 === '5'); // false
Prefer strict comparisons when the expected type is known.
4. What are PHP arrays?
PHP arrays are ordered maps that can use integer or string keys. They can represent lists, dictionaries, and mixed key-value collections.
$colors = ['red', 'blue'];
$user = ['name' => 'Alex', 'active' => true];
5. What is the difference between include and require?
Both include another PHP file. If the target file cannot be loaded, require causes a fatal error, while include emits a warning and execution may continue. The _once variants prevent the same file from being included more than once.
6. What are GET and POST?
GET is commonly used to retrieve resources and places parameters in the URL. POST sends data in the request body and is commonly used for submissions that create or change state. Neither method automatically makes input trustworthy.
7. How do you read form data in PHP?
Use superglobals such as $_GET and $_POST, then validate the values on the server.
$name = trim($_POST['name'] ?? '');
if ($name === '') {
http_response_code(422);
exit('Name is required.');
}
8. What is a PHP session?
A session stores server-side state associated with a browser through a session identifier, usually held in a cookie. Sessions are commonly used for login state and temporary user data.
9. How should passwords be stored?
Never store plaintext passwords. Use password_hash() to create a password hash and password_verify() to check a submitted password.
$hash = password_hash($password, PASSWORD_DEFAULT);
$isValid = password_verify($submittedPassword, $hash);
10. How do you prevent SQL injection?
Use PDO prepared statements and bind user-supplied values rather than concatenating them into SQL.
$stmt = $pdo->prepare('SELECT id FROM users WHERE email = :email');
$stmt->execute(['email' => $email]);
11. What is the difference between isset() and empty()?
isset() checks whether a variable exists and is not null. empty() returns true for values considered empty, including 0, '0', false, null, and an unset variable. Choose based on the exact validation rule.
12. What is htmlspecialchars() used for?
It converts special HTML characters into entities. It is commonly used to safely display untrusted text in HTML text or quoted attribute contexts.
echo htmlspecialchars($comment, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
Output encoding must match the context; HTML escaping is not a universal solution for JavaScript or CSS.