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.
Popular tests
Narcissistic Personality Inventory (NPI)
This self-report measure is used to assess narcissism as a personality trai…
Start Test
Yale-Brown Obsessive Compulsive Scale (Y-BOCS)
This measure is used to rapidly quantify the current severity of obsessive…
Start Test
CRAFFT Screening Test (CRAFFT 2.1)
This brief screening measure is designed to identify potential alcohol and…
Start Test
Patient Health Questionnaire-9 (PHQ-9)
This measure is commonly used to quickly screen for the presence and severi…
Start Test
Maslach Burnout Inventory (MBI)
This self-report measure is used to assess occupational burnout symptoms in…
Start Test
Adolescent Anxiety Questionnaire
This measure is designed to support a brief appraisal of anxiety symptoms a…
Start Test
Emotional Creativity Inventory (ECI)
This self-report measure assesses individual differences in the originality…
Start Test
Horne–Ostberg Morningness–Eveningness Questionnaire (MEQ)
Circadian preferences influence typical patterns of alertness and sleep tim…
Start Test
Ambivalent Sexism Inventory (ASI)
This measure is designed to assess attitudes toward women, including both o…
Start Test
Internalized Misogyny Scale (IMS)
This measure is designed to assess internalized negative beliefs and stereo…
Start Test
Perceived Stress Scale (PSS-10)
This self-report measure assesses the degree to which individuals appraise…
Start Test
Impulsive Behavior Scale (SUPPS-P)
Impulsivity is a multidimensional construct that is often assessed with bri…
Start Test
Clinical Institute Withdrawal Assessment for Alcohol, Revised (CIWA-Ar)
This rating scale is used to rapidly assess the severity of alcohol withdra…
Start Test
Positive and Negative Affect Schedule (PANAS)
This measure provides a brief self-report assessment of current or typical…
Start Test
Light Triad Scale (LTS)
This self-report measure assesses prosocial personality tendencies and orie…
Start Test
Suicidal Ideation Scale
In clinical settings, the Suicidal Ideation Scale is used to structure an i…
Start Test
Body Dysmorphic Disorder Scale (BDD-D)
This brief self-report measure is designed to screen for and quantify distr…
Start Test
Beck Anxiety Inventory (BAI)
This measure is a brief self-report inventory used to screen for anxiety sy…
Start Test
Differential Test of Perfectionism
This instrument is used to screen for perfectionism-related attitudes and t…
Start Test
Locus of Control Scale
This measure assesses generalized expectancies regarding the degree to whic…
Start Test
New Apathy Scale
This brief self-report measure is used to screen for apathy-related symptom…
Start Test
Perth Alexithymia Questionnaire (PAQ)
This measure assesses individual differences in alexithymia, including diff…
Start Test
Social Intelligence Scale
This brief self-report measure is designed to support rapid screening of in…
Start Test
Fear Test
This measure is designed to evaluate individual differences in fear-related…
Start Test
Neuroticism Level Scale
The measure is intended for brief screening of an individual’s propensity t…
Start Test
Aggressiveness Indicators Screening Questionnaire
This screening tool is designed to quickly identify behavioral indicators a…
Start Test