PHP Interview Questions and Answers for Experienced people on “Strings and Regular Expressions – 2”.
1. How many functions does PHP offer for searching and modifying strings using Perl-compatible regular expressions.
a) 7
b) 8
c) 9
d) 10
Answer: b
Clarification: The functions are preg_filter(), preg_grep(), preg_match(), preg_match_all(), preg_quote(), preg_replace(), preg_replace_callback(), and preg_split().
2. What will be the output of the following PHP code?
-
-
$foods = array("pasta", "steak", "fish", "potatoes");
-
$food = preg_grep("/^s/", $foods);
-
print_r($food);
-
?>
a) Array ( [0] => pasta [1] => steak [2] => fish [3] => potatoes )
b) Array ( [3] => potatoes )
c) Array ( [1] => steak )
d) Array ( [0] => potatoes )
Answer: c
Clarification: This function is used to search an array for foods beginning with s.
3. Say we have two compare two strings which of the following function/functions can you use?
i) strcmp() ii) strcasecmp() iii) strspn() iv) strcspn()
a) i) and ii)
b) iii) and iv)
c) only i)
d) i), ii), iii) and iv)
Answer: d
Clarification: All of the functions mentioned above can be used to compare strings in some or the other way.
4. Which one of the following functions will convert a string to all uppercase?
a) strtoupper()
b) uppercase()
c) str_uppercase()
d) struppercase()
Answer: a
Clarification: Its prototype follows string strtoupper(string str).
5. What will be the output of the following PHP code?
-
-
$title = "O'malley wins the heavyweight championship!";
-
echo ucwords($title);
-
?>
a) O’Malley Wins The Heavyweight Championship!
b) O’malley Wins The Heavyweight Championship!
c) O’Malley wins the heavyweight championship!
d) o’malley wins the heavyweight championship!
Answer: a
Clarification: The ucwords() function capitalizes the first letter of each word in a string. Its prototype follows: string ucwords(string str).
6. What will be the output of the following PHP code?
-
-
echo str_pad("Salad", 5)." is good.";
-
?>
a) SaladSaladSaladSaladSalad is good
b) is good SaladSaladSaladSaladSalad
c) is good Salad
d) Salad is good
Answer: d
Clarification: The str_pad() function pads a string with a specified number of characters.
7. Which one of the following functions can be used to concatenate array elements to form a single delimited string?
a) explode()
b) implode()
c) concat()
d) concatenate()
Answer: b
Clarification: None.
8. Which one of the following functions finds the last occurrence of a string, returning its numerical position?
a) strlastpos()
b) strpos()
c) strlast()
d) strrpos()
Answer: d
Clarification: None.
9. What will be the output of the following PHP code?
-
-
$author = "[email protected]";
-
$author = str_replace("a","@",$author);
-
echo "Contact the author of this article at $author.";
-
?>
a) Contact the author of this article at [email protected]@mple.com
b) [email protected] the @uthor of this @rticle @t [email protected]@[email protected]
c) Contact the author of this article at [email protected]@[email protected]
d) Error
Answer: c
Clarification: The str_replace() function case sensitively replaces all instances of a string with another.
10. What will be the output of the following PHP code?
a) [email protected]
b) nachiketh
c) [email protected]
d) example.com
Answer: d
Clarification: The strstr() function returns the remainder of a string beginning with the first occurrence of a predefined string.
PHP for Interviews,
