PHP String Handling Quiz

PHP
0 Passed
0% acceptance

A 30-question quiz on PHP string handling, covering interpolation, encoding, concatenation, formatting, searching, and multibyte safety so everyday text processing stays predictable.

30 Questions
~60 minutes
1

Question 1

Why do code reviews ask engineers to explain whether a string should use single or double quotes when no interpolation is needed?

A
Single quotes avoid accidental interpolation, so reviewers know the text will emit exactly as written.
B
Double quotes are forbidden in PHP.
C
Single quotes execute faster because they skip OPCache.
D
Double quotes cannot contain punctuation.
2

Question 2

A teammate tests interpolation. What does this snippet print?

php
<?php
$user = 'Nova';
echo "Hello $user";
echo ' | Hello $user';
?>
A
Hello Nova | Hello $user
B
Hello $user | Hello Nova
C
Hello Nova | Hello Nova
D
Hello $user | Hello $user
3

Question 3

During localization planning, why is mb_strlen preferred over strlen for user-facing names?

A
mb_strlen counts multibyte characters correctly, preventing underestimates for emojis or accented letters.
B
mb_strlen enforces ASCII-only strings.
C
mb_strlen automatically trims whitespace.
D
mb_strlen encrypts the string length.
4

Question 4

What does this snippet log?

php
<?php
echo strlen('café');
?>
A
5 when using UTF-8 because é takes two bytes
B
4 regardless of encoding
C
Warning: multibyte not supported
D
0
5

Question 5

A helper escapes HTML before logging. What prints?

php
<?php
$input = '<strong>Sale</strong>';
echo htmlspecialchars($input, ENT_QUOTES, 'UTF-8');
?>
A
&lt;strong&gt;Sale&lt;/strong&gt;
B
<strong>Sale</strong>
C
&amp;lt;strong&amp;gt;Sale&amp;lt;/strong&amp;gt;
D
Sale
6

Question 6

Why do architecture guidelines warn against concatenating user input into SQL strings for debugging even when the SQL is not executed?

A
Log files may later be parsed or replayed, so unescaped text can break tooling or expose sensitive data.
B
PHP disallows concatenation with dots.
C
Strings cannot include newline characters.
D
Concatenation deletes variables.
7

Question 7

A template uses heredoc to format copy. What appears?

php
<?php
$plan = 'Pro';
$message = <<<TEXT
Plan: $plan
Thank you!
TEXT;
echo $message;
?>
A
Plan: Pro Thank you!
B
Plan: $plan Thank you!
C
Plan: Pro Thank you!
D
Plan:
8

Question 8

When formatting currency strings for UI, why do teams prefer number_format over manual concatenation of symbols?

A
number_format keeps thousands separators and decimal places consistent, improving readability across locales.
B
Manual concatenation crashes PHP.
C
number_format automatically converts to foreign exchange rates.
D
number_format encrypts currency values.
9

Question 9

What does this trimming helper print?

php
<?php
$sku = "  item-24  ";
echo trim($sku);
?>
A
item-24
B
item-24
C
item-24
D
item-24
10

Question 10

Why should log messages use sprintf placeholders instead of concatenating user input directly?

A
Formatted placeholders keep structure constant and avoid mixing types or accidentally omitting context.
B
Concatenation disables log rotation.
C
sprintf encrypts user data automatically.
D
Concatenation is not allowed in PSR-3.
11

Question 11

What does this sprintf call output?

php
<?php
$amount = 12.5;
echo sprintf('%.02f USD', $amount);
?>
A
12.50 USD
B
12.5 USD
C
12 USD
D
USD 12.50
12

Question 12

Why is json_encode with JSON_UNESCAPED_UNICODE useful when sending localized strings to clients?

A
It keeps characters like ñ or 汉字 readable rather than escaping them as \uXXXX sequences.
B
It compresses the JSON payload.
C
It automatically translates text.
D
It enforces ASCII-only output.
13

Question 13

A snippet removes prefix text. What prints?

php
<?php
$value = 'order:123';
echo substr($value, 6);
?>
A
123
B
order:
C
rder:123
D
14

Question 14

Why do product teams prefer str_contains over strpos !== false when scanning for keywords in PHP 8+ codebases?

A
str_contains is clearer to read and avoids mistakes when strpos returns 0 for matches at the beginning.
B
strpos no longer exists.
C
str_contains automatically escapes regex characters.
D
str_contains is the only function usable in CLI scripts.
15

Question 15

What does this substring check echo?

php
<?php
echo str_contains('production', 'duct') ? 'found' : 'missing';
?>
A
found
B
missing
C
duct
D
production
16

Question 16

