PHP String Basics Quiz

Quiz 7 10 questions

Prerequisites for the quiz

  1. PHP Strings — Basics

Are you ready?

10 questions to solve

Instructions
  1. This quiz goes to full-screen once you press the Start button.
  2. At the end of the quiz, you are able to review all the questions that you answered wrong and see their explanations.
What does the following code output?
<?php

$x = 10;
$y = 20;

echo '($x, $y)';
A single-quoted string ('') in PHP can't be interpolated. Hence, the string '($x, $y)' simply yields the output ($x, $y). This goes with choice (B). For more info, refer to PHP Strings — Basics — String interpolation.
What does the following code output?
<?php

$x = 10;
$y = 20;

echo "($x, $y)";
A double-quoted string ("") in PHP can be interpolated, which is exactly the case with the string "($x, $y)" — this string yields the output (10, 20). Hence, the correct choice is (A). For more info, refer to PHP Strings — Basics — String interpolation.
What does the following code output?
<?php

$x = 0;
$a = [1, 2];

echo "($a[$x], $a[1])";
In the string "($a[$x], $a[1])", $a[$x] is an array element access expression which resolves down to $a[0] and then, ultimately, to 1. A same reasoning applies to $a[1], which resolves down to 2. Hence, the string resolves to the '(1, 2)', and so the correct choice is (A). For more info, refer to PHP Strings — Basics — String interpolation.
Which of the following shows a heredoc string in PHP?

A heredoc string in PHP has the following syntax:

<<<identifier
<string_data>
identifier

This aligns with the choices (B) and (C). For more info, refer to PHP Strings — Basics — Heredoc strings.

What is the error in the following code?
<?php

$str = <<<end
The end of string is this.
end;
There is absolutely no error in the code. For more info, refer to PHP Strings — Basics — Heredoc strings.
How to convert a string to lowercase characters?
The strtolower() function in PHP is used to convert a string to lowercase characters. Hence, the correct choice is (B). For more info, refer to PHP Strings — Basics — Changing the casing.
How to convert a string to uppercase characters?
The strtoupper() function in PHP is used to convert a string to uppercase characters. Hence, the correct choice is (B). For more info, refer to PHP Strings — Basics — Changing the casing.
What does the function f(), as shown below, do?
<?php

function f($str) {
   return $str === strtolower($str);
}
The function f() determines whether the string $str is in lowercase form or not. This goes with choice (A). For more info, refer to PHP Strings — Basics — Changing the casing.
Which of the following functions is used to search for a substring in a string?
The only correct choice here is str_contains() which is used to determine whether a given string contains a particular substring. For more info, refer to PHP Strings — Basics — Searching for a substring.
What does the following code output?
<?php

$str = 'Good';
$str[0] = 'H';

echo $str;
In PHP, strings are mutable, i.e. a character can be modified by accessing it using bracket notation (similar to how an array element is accessed). In the code above, $str[0] = 'H' mutates the first character of $str to 'H'. Consequently, $str becomes 'Hood'. The correct choice is, therefore, (B). For more info, refer to PHP Strings — Basics — String mutability.

"I created Codeguage to save you from falling into the same learning conundrums that I fell into."

— Bilal Adnan, Founder of Codeguage