PHP Coding Questions and Answers – Functions – 3

This set of Advanced PHP Questions and Answers focuses on “Functions – 3”.

1. What will be the output of the following PHP code?

  1. <?php
  2.     $title = "O'malley wins the heavyweight championship!";
  3.     echo ucwords($title);
  4. ?>

a) O’Malley Wins The Heavyweight Championship!
b) O’malley Wins The Heavyweight Championship!
c) O’Malley wins the heavyweight championship!
d) o’malley wins the heavyweight championship!
View Answer

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

2. What will be the output of the following PHP code?

  1. <?php
  2.     echo str_pad("Salad", 5)." is good.";
  3. ?>

a) SaladSaladSaladSaladSalad is good
b) is good SaladSaladSaladSaladSalad
c) is good Salad
d) Salad is good
View Answer

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

3. What will be the output of the following PHP code?

advertisement
  1. <?php
  2.     $str = "Hello World"
  3.     echo wordwrap($str,5,"<br>\n");    
  4. ?>

a) Hello World
b)

Hello
World
advertisement

c)

Hell
o wo
rld

d) World
View Answer

Answer: b
Explanation: The wordwrap() function wraps a string into new lines when it reaches a specific length.

4. What will be the output of the following PHP code?

  1. <?php
  2.     echo ucwords("i love my country");
  3. ?>

a) I love my country
b) i love my Country
c) I love my Country
d) I Love My Country
View Answer

Answer: d
Explanation: The ucwords() function converts the first character of each word in a string to uppercase.

5. What will be the output of the following PHP code?

  1. <?php
  2.     echo lcfirst("welcome to India");
  3. ?>

a) welcome to India
b) welcome to india
c) Welcome to India
d) Welcome to india
View Answer

Answer: a
Explanation: The lcfirst() function converts the first character of a string to lowercase.

6. What will be the output of the following PHP code?

  1. <?php
  2.     echo hex2bin("48656c6c6f20576f726c6421");
  3. ?>

a) Hello World!
b) welcome to india
c) This is PHP!
d) MCQ questons
View Answer

Answer: a
Explanation: The hex2bin() function converts a string of hexadecimal values to ASCII characters.

7. What will be the output of the following PHP code?

  1. <?php 
  2.     $str = addslashes('What does "yolo" mean?');
  3.     echo($str); 
  4. ?>

a) What does /”yolo/” mean?
b) What does \\”yolo\\” mean?
c) What does \”yolo\” mean?
d) \What does \”yolo\” mean?\
View Answer

Answer: c
Explanation: The addslashes() function returns a string with backslashes in front of predefined characters.

8. What will be the output of the following PHP code?

  1. <?php
  2.     $str = "Hello world. It's a beautiful day.";
  3.     print_r (explode(" ",$str));
  4. ?>

a) Array ( [0] => Hello [0] => world. [0] => It’s [0] => a [0] => beautiful [0] => day. )
b) Array ( [0] => Hello [1] => world. [2] => It’s [3] => a [4] => beautiful [5] => day. )
c) Hello world. It’s a beautiful day
d) Array ( [1] => Hello [2] => world. [3] => It’s [4] => a [5] => beautiful [6] => day. )
View Answer

Answer: b
Explanation: The explode() function breaks a string into an array.

9. What will be the output of the following PHP code?

  1. <?php
  2.     echo strtr("Hilla Warld","ia","eo");
  3. ?>

a) Hilla Warld
b) Hello World
c) ia
d) eo
View Answer

Answer: b
Explanation: The strtr() function translates certain characters in a string.

10. What will be the output of the following PHP code?

  1. <?php
  2.     echo stripos("I love php, I love php too!","PHP");
  3. ?>

a) 3
b) 7
c) 8
d) 10
View Answer

Answer: b
Explanation: The stripos() function finds the position of the first occurrence of a string inside another string.

Sanfoundry Global Education & Learning Series – PHP Programming.

To practice advanced questions and answers on all areas of PHP, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.