PHP String Functions Quiz

Quiz 8 13 questions

Prerequisites for the quiz

  1. PHP Strings — String Functions

Are you ready?

13 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.
Name the function used to obtain the lowercase version of a string.
strtolower() is the function that converts a given string to lowercase characters. Hence, the correct choice is (B). Refer to PHP Strings — strtolower() for more info.

Consider the following code:

<?php

$str = 'hello';
$str[0] = strtoupper($str[0]);
echo $str;

We aim to lowercase the first character of the string $str.

Name the function to do this directly.
ucfirst() is used to convert the first character of a string to uppercase. Hence, the correct choice is (A). Refer to PHP Strings — ucfirst() for more info.
What does the following code output?
<?php

$str = '-|-|Hello|-|--';
echo trim($str, '-'), "\n";
echo trim($str, '-|'), "\n";
The second argument to trim() specifies a string containing all the characters that ought to be trimmed from the original string passed to the function.

In the code above, trim($str, '-') removes - from both ends of the string to give '|-|Hello|-|'. Similarly, trim($str, '-|') removes both - and | from both ends of the string to give 'Hello'.

Thus, the correct choice is (C). Refer to PHP Strings — trim() for more info.
What does the following code output?
<?php

$str = 'a..zABCa..y';
echo trim($str, 'a..z');
The second argument to trim() specifies a string containing all the characters that ought to be trimmed from the original string passed to the function. We can even pass a range of characters using the special .. symbol in this string.

trim($str, 'a..z') means to trim all characters from a to z from both ends of $str. This gives us '..zABCa..' and this goes with choice (D). Refer to PHP Strings — trim() for more info.
How to trim whitespace off from the end of a string $str?
The correct choice is ltrim(). Refer to PHP Strings — ltrim() for more info.
A task requires us to determine whether a substring exists in a given string or not. Which function is the best to use here?
When we only want to determine whether a given substring exists in a string or not, we should use the str_contains() function. Hence, the correct choice is (C). Refer to PHP Strings — str_contains() for more info.
What does the following code output?
<?php

var_dump(strpos('PHP is great', 'JavaScript'));
When strpos() is unable to find a match for the substring (second arg) in the main string (first arg), it returns false. Hence, the correct choice is (C). Refer to PHP Strings — strpos() for more info.
Name a function to achieve a similar result to the following:
<?php

// Suppose $str and $substr are defined.
strpos($str, $substr) === strlen($str) - strlen($substr);
The str_ends_with() function is used to determine whether a given string ends with a given substring. That's effectively what the statement in the code above is trying to do. Likewise, the correct choice is (D). Refer to PHP Strings — str_ends_with() for more info.
What is the value of $str in the following code?
<?php

$str = substr('PHP is great', 0, 3);
substr() slices a given string from a starting position (the second arg) to a given length (the third arg). In the code above, it slices the string 'PHP is great' from position 0 onwards for three characters to ultimately return 'PHP'. This goes with choice (A). Refer to PHP Strings — substr() for more info.
What is the value of $str in the following code?
<?php

$str = substr('PHP is great', 1, 3);
substr() slices a given string from a starting position (the second arg) to a given length (the third arg). In the code above, it slices the string 'PHP is great' from position 1 onwards for three characters to ultimately return 'HP '. This goes with choice (B). Refer to PHP Strings — substr() for more info.
What does the following code output?
<?php

echo str_replace('PHP', 'Java', 'Java is great'), "\n";
echo str_replace('Java', 'PHP', 'Java is great'), "\n";
The first argument to str_replace() specifies the substring to search, the second one specifies the substring to replace it with, and the third argument specifies the main string. The function returns back a string with the replacements

In the code above, the first str_replace() call doesn't have any effect since we are trying to search 'PHP' in 'Java is great' which obviously doesn't exist in there. The second call, however, does complete with one replacement as 'Java' exists in 'Java is great'.

The correct choice in this case is (B). Refer to PHP Strings — str_replace() for more info.
How to obtain the total number of occurrences of a given substring inside a main string in PHP?
The substr_count() function is used for this purpose. Refer to PHP Strings — substr_count() for more info.
What's the difference between str_split() and explode()?
The correct explanation is laid out in choice (A). Refer to PHP Strings — String splitting for more info.