PHP String Functions Quiz

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Alfredhook3
A
Alfredhook3
Community Contributor
Quizzes Created: 6173 | Total Attempts: 3,194,780
| Questions: 30 | Updated: Nov 5, 2026
Please wait...
Question 1 / 31
🏆 Rank #-- ▾
0 %
0/100
Score 0/100

1. What does `str_replace("world", "peter", "hello world!")` return?

Explanation

The `str_replace` function in PHP is used to replace occurrences of a substring within a string. In this case, it searches for the substring "world" in the string "hello world!" and replaces it with "peter". Since "world" appears once in the input string, it is replaced, resulting in "hello peter!".

Submit
Please wait...
About This Quiz
Web Development Quizzes & Trivia

This assessment focuses on PHP string functions, evaluating your understanding of functions like chr(), explode(), and str_replace(). It's essential for developers looking to enhance their PHP programming skills and effectively manipulate strings in their applications.

2.

What first name or nickname would you like us to use?

You may optionally provide this to label your report, leaderboard, or certificate.

2. Which of the following PHP functions is used to find the position of the first occurrence of a substring within a string?

Submit

3. What does `ucwords("hello world")` return?

Submit

4. What does `ucfirst("hello world!")` return?

Submit

5. What will `trim("hello world!", "hed!")` output?

Submit

6. What does `substr("hello world", 6)` return?

Submit

7. What does `strtoupper("hello world!")` return?

Submit

8. What does `strtolower("HELLO WORLD.")` return?

Submit

9. What will `strrev("hello world!")` output?

Submit

10. What does `strlen("hello")` return?

Submit

11. What will `strpos("I love php, I love php too!", "php")` return?

Submit

12. What is the difference between `strcmp()` and `strcasecmp()` in PHP?

Explanation

`strcmp()` and `strcasecmp()` are both string comparison functions in PHP, but they differ in how they treat letter casing. `strcmp()` performs a case-sensitive comparison, meaning it distinguishes between uppercase and lowercase letters. In contrast, `strcasecmp()` ignores case differences, treating 'A' and 'a' as equivalent. This distinction is crucial when comparing strings where case sensitivity may affect the outcome, such as in user input validation or sorting operations.

Submit

13. What does `strcmp("hello world!", "hello world!")` return?

Explanation

The `strcmp` function compares two strings character by character. If the strings are identical, as in this case with both being "hello world!", the function returns 0. This indicates that there is no difference between the two strings. A return value of 1 would suggest the first string is greater, while -1 would indicate the first string is less than the second. Since both strings are exactly the same, the output is 0, confirming they are equal.

Submit

14. What will `str_word_count("hello world!")` return?

Explanation

The function `str_word_count` counts the number of words in a string. In the case of `"hello world!"`, there are two words: "hello" and "world". The exclamation mark does not count as a separate word; it is considered part of the second word. Therefore, the function correctly returns 2, representing the total number of distinct words present in the input string.

Submit

15. What does the `str_shuffle()` function do in PHP?

Explanation

The `str_shuffle()` function in PHP takes a string as input and randomly rearranges its characters. This means that the order of the characters is altered in a way that is unpredictable, creating a new string that contains the same characters but in a different sequence. It is a useful function for tasks that require randomization, such as creating passwords or random sample strings.

Submit

16. What does the PHP `chr()` function do?

Explanation

The PHP `chr()` function takes an integer representing an ASCII value and returns the corresponding character. For example, `chr(65)` would return 'A', as 65 is the ASCII value for the uppercase letter A. This function is useful for generating characters dynamically based on their ASCII codes, allowing for flexible string manipulation and creation in PHP programming.

Submit

17. What will `str_repeat("wow", 13)` output?

Explanation

The `str_repeat` function in PHP is used to repeat a string a specified number of times. In this case, the string "wow" is repeated 13 times. Therefore, the output will be the string "wow" concatenated together 13 times, resulting in "wowwowwowwowwowwowwowwowwowwowwowwowwowwow". The option "wow repeated 13 times" correctly describes this output, indicating the number of repetitions without showing the actual repeated string.

Submit

18. What does `str_contains($txt, "love")` return when `$txt = "I really love PHP!"`?

Explanation

The function `str_contains()` checks if a specified substring exists within a given string. In this case, it evaluates whether the substring "love" is present in the string `$txt`, which is "I really love PHP!". Since "love" is indeed part of the string, the function returns `true`, indicating that the substring exists.

Submit

19. How many characters long is the SHA-1 hash returned by `sha1("hello")`?

Explanation

SHA-1 produces a hash value that is typically represented as a 40-character hexadecimal string. Each byte of the hash (20 bytes total) is represented by two hexadecimal characters, leading to a total of 40 characters. This format is standard for displaying SHA-1 hashes, making it essential to recognize that the length corresponds to the hexadecimal representation, not the raw binary output.

Submit

20. What does `rtrim($str, "world!")` do when `$str = "hello world!"`?

Explanation

The `rtrim` function in PHP is designed to remove specified characters from the end of a string. In this case, when `$str` is set to "hello world!", `rtrim($str, "world!")` removes any combination of the characters 'w', 'o', 'r', 'l', 'd', and '!' from the right side of the string. Thus, it will strip these characters until it encounters a character that is not included in the specified set, resulting in "hello ".

Submit

21. What is the length of the MD5 hash string returned by `md5("hello")`?

Explanation

MD5 is a widely used cryptographic hash function that produces a fixed-size output. Regardless of the input size, the MD5 algorithm generates a hash that is always 128 bits long. When represented in hexadecimal format, each byte (8 bits) is depicted as two hexadecimal characters. Therefore, the 128 bits result in a 32-character hexadecimal string, making the length of the MD5 hash for any input, including the string "hello," consistently 32 characters.

