This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Introduction to Preg in PHP”.
1. What will be the output of the following PHP code?
<?php
$number = array(0,1,two,three,four,5);
$num = preg_grep("/[0-5]/", $number);
print_r($num);
?>
a) Array([0]=>0 [1]=>1 [2]=>two [3]=>three [4]=>four [5]=>5)
b) Array([2]=>two [3]=>three [4]=>four)
c) Array([1]=> 1)
d) Array([0]=>0 [1]=>1 [5]=>5)
View Answer
Explanation: The preg_grep function is used to search an array for specific patterns and then return a new array based on that filtering.
2. What will be the output if we replace the line $num = preg_grep(“/[0-5]/”, $number); with $num = preg_grep(“/[0-5]/”, $number, PREG_GREP_INVERT);?
a) Array([0]=>0 [1]=>1 [2]=>two [3]=>three [4]=>four [5]=>5)
b) Array([2]=>two [3]=>three [4]=>four)
c) Array([1]=> 1)
d) Array([0]=>0 [5]=>5)
View Answer
Explanation: When we include PREG_GREP_INVERT, this will invert our data, so instead of outputting numbers it will output our non-numeric values.
3. Which one of the following functions are used to search a string?
a) preg_match
b) preg_search
c) preg_find
d) preg_found
View Answer
Explanation: The function preg_match() searches string for pattern and it returns true if pattern exists, and false otherwise. The function returns 1 if search was successful else returns 0.
4. What will be the output of the following PHP code?
<?php
$name = "What is your name?";
if (preg_match("/name/"),$name)
echo "My name is Will Pitt ";
else
echo "My name is not Will Pitt ";
if (preg_match("/are/"))
echo "I am great";
else
echo "I am not great";
?>
a) My name is Will Pitt I am great
b) My name is not Will Pitt I am great
c) My name is Will Pitt I am not great
d) My name is not Will Pitt I am not great
View Answer
Explanation: The code uses preg_match to check for a keyword and replies based on whether it is true (1) or false (0).
5. Which one of the following preg PHP function is used to do a find and replace on a string or an array?
a) preg_replace()
b) preg_find()
c) preg_find_replace()
d) preg_findre()
View Answer
Explanation: In preg_replace() function, after the replacement has occurred, the modified string will be returned and if no matches are found, the string will remain unchanged.
6. What will be the output of the following PHP code?
<?php
$str = "Hello! My name is Cameron Fox. Coffee?";
$find = array('/is/','/coffee/');
$replace = array('/was/','/tea/');
echo preg_replace ($find, $replace, $str);
?>
a) Hello! My name was Cameron Fox. tea?
b) Hello! My name is Cameron Fox. tea?
c) Hello! My name is Cameron Fox. Coffee?
d) Hello! My name was Cameron Fox. Coffee?
View Answer
Explanation: Coffee was not replaced because the preg_replace function is case sensitive. Therefore it treats coffee and Coffee differently.
7. Which one of the following preg PHP functions is used to take a string, and put it in an array?
a) preg_destroy()
b) preg_split()
c) preg_unchain()
d) preg_divide()
View Answer
Explanation: The string is broken up into different values in the array based upon your input.
8. What will be the output of the following PHP code?
<?php
$line = "You like dogs. I hate dogs. We should marry.";
$sen = preg_split('/\./', $line);
print_r($sen);
?>
a) You like dogs. I hate dogs. We should marry.
b) Array([0]=>You like dogs. I hate dogs. We should marry.)
c) Array([0]=>You like dogs. [1]=>I hate dogs. [2]=>We should marry.)
d) Error
View Answer
Explanation: We use a ‘.’ period to split the data, therefor giving each sentence it’s own array entry.
9. Which one of the following is not a preg PHP function?
a) preg_match
b) preg_match_all
c) preg_matchall
d) preg_split
View Answer
Explanation: The function preg_match_all() matches all occurrences of pattern in string.
10. Parameter flags was added in which version of PHP?
a) PHP 4.0
b) PHP 4.1
c) PHP 4.2
d) PHP 4.3
View Answer
Explanation: Parameter flags was added in PHP 4.3 version.
Sanfoundry Global Education & Learning Series – PHP Programming.
To practice all questions on PHP Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.
- Check PHP Books
- Apply for Programming Internship
- Check MCA Books
- Check Information Technology Books
- Practice Programming MCQs