When sanitizing filenames, why is preg_replace with a whitelist pattern safer than stripping disallowed characters one by one?

A
Whitelisting ensures only approved characters remain, avoiding edge cases where new characters were never considered.
B
preg_replace refuses to remove text.
C
Whitelists run faster than blacklists in every case.
D
Regular expressions are required for any string change.
17

Question 17

A cleanup filter keeps safe characters. What prints?

php
<?php
$raw = 'report_2025?.pdf';
echo preg_replace('/[^A-Za-z0-9_.-]/', '', $raw);
?>
A
report_2025.pdf
B
report_2025?.pdf
C
report_2025?
D
.pdf
18

Question 18

Why do review checklists ask whether string replacements should be case-insensitive, especially for user-entered keywords?

A
Case awareness changes behavior significantly; replacing "Sale" vs "sale" affects marketing copy and search indexing.
B
PHP only supports lowercase strings.
C
Case-insensitive replacements are deprecated.
D
Case-insensitive replacements automatically sort arrays.
19

Question 19

What does this str_ireplace snippet echo?

php
<?php
echo str_ireplace('sale', 'deal', 'SALE starts now!');
?>
A
deal starts now!
B
SALE starts now!
C
Deal starts now!
D
deal DEAL
20

Question 20

When documenting APIs, why should examples show whether strings include trailing newline characters?

A
Trailing newlines affect copy/paste behavior and may cause signature mismatches if clients send extra bytes.
B
PHP cannot output newline characters.
C
JSON spec forbids newline characters.
D
Newlines make requests faster.
21

Question 21

A snippet normalizes line endings. What prints?

php
<?php
$text = "Line1
Line2";
echo str_replace(["
", "
"], "
", $text);
?>
A
Line1 Line2
B
Line1Line2
C
Line1 Line2
D
Line1\nLine2
22

Question 22

Why do teams log the encoding (UTF-8 vs ISO-8859-1) when debugging garbled characters?

A
Mixed encodings lead to mojibake; knowing the source encoding speeds up reproduction and fixes.
B
Logging encoding automatically converts the data.
C
Encodings change the database schema.
D
PHP cannot detect encodings.
23

Question 23

What does this iconv snippet output?

php
<?php
$latin = iconv('ISO-8859-1', 'UTF-8', "Éclair");
echo $latin;
?>
A
Éclair
B
Éclair with mojibake characters
C
Warning: iconv failed
D
24

Question 24

Why is htmlentities often reserved for content that needs full entity encoding rather than minimal escaping?

A
htmlentities converts many characters (including quotes and accents) into entities, which can be verbose and harder to read in logs.
B
htmlentities refuses to encode ampersands.
C
htmlentities only works in CLI scripts.
D
htmlentities removes spaces.
25

Question 25

What does this htmlentities call echo?

php
<?php
echo htmlentities('"Paris" & "Lyon"', ENT_QUOTES, 'UTF-8');
?>
A
&quot;Paris&quot; &amp; &quot;Lyon&quot;
B
"Paris" & "Lyon"
C
&quot;Paris" & "Lyon&quot;
D
&amp;Paris&amp;Lyon
26

Question 26

Why do mentoring guides encourage using sprintf rather than concatenation when building sentences with multiple variables?

A
sprintf keeps the sentence template in one place, preventing mistakes when order or punctuation changes.
B
Concatenation does not support uppercase letters.
C
sprintf automatically translates text.
D
Concatenation makes code fail under strict typing.
27

Question 27

What does this formatted sentence output?

php
<?php
$city = 'Jakarta';
$temp = 31;
echo sprintf('%s is %d°C today.', $city, $temp);
?>
A
Jakarta is 31°C today.
B
%s is %d°C today.
C
Jakarta is %d°C today.
D
31 is Jakarta°C today.
28

Question 28

Why is strtoupper inappropriate for locale-aware case conversions such as Turkish dotted/dotless I?

A
strtoupper follows ASCII rules; locale-aware conversions require mb_strtoupper with the proper locale.
B
strtoupper deletes vowels.
C
strtoupper only runs on Windows.
D
strtoupper forces strings to numeric form.
29

Question 29

What does this mb_strtoupper snippet echo?

php
<?php
echo mb_strtoupper('istanbul', 'tr_TR');
?>
A
İSTANBUL
B
ISTANBUL
C
istanbul
D
Warning: unsupported locale
30

Question 30

Why do documentation templates remind developers to normalize whitespace before comparing multiline strings in tests?

A
Minor whitespace differences can cause brittle assertions, so intentional normalization makes tests clearer.
B
Whitespace normalization is required before any comparison.
C
PHP ignores whitespace automatically.
D
Tests cannot compare strings longer than one line.

QUIZZES IN PHP