This set of PHP Questions and Answers for focuses 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
View Answer
Explanation: 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?
<?php
$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 )
View Answer
Explanation: 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)
View Answer
Explanation: 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()
View Answer
Explanation: Its prototype follows string strtoupper(string str).
5. What will be the output of the following PHP code?
<?php
$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!
View Answer
Explanation: 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?
<?php
echo str_pad("Salad", 5)." is good.";
?>
a) SaladSaladSaladSaladSalad is good
b) is good SaladSaladSaladSaladSalad
c) is good Salad
d) Salad is good
View Answer
Explanation: 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()
View Answer
Explanation: 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()
View Answer
Explanation: None.
9. What will be the output of the following PHP code?
<?php
$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 nachiketh@[email protected]
b) Cont@ct the @uthor of this @rticle @t n@chiketh@[email protected]
c) Contact the author of this article at n@chiketh@[email protected]
d) Error
View Answer
Explanation: 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?
<?php
$url = "[email protected]";
echo ltrim(strstr($url, "@"),"@");
?>
a) [email protected]
b) nachiketh
c) nachiketh@
d) example.com
View Answer
Explanation: The strstr() function returns the remainder of a string beginning with the first occurrence of a predefined string.
Sanfoundry Global Education & Learning Series – PHP Programming.
To practice all areas of PHP, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.
- Check Information Technology Books
- Apply for Programming Internship
- Check MCA Books
- Practice Programming MCQs
- Practice MCA MCQs