1. Which function is used to find the length of a string in PHP?
Answer:
Explanation:
The strlen() function is used to find the length of a string in PHP.
2. How do you concatenate two strings in PHP?
Answer:
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?
Answer:
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];
Answer:
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?
Answer:
Explanation:
The str_replace() function is used for string replacement in PHP.
6. Which function is used to reverse a string in PHP?
Answer:
Explanation:
The strrev() function is used to reverse a string in PHP.
7. How can you convert a string to lowercase in PHP?
Answer:
Explanation:
The strtolower() function is used to convert a string to lowercase in PHP.
8. What does the strpos() function do in PHP?
Answer:
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?
Answer:
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);
Answer:
Explanation:
strtoupper() converts all characters in a string to uppercase.
11. How do you access the last character of a string in PHP?
Answer:
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?
Answer:
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?
Answer:
Explanation:
The trim() function removes whitespace from the beginning and end of a string.
14. What does the substr() function do in PHP?
Answer:
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?
Answer:
Explanation:
The backslash (\) is used to escape special characters in a string in PHP.