PHP Variable Naming Quiz

PHP
0 Passed
0% acceptance

A 35-question quiz devoted to PHP variables, naming conventions, scope rules, references, and the small habits that keep data easy to follow across templates, services, and background jobs.

35 Questions
~70 minutes
1

Question 1

While wiring a shipping dashboard, which variable name safely represents the number of parcels loaded today without violating PHP identifier rules?

A
$todayShipments
B
$Today-Shipments
C
$1shipments
D
$today shipments
2

Question 2

A teammate double-checks whether case sensitivity matters. What does this snippet output?

php
<?php
$count = 4;
echo $Count ?? 0;
?>
A
4
B
0
C
Notice followed by 4
D
Notice followed by 0
3

Question 3

During a design system review, which boolean naming style communicates intent most clearly to other engineers?

A
$isArchived
B
$archivedFlag
C
$a
D
$arch
4

Question 4

A junior dev wonders whether functions see outer variables automatically. What does this snippet echo?

php
<?php
$total = 7;
function addOne() {
    $total = 1;
}
addOne();
echo $total;
?>
A
1
B
7
C
0
D
Notice: Undefined variable $total
5

Question 5

During a payroll audit, why is `$hoursWorkedThisWeek` preferred over `$hw` for long-lived variables?

A
PHP forbids two-letter names
B
Descriptive names let reviewers grasp meaning without cross-referencing comments
C
Short names reduce opcode caching
D
Only uppercase identifiers are persisted
6

Question 6

A bug hunt involves references. What does this snippet output?

php
<?php
$region = 'north';
$alias =& $region;
$alias = 'south';
echo $region;
?>
A
north
B
south
C
$alias
D
An undefined variable notice
7

Question 7

In an object-oriented codebase, what does `$this` represent when used inside a class method?

A
The parent class name
B
A reference to the current object instance
C
A global configuration array
D
The namespace alias of the file
8

Question 8

A cleanup script removes temporary values. What does the snippet print?

php
<?php
$userName = 'Rafi';
unset($userName);
echo isset($userName) ? 'set' : 'cleared';
?>
A
set
B
cleared
C
Rafi
D
Notice: Undefined variable
9

Question 9

When storing multiple orders in an array, why do teams prefer plural names such as `$orders` over `$orderListThing` or `$o`?

A
Plural nouns immediately signal a collection without extra comments
B
Plural names disable foreach loops
C
PHP errors unless arrays end with "s"
D
Plural names are required for Composer autoloading
10

Question 10

A utility function tracks clicks. What appears in the output buffer?

php
<?php
function tapCounter() {
    static $count = 0;
    $count += 2;
    return $count;
}
echo tapCounter();
echo tapCounter();
?>
A
02
B
24
C
22
D
24 with notices
11

Question 11

Why do most style guides keep constant names such as `MAX_UPLOAD_SIZE` in uppercase?

A
Uppercase names guarantee faster execution
B
Uppercase visually distinguishes immutable values from regular variables
C
Lowercase constants break namespaces
D
Uppercase makes constants accessible inside JavaScript
12

Question 12

A finance helper needs to adjust a shared rate. What does this snippet echo?

php
<?php
$rate = 1.2;
function adjustRate() {
    global $rate;
    $rate += 0.1;
}
adjustRate();
echo $rate;
?>
A
1.2
B
1.3
C
0.1
D
Notice: Undefined variable
13

Question 13

Code reviewers often allow single-letter variables only inside short loops. Why the restriction?

A
PHP cannot parse longer names inside loops
B
Short, scoped loops are the rare places where a temporary like $i does not obscure meaning
C
Single letters are required for foreach
D
Security scanners reject vowels
14

Question 14

A teammate experiments with array destructuring. What gets echoed?

php
<?php
$pair = ['make', 'model'];
[$first, $second] = $pair;
echo $second;
?>
A
make
B
model
C
Array
D
Notice: Undefined variable $second
15

Question 15

Billing code stores currency in cents. Why name the variable `$priceCents` instead of `$price`?

A
PHP refuses to handle decimals
B
Including the unit clarifies that the raw integer still needs formatting before display
C
Shorter names consume more memory
D
Variables may only contain letters or numbers
16

Question 16

A support script wants a fallback alias. What happens here?

php
<?php
$alias = null;
$alias ??= 'guest';
echo $alias;
?>
A
null
B
guest
C
$alias
D
Notice about undefined variable
17

Question 17

When injecting dependencies, why pair a descriptive variable like `$emailSender` with the type-hinted interface?

A
Otherwise PHP cannot resolve the class
B
The name tells readers which concern the collaborator handles, reinforcing the interface contract
C
Type hints are ignored unless the variable ends with Service
D
Constructor parameters must always be plural
18

Question 18

An old plugin used variable variables. What does this snippet echo?

php
<?php
$field = 'status';
$status = 'active';
echo $$field;
?>
A
status
B
active
C
$$field
D
Undefined variable notice
19

