Output of PHP programs | Set 1 (Regular Expressions)

Predict the output of following PHP programs:

Question 1

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
    echo str_pad("Welcome", 5)." to GeeksforGeeks.";
?>

chevron_right


Options:

  1. WelcomeWelcomeWelcomeWelcomeWelcome to GeeksforGeeks.
  2. to GeeksforGeeks. WelcomeWelcomeWelcomeWelcomeWelcome
  3. to GeeksforGeeks. Welcome
  4. Welcome to GeeksforGeeks.

Output:

Welcome to GeeksforGeeks.

Explanation: The str_pad() function pads a string with a specified number of characters.

Question 2

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
    $author = "GeeksforGeeks";
    $author = str_replace("e","i",$author);
    echo "I am intern at $author.";
?>

chevron_right


Options:

  1. I am intern at GeeksforGeeks.
  2. I am intirn at GiiksforGiiks.
  3. I am intern at GiiksforGiiks.
  4. Error

Output:

I am intern at GiiksforGiiks.

Explanation: The str_replace() function case sensitively replaces all instances of a string with another.

Question 3

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
    $GfG = "GeeksforGeeks";
    echo ltrim(strstr($GfG, "f"),"f");
?>

chevron_right


Options:

  1. GeeksforGeeks
  2. Geeks
  3. Geeksf
  4. orGeeks

Output:

orGeeks

Explanation: The strstr() function returns the remainder of a string beginning with the first occurrence of a predefined string.

Question 4

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
    $username = "sagarshUkla785";
    if (ereg("([^a-z])",$username))
        echo "Not a valid username!";
    else
        echo "Valid username!";
?>

chevron_right


Options:

  1. Error
  2. Not a valid username!
  3. Valid username!
  4. No Output is returned

Output:

Not a valid username!

Explanation: Because the provided username is not all lowercase, ereg() will not return FALSE (instead returning the length of the matched string, which PHP will treat as TRUE), causing the message to output.

Question 5

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
    $GfG = "Hello\tWelcome to\nGeeksforGeeks.";
    print_r(split("[\n\t]",$GfG));
?>

chevron_right


Options:

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

Output:

[0] => Hello [1] => Welcome to [2] => GeeksforGeeks.

Explanation: The split() function divides a string into various elements, with the boundaries of each element based on the occurrence of a defined pattern within the string.

Question 6

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
    $languages = array("C++", "JAVA", "PYTHON", "SCALA");
    $language = preg_grep("/^S/", $languages);
    print_r($language);
?>

chevron_right


Options:

  1. Array ( [0] => C++ [1] => JAVA [2] => PYTHON [3] => SCALA )
  2. Array ( [3] => SCALA )
  3. Array ( [1] => JAVA )
  4. Array ( [0] => C++ )

Output:

Array ( [3] => SCALA )

Explanation: This function is used to search an array for languages beginning with S.

Question 7

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
    $title = "i'm intern at geeksforGeeks.";
    echo ucwords($title);
?>

chevron_right


Options:

  1. I’m Intern At GeeksforGeeks
  2. I’m intern at geeksforGeeks
  3. i’m Intern At GeeksforGeeks
  4. i’m intern at geeksforGeeks

Output:

I'm Intern At GeeksforGeeks.

Explanation: The ucwords() function capitalizes the first letter of each word in a string. Its prototype follows: string ucwords(string str).



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.