Skip to content
Related Articles

Related Articles

Improve Article

Output of PHP programs | Set 3

  • Last Updated : 07 Mar, 2018

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:

  1. Array([0]=>0 [1]=>1 [2]=>one [3]=>two [4]=>three [5]=>5)
  2. Array([2]=>one [3]=>two [4]=>three)
  3. Array([1]=> 1)
  4. 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:

  1. Array([0]=>0 [1]=>1 [2]=>one [3]=>two [4]=>three [5]=>5)
  2. Array([2]=>one [3]=>two [4]=>three)
  3. Array([1]=> 1)
  4. 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:

  1. My name is Sagar Shukla
  2. My name is not Sagar Shukla
  3. Error
  4. 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:

  1. My name is Sagar Shukla
  2. My name is not Sagar Shukla
  3. Error
  4. 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:

  1. I am intern at GeeksforGeeks
  2. I was intern at GeeksforGeeks
  3. Error
  4. 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:

  1. I am intern at GeeksforGeeks
  2. I am intern at GEEKSFORGEEKS
  3. Error
  4. 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:

  1. Hello. Welcome to GeeksforGeeks!
  2. Array([0]=> Hello. Welcome to GeeksforGeeks!)
  3. Array([0]=> Hello [1]=> Welcome to GeeksforGeeks! )
  4. Error

    Output:

    Array([0]=> Hello [1]=> Welcome to GeeksforGeeks! )
    

    Explanation: We use a ‘.’ period to split the data, therefor giving each sentence it’s own array entry.




    My Personal Notes arrow_drop_up
Recommended Articles
Page :