Question 19

Configuration files often prefix environment variables with `ENV_` or `APP_`. Why keep that convention?

A
PHP requires uppercase prefixes for getenv()
B
A shared prefix groups related keys and prevents collisions with system-level variables
C
Lowercase names cannot be exported
D
Prefixes enable automatic encryption
20

Question 20

When documenting a helper function, why should parameter names in the docblock match the actual signature?

A
The interpreter reads docblocks for execution
B
Matching names keep IDE hints accurate and avoid confusion during reviews
C
Mismatched names disable type hints
D
Docblocks are ignored when names match
21

Question 21

A closure needs a label defined outside. What does this snippet echo?

php
<?php
$prefix = 'srv';
$builder = function () use ($prefix) {
    return $prefix . '-01';
};
echo $builder();
?>
A
-01
B
srv-01
C
$prefix-01
D
Notice about undefined variable $prefix
22

Question 22

What is the practical difference between using `global $config;` inside a function and reading `$GLOBALS['config']` directly?

A
global creates a local alias, while $GLOBALS accesses the superglobal array manually
B
global copies values, $GLOBALS references by pointer
C
There is no difference at all
D
global only works for constants
23

Question 23

During a null-check tutorial, what does this snippet output?

php
<?php
$value = null;
echo isset($value) ? 'set' : 'missing';
?>
A
set
B
missing
C
null
D
Notice: Undefined variable
24

Question 24

Why should developers avoid reusing a variable for entirely different meanings later in the same function?

A
PHP forbids reassignment
B
Changing the semantic meaning mid-function confuses reviewers and invites subtle bugs
C
Garbage collection fails when names change
D
It prevents the file from compiling
25

Question 25

A responder packages a payload. What structure is produced?

php
<?php
$team = 'ops';
$role = 'lead';
$bundle = compact('team', 'role');
echo $bundle['team'] . ':' . $bundle['role'];
?>
A
ops:lead
B
team:role
C
Array
D
Notice about undefined index
26

Question 26

In a value object storing addresses, why keep property names like `$streetLine1` instead of generic terms such as `$fieldOne`?

A
PHP sorts properties alphabetically
B
Domain-aligned names document the data structure without separate diagrams
C
Generic terms trigger lint errors
D
Only camelCase is serializable
27

Question 27

When logging metrics, why distinguish between `$requestDurationMs` and `$requestDurationSeconds` rather than reusing one variable?

A
PHP formats time automatically when names differ
B
Unique names prevent mixing units and make downstream math reliable
C
Variables cannot store floats
D
Long names are required for logging
28

Question 28

A legacy template still uses extract(). What does this snippet echo?

php
<?php
$data = ['title' => 'Docs', 'status' => 'draft'];
extract($data);
echo $status;
?>
A
Docs
B
draft
C
$status
D
Notice: Undefined variable $status
29

Question 29

Why is it useful to prefix feature-flag variables (for example, `$ffCheckoutDiscount`) when multiple experiments coexist?

A
Prefixes trigger automatic toggling
B
A shared prefix groups feature-related flags so dashboards and search tools surface them together
C
PHP disallows flags without prefixes
D
Prefixed names bypass cache invalidation
30

Question 30

When a variable does something non-obvious, why include a quick inline comment explaining the intent?

A
Comments are required for all variables
B
A short comment clarifies intent without forcing the reader to reconstruct the surrounding business rule
C
Comments change how PHP executes
D
Variables cannot be used unless documented
31

Question 31

A product export destructures associative data. What prints here?

php
<?php
$user = ['id' => 9, 'name' => 'Ivy'];
['id' => $userId] = $user;
echo $userId;
?>
A
9
B
Ivy
C
Array
D
Notice: Undefined index
32

Question 32

Why must superglobal names such as `$_POST` stay uppercase when referenced?

A
The engine defines them in uppercase; changing case points to a different variable entirely
B
Lowercase superglobals are reserved for extensions
C
Uppercase names run faster
D
Uppercase avoids JSON conversion
33

Question 33

In multilingual applications, why include locale hints in variable names such as `$dateFr` versus `$dateEn`?

A
PHP chooses translation files based on variable names
B
Locale hints prevent accidentally mixing formats when presenting to different regions
C
Variables must end with a country code
D
Locale suffixes enable Unicode mode
34

Question 34

Why is `$temporaryToken` a better name than `$token2` when holding a one-time password during signup?

A
Numbers in names are illegal
B
The descriptive noun explains the lifecycle, which helps developers invalidate it correctly
C
Short names cannot be hashed
D
Numbered names force strict typing
35

Question 35

When teaching interns, why emphasize aligning variable names with the business glossary kept by product managers?

A
Glossary alignment keeps cross-functional conversations consistent, so everyone knows a variable’s meaning instantly
B
Glossaries control memory allocation
C
Without the glossary, PHP refuses to run
D
Legal compliance demands identical spelling

QUIZZES IN PHP