PHP Coding Questions and Answers – Functions – 5

This set of PHP Assessment Questions and Answers focuses on “Functions – 5”.

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

  1. <?php 
  2. $x = 75;
  3. $y = 25; 
  4. function addition()
  5. {
  6.     $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
  7. }
  8. addition();
  9. echo $z;
  10. ?>

a) 100
b) error
c) 75
d) 25
View Answer

Answer: a
Explanation: z is a variable present within the $GLOBALS array, it is also accessible from outside the function!
advertisement
advertisement

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

  1. <?php
  2. function 2myfunc()
  3. {
  4.     echo "Hello World";
  5. }
  6. 2myfunc();
  7. ?>

a) Hello World
b) No Output
c) ERROR
d) None of the mentioned
View Answer

Answer: c
Explanation: Function cannot begin with a number.

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

advertisement
  1. <?php
  2. function _func()
  3. {
  4.     echo "Hello World";
  5. }
  6. _func();
  7. ?>

a) Hello World
b) No Output
c) ERROR
d) None of the mentioned
View Answer

Answer: a
Explanation: Function Beginning with “_” is valid.
advertisement

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

  1. <?php
  2. function test($int)
  3. {
  4.     if ($int == 1)
  5.         echo "This Works";
  6.     if ($int == 2)
  7.         echo "This Too Seems To Work";
  8. }
  9. test(1);
  10. TEST(2);
  11. ?>

a) This Works
b) This Too Seems To Work
c) This WorksThis Too Seems To Work
d) ERROR
View Answer

Answer: c
Explanation: Function Is case Insensitive.

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

  1. <?php
  2. function mine($num)
  3. {
  4.     $num = 2 + $num;
  5. echo $num;
  6. }
  7. mine(3);
  8. ?>

a) 3
b) $num
c) 5
d) None of the mentioned
View Answer

Answer: c
Explanation: Simple arithmetic operation.

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

  1. <?php
  2. function mine($num)
  3. {
  4.     $num = 2 + $num;
  5.     echo "$num";
  6. }
  7. mine(3);
  8. ?>

a) 3
b) $num
c) 5
d) None of the mentioned
View Answer

Answer: b
Explanation: The function is defined as echo “$num”. This means $num is treated as a string and not as a variable.

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

  1. <?php
  2. function one($string)
  3. {
  4.     echo "I am ". $String;
  5. }
  6. one("Batman");
  7. ?>

a)

I
 am Batman

b) I am
c) Batman
d) ERROR
View Answer

Answer: d
Explanation: Variable Undeclared) $string is not the same as $String.

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

  1. <?php
  2.     function string($title);
  3.     {
  4.         $title = ucwords($title);
  5.         echo lcfirst($title);
  6.     }
  7.     string("you went full retard");
  8. ?>

a) You went full retard
b) You Went Full Retard
c) YOU WENT FULL RETARD
d) you Went Full Retard
View Answer

Answer: d
Explanation: ucwords() changes all the first letters to capitals. lcfirst() changes first letter of a string to small.

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

  1. <?php
  2.     function multi($num)
  3.     {
  4.         if ($num == 3)
  5.             echo "I Wonder";
  6.         if ($num == 7)
  7.             echo "Which One";
  8.         if ($num == 8)
  9.             echo "Is The";
  10.         if ($num == 19)
  11.             echo "Correct Answer";
  12.     }
  13.     $can = stripos("I love php, I love php too!","PHP");
  14.     multi($can);
  15. ?>

a) I Wonder
b) Which One
c) Is The
d) Correct Answer
View Answer

Answer: b
Explanation: The stripos() function finds the position of the first occurrence of a string inside another string. In this case it returns 7.

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

  1. <?php
  2. function movie($int)
  3. {
  4.     $movies = array("Fight Club", "Kill Bill", "Pulp Fiction");
  5.     echo "You Do Not Talk About ". $movie[$integer];
  6. }
  7. movie(0);
  8. ?>

a) You Do Not Talk About Fight Club
b) You Do Not Talk About Kill Bill
c) You Do Not Talk About Pulp Fiction
d) None of the mentioned
View Answer

Answer: a
Explanation: Simple use of arrays.

Sanfoundry Global Education & Learning Series – PHP Programming.

To practice all areas of PHP Assessment Questions, 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.