PHP Syntax Building Blocks Quiz

PHP
0 Passed
0% acceptance

A 35-question quiz that strengthens knowledge of PHP syntax, language structure, tags, statements, interpolation, and foundational control constructs before stepping into frameworks.

35 Questions
~70 minutes
1

Question 1

During a code review for a shared hosting client, which opening tag keeps templates portable on servers that may disable short tags by default?

A
<?php
B
<?
C
<script language="php">
D
<?=
2

Question 2

While inspecting a marketing partial, what will this snippet render between the section tags when processed?

php
<section>
  <?php echo "Stage "; ?>
  <?= 1 + 2 ?>
</section>
A
Stage 12
B
Stage 3
C
Stage <?= 3 ?>
D
Nothing, because short echo is disabled
3

Question 3

A teammate saved a pure PHP configuration file without a closing ?> tag. Why is omitting the closing tag actually recommended for such files?

A
It shortens bytecode generation time
B
It prevents accidental whitespace from being sent to the output buffer
C
It disables error reporting for the file
D
It lets the file declare multiple namespaces
4

Question 4

A parse error appears in a quick CLI test using this snippet. What is the root cause?

php
<?php
$greeting = "Hi"
echo $greeting;
?>
A
The echo statement must be print
B
The variable must start with $$
C
Missing semicolon after the assignment
D
Closing tag should be omitted in CLI scripts
5

Question 5

When onboarding interns, how do you describe the role of semicolons in PHP source files?

A
They are optional if a statement sits on its own line
B
They terminate most statements so the engine knows where one ends and the next begins
C
They are only needed inside loops
D
They indicate comments will follow
6

Question 6

You compare how single and double quotes treat variables. What output does this snippet produce with $name = "Mina"?

php
<?php
$name = "Mina";
echo "Hi $name";
echo ' | Hi $name';
?>
A
Hi Mina | Hi Mina
B
Hi $name | Hi $name
C
Hi Mina | Hi $name
D
Hi $name | Hi Mina
7

Question 7

While building a logging message, why would you wrap an interpolated array element like {$data["id"]} inside braces within a double-quoted string?

A
Braces force the string to become uppercase
B
Braces clarify where the variable name ends when additional characters follow
C
Braces convert the value to JSON automatically
D
Braces disable interpolation for nested arrays
8

Question 8

A teammate experimented with variable variables. What does this snippet echo?

php
<?php
$label = 'mode';
$mode = 'cli';
echo $$label;
?>
A
mode
B
cli
C
$$label
D
Undefined variable notice
9

Question 9

When coaching juniors on naming rules, which identifier is valid for a PHP variable receiving form data?

A
$2user
B
$user-name
C
$_payload2
D
$user name
10

Question 10

A quick experiment in interactive mode uses this snippet. What does var_dump display?

php
<?php
$value = '5' + 3;
var_dump($value);
?>
A
int(8)
B
string(2) "53"
C
float(8)
D
Parse error
11

Question 11

You enable declare(strict_types=1) at the top of an API entry file. What behavior does this enforce for subsequent function calls?

A
Return values are automatically cast to strings
B
Scalar type declarations must be satisfied exactly rather than coerced
C
All errors are silenced until the script finishes
D
Class autoloading is disabled
12

Question 12

During debugging, what output does this comparison snippet produce?

php
<?php
var_dump('0' == false);
var_dump('0' === false);
?>
A
bool(true) then bool(true)
B
bool(false) then bool(true)
C
bool(true) then bool(false)
D
bool(false) then bool(false)
13

Question 13

When a reviewer flags == in a security-sensitive comparison, what is the recommended approach?

A
Switch to === to avoid unwanted type coercion
B
Add more parentheses around the expression
C
Reverse operand order to make it safer
D
Use != instead because it is faster
14

Question 14

A teammate wraps multi-line copy in a heredoc. What does this snippet echo when $batch equals 7?

php
<?php
$batch = 7;
$message = <<<CARD
Batch $batch ships tonight.
CARD;
echo $message;
?>
A
Batch $batch ships tonight.
B
Batch 7 ships tonight.
C
CARD
D
7 ships tonight.
15

Question 15

When composing deployment notes, why might you prefer nowdoc syntax over heredoc?

A
Nowdoc automatically escapes HTML entities
B
Nowdoc behaves like single quotes, so variables are not interpolated
C
Nowdoc compresses the string at runtime
D
Nowdoc strips newline characters by default
16

Question 16

An onboarding exercise adds items to a list. What does this snippet output?

php
<?php
$colors = ['red', 'green'];
$colors[] = 'blue';
echo count($colors);
?>
A
2
B
3
C
4
D
An undefined offset notice
17

Question 17

Why do modern codebases prefer [] over array() when creating arrays in new files?

A
[] automatically sorts the array
B
[] offers a concise syntax introduced in PHP 5.4 and mirrors other language literals
C
array() is deprecated and removed
D
[] forces values to be integers
18

Question 18

A product page loops through badges. What does this snippet echo?

