Output of PHP programs | Set 3
Predict the output of below PHP programs:
Question 1
<?php $number = array (0, 1, one, two, three, 5); $num = preg_grep( "/[0-5]/" , $number ); print_r( $num ); ?> |
Options:
- Array([0]=>0 [1]=>1 [2]=>one [3]=>two [4]=>three [5]=>5)
- Array([2]=>one [3]=>two [4]=>three)
- Array([1]=> 1)
- Array([0]=>0 [1]=>1 [5]=>5)
Output:
Array([0]=>0 [1]=>1 [5]=>5)
Explanation: The preg_grep() function is used to search an array for specific patterns and then return a new array based on that filtering.
Question 2
<?php $number = array (0, 1, one, two, three, 5); $num = preg_grep( "/[0-5]/" , $number , PREG_GREP_INVERT); print_r( $num ); ?> |
Options:
- Array([0]=>0 [1]=>1 [2]=>one [3]=>two [4]=>three [5]=>5)
- Array([2]=>one [3]=>two [4]=>three)
- Array([1]=> 1)
- Array([0]=>0 [1]=>1 [5]=>5)
Output:
Array([2]=>one [3]=>two [4]=>three)
Explanation: When we include PREG_GREP_INVERT, this will invert our data, so instead of outputting numbers it will output our non-numeric values.
Question 3
<?php $name = "I am intern at GeeksforGeeks." ; if (preg_match( "/at/" , $name )) echo "My name is Sagar Shukla" ; else echo "My name is not Sagar Shukla" ; ?> |
Options:
- My name is Sagar Shukla
- My name is not Sagar Shukla
- Error
- No Output
Output:
My name is Sagar Shukla
Explanation: The code uses preg_match() to check for a keyword and replies based on whether it is true (1) or false (0).
Question 4
<?php $name = "I am intern at GeeksforGeeks." ; if (preg_match( "/was/" , $name )) echo "My name is Sagar Shukla" ; else echo "My name is not Sagar Shukla" ; ?> |
Options:
- My name is Sagar Shukla
- My name is not Sagar Shukla
- Error
- No Output
Output:
My name is not Sagar Shukla
Explanation: The code uses preg_match to check for a keyword and replies based on whether it is true (1) or false (0).
Question 5
<?php $str = "I am intern at GeeksforGeeks" ; $find = array ( '/am/' ); $replace = array ( 'was' ); echo preg_replace ( $find , $replace , $str ); ?> |
Options:
- I am intern at GeeksforGeeks
- I was intern at GeeksforGeeks
- Error
- No Output
Output:
I was intern at GeeksforGeeks
Explanation: In the above program, am replaced with was as preg PHP function is used to do a find and replace on a string or an array.
Question 6
<?php $str = "I am intern at GeeksforGeeks" ; $find = array ( '/geeksforgeeks/' ); $replace = array ( 'GEEKSFORGEEKS' ); echo preg_replace ( $find , $replace , $str ); ?> |
Options:
- I am intern at GeeksforGeeks
- I am intern at GEEKSFORGEEKS
- Error
- No Output
Output:
I am intern at GeeksforGeeks
Explanation: GeeksforGeeks was not replaced because the preg_replace function is case sensitive. Therefore it treats GeeksforGeeks and geeksforgeeks differently.
Question 7
<?php $line = "Hello. Welcome to GeeksforGeeks!" ; $sen = preg_split( '/\./' , $line ); print_r( $sen ); ?> |
Options:
- Hello. Welcome to GeeksforGeeks!
- Array([0]=> Hello. Welcome to GeeksforGeeks!)
- Array([0]=> Hello [1]=> Welcome to GeeksforGeeks! )
- Error
Output:
Array([0]=> Hello [1]=> Welcome to GeeksforGeeks! )
Explanation: We use a ‘.’ period to split the data, therefore giving each sentence it’s own array entry.
Please Login to comment...