PHP Quiz
Questions: 16 · 10 minutes
1. Given `$json = '{""theme"":""dark""}';`, which expression decodes it as an associative array rather than an object?
json_decode($json, true)
json_parse($json)
json_decode($json, false)
json_encode($json, true)
2. In normal control flow, what is the purpose of a `finally` block used with `try` and `catch`?
It runs only when no exception occurs.
It runs after the try/catch handling whether or not an exception occurred, making it useful for cleanup.
It converts every exception into a warning.
It retries the code in the `try` block automatically.
3. Which comparison checks whether two PHP values have both the same value and the same type?
The assignment operator, a single equals sign (`=`)
The loose-equality operator, two equals signs (`==`)
The not-equal operator, exclamation mark followed by equals (`!=`)
The strict-equality operator, three equals signs (`===`)
4. What value does `$total` have after this code runs: `$total = 0; foreach ([2, 4, 6] as $n) { $total += $n; }`?
12
8
10
6
5. A PDO query uses a value received from a form. Which pattern best reduces the risk of SQL injection?
Concatenate the value into the SQL after removing spaces.
Prepare a statement with a placeholder, then bind or pass the value when executing it.
Place the value inside double quotation marks in the SQL string.
Convert the complete SQL statement with `htmlspecialchars` before running it.
6. A page contains `$name = $_GET['name'] ?? 'Guest';`. What is assigned to `$name` when the `name` parameter is absent?
The literal string ""name""
An empty string
The Boolean value false
The string ""Guest""
7. How should an integer return type be added to a PHP function named `total` that takes no arguments?
Put an arrow before the type: `function total() -> int { return 5; }`
Place the type before the function name: `function int total() { return 5; }`
Add a colon and the type after the parameter list: `function total(): int { return 5; }`
Begin the declaration with the type: `int function total() { return 5; }`
8. Which statement correctly assigns the string ""Ada"" to a PHP variable named user?
An unprefixed assignment: `user = ""Ada"";`
A dollar-prefixed assignment: `$user = ""Ada"";`
A JavaScript-style declaration: `let $user = ""Ada"";`
A colon-equals assignment: `$user := ""Ada"";`
9. Code is inside the namespace `App`. Without adding a `use` statement, which name explicitly refers to PHP's global `DateTime` class?
App::DateTime
DateTime::global
\DateTime
namespace\DateTime\global
10. Which opening tag is the standard way to begin a block of PHP code?
An HTML-style opening tag written as `<php>`
The standard full opening tag written as `<?php`
The short opening tag written as `<?`
A script element written as `<script php>`
11. Given `$user = ['role' => 'admin', 'active' => true];`, which expression retrieves the value `admin`?
Object-property access using `$user->role`
Numeric array access using `$user[0]`
Function-call syntax using `$user(role)`
Associative-key access using `$user['role']`
12. A required configuration file is missing. What is the key behavioral difference between `require` and `include` in this situation?
`include` retries the file automatically, while `require` does not.
`require` creates an empty replacement file, while `include` skips it.
There is no behavioral difference when the file is missing.
`require` stops normal script execution, while `include` generally raises a warning and allows execution to continue.
13. A username supplied by a visitor will be inserted into an HTML text context. Which approach is most appropriate for escaping it before output?
Use `htmlspecialchars($name, ENT_QUOTES, 'UTF-8')`.
Use `urlencode($name)`.
Use `addslashes($name)`.
Use `strip_tags($name)` as the only protection.
14. A variable `$count` is declared outside a function. Code inside the function tries to read `$count` without receiving it as a parameter or declaring it global. What best explains the problem?
PHP variables can contain only strings inside functions.
Functions can read global variables but cannot return them.
Function scope does not automatically provide access to that global variable.
The variable must first be converted into a constant.
15. Which operator concatenates two strings in PHP?
The period operator (`.`)
The ampersand operator (`&`)
The plus operator (`+`)
The scope-resolution operator (`::`)
16. A PHP page needs to create or resume a session and then send HTML. Which ordering is generally correct?
Send the HTML first, then call `session_destroy()`.
Call `setcookie()` after closing the PHP process.
Call `session_start()` before output that would send response headers.
Place `session_start()` inside the final HTML tag.