Output of PHP programs | Set 1 (Regular Expressions)
Predict the output of following PHP programs:
Question 1
<?php echo str_pad ( "Welcome" , 5). " to w3wiki." ; ?> |
Options:
- WelcomeWelcomeWelcomeWelcomeWelcome to w3wiki.
- to w3wiki. WelcomeWelcomeWelcomeWelcomeWelcome
- to w3wiki. Welcome
- Welcome to w3wiki.
Output:
Welcome to w3wiki.
Explanation: The str_pad() function pads a string with a specified number of characters.
Question 2
<?php $author = "w3wiki" ; $author = str_replace ( "e" , "i" , $author ); echo "I am intern at $author." ; ?> |
Options:
- I am intern at w3wiki.
- I am intirn at GiiksforGiiks.
- I am intern at GiiksforGiiks.
- Error
Output:
I am intern at GiiksforGiiks.
Explanation: The str_replace() function case sensitively replaces all instances of a string with another.
Question 3
<?php $GfG = "w3wiki" ; echo ltrim( strstr ( $GfG , "f" ), "f" ); ?> |
Options:
- w3wiki
- Beginner
- Beginnerf
- orBeginner
Output:
orBeginner
Explanation: The strstr() function returns the remainder of a string beginning with the first occurrence of a predefined string.
Question 4
<?php $username = "sagarshUkla785" ; if ( ereg ( "([^a-z])" , $username )) echo "Not a valid username!" ; else echo "Valid username!" ; ?> |
Options:
- Error
- Not a valid username!
- Valid username!
- 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
<?php $GfG = "Hello\tWelcome to\nw3wiki." ; print_r(split( "[\n\t]" , $GfG )); ?> |
Options:
- Hello Welcome to w3wiki.
- Array ( [0] => Welcome to [1] => w3wiki. )
- Array ( [0] => Hello [1] => Welcome to [2] => w3wiki. )
- [0] => Hello [1] => Welcome to [2] => w3wiki.
Output:
[0] => Hello [1] => Welcome to [2] => w3wiki.
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
<?php $languages = array ( "C++" , "JAVA" , "PYTHON" , "SCALA" ); $language = preg_grep( "/^S/" , $languages ); print_r( $language ); ?> |
Options:
- Array ( [0] => C++ [1] => JAVA [2] => PYTHON [3] => SCALA )
- Array ( [3] => SCALA )
- Array ( [1] => JAVA )
- Array ( [0] => C++ )
Output:
Array ( [3] => SCALA )
Explanation: This function is used to search an array for languages beginning with S.
Question 7
<?php $title = "i'm intern at w3wiki." ; echo ucwords( $title ); ?> |
Options:
- I’m Intern At w3wiki
- I’m intern at w3wiki
- i’m Intern At w3wiki
- i’m intern at w3wiki
Output:
I'm Intern At w3wiki.
Explanation: The ucwords() function capitalizes the first letter of each word in a string. Its prototype follows: string ucwords(string str).
Contact Us