php
<?php
$badges = ['new', 'eco'];
foreach ($badges as $index => $label) {
    echo "#{$index}:{$label} ";
}
?>
A
#0:new #1:eco
B
new eco
C
#1:new #2:eco
D
#index:label
19

Question 19

When comparing include and require within a bootstrap file, what is the key behavioral difference if a file is missing?

A
include halts execution with a fatal error, require only triggers a warning
B
require halts execution with a fatal error, include only triggers a warning
C
Both behave identically in all cases
D
require automatically retries from a backup path
20

Question 20

A utility module returns configuration data. What does this snippet output if config.php contains "<?php return 'ready';"?

php
<?php
$status = include __DIR__ . '/config.php';
echo $status;
?>
A
1
B
ready
C
config.php
D
21

Question 21

Designers prefer alternative syntax in templates. When is the colon/endif form especially helpful?

A
Inside plain PHP files with no HTML
B
Inside mixed HTML/PHP templates to avoid deeply nested braces
C
When writing CLI scripts to speed up execution
D
When defining traits
22

Question 22

Given this snippet inside a layout, which lines are rendered when $active equals true?

php
<?php if ($active): ?>
<p>Promo on</p>
<?php else: ?>
<p>Promo off</p>
<?php endif; ?>
A
<p>Promo on</p>
B
<p>Promo off</p>
C
No output because endif is missing
D
Both paragraphs print
23

Question 23

When adding quick reminders in a script, which comment style cleanly documents a single line without affecting nested HTML?

A
//
B
<!-- -->
C
/* */
D
<?--
24

Question 24

This multi-line comment hides part of a calculation. What does the script echo?

php
<?php
$total = 10;
/* $total += 5; */
$total += 2;
echo $total;
?>
A
10
B
12
C
15
D
17
25

Question 25

A style guide mentions that PHP largely ignores extra whitespace. Why do teams still align operators and indent consistently?

A
Whitespace directly changes opcode generation
B
Readable alignment helps humans scan control blocks and spot mistakes faster
C
Misaligned whitespace disables OPCache
D
Indentation determines operator precedence
26

Question 26

A teammate wonders about case sensitivity. Which statement accurately describes PHP behavior?

A
Function names are case-insensitive, but variable names are case-sensitive
B
Variables are case-insensitive, but functions are case-sensitive
C
Both variables and user-defined functions are case-insensitive
D
Both variables and functions are case-sensitive
27

Question 27

While reviewing constants, what best practice keeps names predictable when using define()?

A
Define constants with lowercase snake_case to avoid collisions
B
Use uppercase identifiers because constants are global and case-sensitive by default
C
Prefix constants with $$ to mark them as immutable
D
Declare constants inside functions only
28

Question 28

What does this constant snippet output when executed without strict typing changes?

php
<?php
define('VERSION', '8.3');
echo VERSION;
echo version;
?>
A
8.38.3
B
8.3Notice: Use of undefined constant version
C
Notice: VERSION is deprecated
D
version8.3
29

Question 29

In a modular codebase, why do developers group related use statements at the top of a file?

A
use statements automatically generate documentation
B
They alias fully qualified class names so the rest of the file stays readable
C
They speed up opcode caching
D
They restrict classes from being instantiated elsewhere
30

Question 30

A feature branch introduces a namespace declaration. Where should the namespace statement appear within a PHP file?

A
Anywhere after all class definitions
B
At the very top of the file, after the opening tag and optional declare statements
C
Inside the constructor of the first class
D
At the bottom so it wraps everything preceding it
31

Question 31

Product managers asked why match expressions were adopted. Compared to switch, what structural improvement do they provide?

A
match evaluates expressions strictly and returns a value, eliminating break statements
B
match allows fall-through logic by default
C
match only accepts numeric cases, making it faster
D
match permits assignment in case labels
32

Question 32

A helper uses match to translate modes. What does the snippet output when $mode equals "api"?

php
<?php
$mode = 'api';
echo match ($mode) {
    'cli' => 'Command line',
    'api' => 'Service call',
    default => 'Unknown'
};
?>
A
Command line
B
Service call
C
Unknown
D
Nothing, match requires break
33

Question 33

While preparing defensive code, when does the null coalescing operator (??) shine?

A
When you want to concatenate strings safely
B
When you need to fall back only when a value is undefined or null
C
When you must compare two floating-point numbers
D
When you want to repeat a block until a condition changes
34

Question 34

A library defines function logEvent(string $message, string ...$tags). What does the ellipsis communicate to callers?

A
The function is deprecated
B
The function accepts zero or more additional string arguments that arrive as an array
C
Only three tags are accepted
D
The function must be static
35

Question 35

When mixing HTML markup with embedded PHP, what structural habit keeps templates predictable for future editors?

A
Place PHP code in long inline attributes without newlines
B
Use dedicated regions for PHP blocks and keep HTML indentation consistent so the flow is obvious
C
Always close and reopen tags after every echo call
D
Avoid comments to reduce file size

QUIZZES IN PHP