Output of PHP programs | Set 3

Predict the output of below PHP programs:

Question 1

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
    $number = array(0, 1, one, two, three, 5);
    $num = preg_grep("/[0-5]/", $number);
    print_r($num);
?>

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
    $number = array(0, 1, one, two, three, 5);
    $num = preg_grep("/[0-5]/", $number, PREG_GREP_INVERT);
    print_r($num);
?>

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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";
?>

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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";
?>

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
    $str = "I am intern at GeeksforGeeks";
    $find = array('/am/');
    $replace = array('was');
    echo preg_replace ($find, $replace, $str);
?>

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
    $str = "I am intern at GeeksforGeeks";
    $find = array('/geeksforgeeks/');
    $replace = array('GEEKSFORGEEKS');
    echo preg_replace ($find, $replace, $str);
?>

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
    $line = "Hello. Welcome to GeeksforGeeks!";
    $sen = preg_split('/\./', $line);
    print_r($sen);
?>

chevron_right


Options:

  1. Hello. Welcome to GeeksforGeeks!
  2. Array([0]=> Hello. Welcome to GeeksforGeeks!)
  3. Array([0]=> Hello [1]=> Welcome to GeeksforGeeks! )
  4. Error
  5. 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

    Intern at GeeksforGeeks

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




    Article Tags :
    Practice Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.