PHP Strings MCQ

1. Which function is used to find the length of a string in PHP?

a) strlen()
b) strlength()
c) length()
d) len()

Answer:

a) strlen()

Explanation:

The strlen() function is used to find the length of a string in PHP.

2. How do you concatenate two strings in PHP?

a) Using the + operator
b) Using the . operator
c) Using the & operator
d) Using the && operator

Answer:

b) Using the . operator

Explanation:

The . operator is used for string concatenation in PHP.

3. Which of the following is the correct way to declare a string in PHP?

a) $string = 'Hello World';
b) $string = "Hello World";
c) Both a) and b)
d) None of the above

Answer:

c) Both a) and b)

Explanation:

In PHP, strings can be declared with either single quotes or double quotes.

4. What is the output of the following PHP code? $str = "Hello"; echo $str[1];

a) H
b) e
c) l
d) o

Answer:

b) e

Explanation:

Strings in PHP are zero-indexed. $str[1] refers to the second character, which is 'e'.

5. How do you replace 'world' with 'PHP' in the string 'Hello world' using PHP?

a) replace('world', 'PHP', 'Hello world');
b) str_replace('world', 'PHP', 'Hello world');
c) substr_replace('Hello world', 'PHP', 5);
d) None of the above

Answer:

b) str_replace('world', 'PHP', 'Hello world');

Explanation:

The str_replace() function is used for string replacement in PHP.

6. Which function is used to reverse a string in PHP?

a) reverse()
b) strreverse()
c) strrev()
d) revstr()

Answer:

c) strrev()

Explanation:

The strrev() function is used to reverse a string in PHP.

7. How can you convert a string to lowercase in PHP?

a) strtolower()
b) tolower()
c) lower()
d) strlower()

Answer:

a) strtolower()

Explanation:

The strtolower() function is used to convert a string to lowercase in PHP.

8. What does the strpos() function do in PHP?

a) Splits a string
b) Replaces part of a string
c) Finds the position of the first occurrence of a substring in a string
d) Counts the number of characters in a string

Answer:

c) Finds the position of the first occurrence of a substring in a string

Explanation:

The strpos() function is used to find the position of the first occurrence of a specified substring in a string.

9. Which of the following is not a valid way to declare a multiline string in PHP?

a) Using single quotes
b) Using double quotes
c) Using heredoc syntax
d) Using nowdoc syntax

Answer:

a) Using single quotes

Explanation:

Multiline strings can be declared using double quotes, heredoc, or nowdoc syntax, but not with single quotes.

10. What is the output of the following code? $str = "Hello"; echo strtoupper($str);

a) hello
b) Hello
c) HELLO
d) HeLlO

Answer:

c) HELLO

Explanation:

strtoupper() converts all characters in a string to uppercase.

11. How do you access the last character of a string in PHP?

a) $str[-1]
b) $str[strlen($str) – 1]
c) $str->last()
d) end($str)

Answer:

b) $str[strlen($str) – 1]

Explanation:

$str[strlen($str) – 1] will return the last character of the string.

12. Which function is used to split a string into an array in PHP?

a) split()
b) explode()
c) divide()
d) break()

Answer:

b) explode()

Explanation:

The explode() function is used to split a string by a delimiter into an array.

13. How do you trim whitespace from the beginning and end of a string in PHP?

a) trim()
b) rtrim()
c) ltrim()
d) strtrim()

Answer:

a) trim()

Explanation:

The trim() function removes whitespace from the beginning and end of a string.

14. What does the substr() function do in PHP?

a) Finds a substring in a string
b) Replaces a substring in a string
c) Returns part of a string
d) Converts a substring to uppercase

Answer:

c) Returns part of a string

Explanation:

The substr() function is used to return a part of a string.

15. Which of the following characters is used to escape special characters in a string in PHP?

a) !
b) &
c) \
d) %

Answer:

c) \

Explanation:

The backslash (\) is used to escape special characters in a string in PHP.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top