Submit

22. Which PHP function calculates the MD5 hash of a string?

Explanation

The `md5()` function in PHP is specifically designed to compute the MD5 hash of a given string. This function takes a string as input and returns a 32-character hexadecimal representation of the hash. It is commonly used for data integrity checks and password storage, although it is not recommended for secure password hashing due to vulnerabilities. Other options like `sha1()`, `hash()`, and `crypt()` serve different hashing algorithms or purposes, making `md5()` the correct choice for calculating an MD5 hash.

Submit

23. What does `ltrim($str, "hello")` do when `$str = "hello world!"`?

Explanation

The `ltrim` function in PHP removes specified characters from the beginning of a string. In this case, when `$str` is set to "hello world!", the function call `ltrim($str, "hello")` removes any combination of the characters 'h', 'e', 'l', and 'o' from the left side of the string until it encounters a character not included in this set. Therefore, it results in " world!" since the leading characters 'h', 'e', 'l', and 'o' are stripped away.

Submit

24. What will `implode(" ", array('hello','world!','beautiful','day!'))` output?

Explanation

The `implode` function in PHP joins array elements into a single string using a specified delimiter. In this case, the delimiter is a space (" "), and the array contains the elements 'hello', 'world!', 'beautiful', and 'day!'. When these elements are combined with a space in between, the output becomes "hello world! beautiful day!", effectively creating a readable sentence from the array elements.

Submit

25. What does the PHP `implode()` function do?

Explanation

The PHP `implode()` function is used to combine the elements of an array into a single string, with a specified delimiter inserted between each element. This allows for easy formatting of array data into a more readable or usable string format. For example, if you have an array of words and you want to create a sentence or a comma-separated list, `implode()` efficiently accomplishes this task.

Submit

26. In `fprintf($file, "There are %u million bicycles in %s.", $number, $str)`, what does `%u` represent?

Explanation

In the `fprintf` function, the format specifier `%u` is used to represent an unsigned integer. This means it is designed to format and display a non-negative integer value without any sign. In this context, it is applied to the variable `$number`, which is expected to hold a value that indicates the count of bicycles in millions. The other options, such as string, floating-point number, and hexadecimal number, do not accurately describe the purpose of `%u` in this formatting scenario.

Submit

27. What does the PHP `fprintf()` function do?

Explanation

The `fprintf()` function in PHP is used to write a formatted string to a specified output stream, such as a file or a network socket. It allows developers to format data using placeholders and write the formatted output directly, making it useful for generating structured data or logs. This function is particularly valuable when precise control over the output format is required, such as when dealing with numerical values, strings, or dates, ensuring that the output meets specific formatting needs.

Submit

28. What is the output of `explode(" ", "hello world. it's a beautiful day.")` in terms of array size?

Explanation

The `explode` function in PHP splits a string into an array based on a specified delimiter, in this case, a space (" "). The string "hello world. it's a beautiful day." contains five segments when divided by spaces: "hello", "world.", "it's", "a", and "beautiful", "day.". Therefore, the resulting array will have a size of 5, reflecting the number of words in the original string.

Submit

29. Which PHP function is used to break a string into an array?

Explanation

The `explode()` function in PHP is specifically designed to split a string into an array based on a specified delimiter. By providing the delimiter and the string to be split, `explode()` creates an array where each element corresponds to a segment of the original string, making it useful for parsing data. In contrast, the other options serve different purposes: `implode()` combines array elements into a string, `str_split()` divides a string into equal parts, and `chunk_split()` breaks a string into smaller chunks but retains the original string format.

Submit

30. What will `chr(0x52)` return in PHP?

Explanation

In PHP, the `chr()` function returns a string containing a single character specified by the ASCII value provided as an argument. The hexadecimal value `0x52` corresponds to the decimal value `82`, which is the ASCII code for the uppercase letter 'R'. Therefore, calling `chr(0x52)` will return the character 'R'.

Submit
×
Saved
Thank you for your feedback!
View My Results
Cancel
  • All
    All (30)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
What does `str_replace("world", "peter", "hello world!")` return?
Which of the following PHP functions is used to find the position of...
What does `ucwords("hello world")` return?
What does `ucfirst("hello world!")` return?
What will `trim("hello world!", "hed!")` output?
What does `substr("hello world", 6)` return?
What does `strtoupper("hello world!")` return?
What does `strtolower("HELLO WORLD.")` return?
What will `strrev("hello world!")` output?
What does `strlen("hello")` return?
What will `strpos("I love php, I love php too!", "php")` return?
What is the difference between `strcmp()` and `strcasecmp()` in PHP?
What does `strcmp("hello world!", "hello world!")` return?
What will `str_word_count("hello world!")` return?
What does the `str_shuffle()` function do in PHP?
What does the PHP `chr()` function do?
What will `str_repeat("wow", 13)` output?
What does `str_contains($txt, "love")` return when `$txt = "I really...
How many characters long is the SHA-1 hash returned by...
What does `rtrim($str, "world!")` do when `$str = "hello world!"`?
What is the length of the MD5 hash string returned by `md5("hello")`?
Which PHP function calculates the MD5 hash of a string?
What does `ltrim($str, "hello")` do when `$str = "hello world!"`?
What will `implode(" ", array('hello','world!','beautiful','day!'))`...
What does the PHP `implode()` function do?
In `fprintf($file, "There are %u million bicycles in %s.", $number,...
What does the PHP `fprintf()` function do?
What is the output of `explode(" ", "hello world. it's a beautiful...
Which PHP function is used to break a string into an array?
What will `chr(0x52)` return in PHP?
play-Mute sad happy unanswered_answer up-hover down-hover success oval cancel Check box square blue
Alert!