PHP Coding Questions and Answers – Functions – 6

This set of PHP Question Paper focuses on “Functions – 6”.

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

  1. <?php
  2. function colour()
  3. { 
  4.     $colors = array("red", "green", "blue", "yellow"); 
  5.     foreach ($colors as $value)
  6.     {
  7.         echo "$value <br>";
  8.     }
  9. }
  10. colour();
  11. ?>

a)

  red 
  green 
  blue 
  yellow 
advertisement
advertisement

b)

  green 
  blue 
  yellow 
  red

c)

  red  
  blue 
  yellow
  green

d)

  red 
  green  
  yellow  
  blue
View Answer
Answer: a
Explanation: For each function causes the program to loop through each array value once.
 
 

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

advertisement
  1. <?php
  2. function addFunction($num1, $num2)
  3. {
  4.     $sum = $num1 + $num2;
  5.     return $sum;
  6. }
  7. $return_value = addFunction(10, 20);
  8. echo "Returned value from the function : " .$return_value
  9. ?>

a) Returned value from the function : $return_value
b) Error
c) Returned value from the function : 30
d) Returned value from the function :
View Answer

Answer: c
Explanation: Function returns 30.
advertisement

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

  1. <?php
  2. function time($string)
  3. {
  4.     echo strtr("Towe Pa55", "ow5", $string);
  5. }
  6. time("ims");
  7. ?>

a) Time Pa55
b) Towe Pa55
c) Towe Pass
d) Time Pass
View Answer

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

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

  1. <?php
  2. function constant()
  3. {
  4.     define("GREETING", "Welcome to Narnia");
  5.     echo greeting;
  6. }
  7. ?>

a) Welcome to Narnia
b) greeting
c) GREETING
d) ERROR
View Answer

Answer: d
Explanation: By default constants are case sensitive. Hence an error will arise.

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

  1. <?php
  2. function constant()
  3. {
  4.     define("GREETING", "Welcome to Narnia",true);
  5.     echo greeting;
  6. }
  7. ?>

a) Welcome to Narnia
b) greeting
c) GREETING
d) ERROR
View Answer

Answer: a
Explanation: By default constants are case sensitive. But the third parameter in define(), if set to true, makes constants case insensitive.

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

  1. <?php
  2. function start($string)
  3. {
  4.     if ($string < 45)
  5.         return 20;
  6.     else
  7.         return 40;
  8. }
  9. $t = start(90);
  10. if ($t < 20)
  11. {
  12.     echo "Have a good day!";
  13. }
  14. else
  15. {
  16.     echo "Have a good night!";
  17. }
  18. ?>

a) Have a good day!
b) Have a good night!
c) ERROR
d) None of the mentioned
View Answer

Answer: b
Explanation: Function returns 40. This is greater than 20, hence the output.

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

  1. <?php
  2. function case()
  3. {
  4.     ECHO "Hello World!<br>";
  5.     echo "Hello World!<br>";
  6.     EcHo "Hello World!<br>";
  7. }
  8. case();
  9. ?>

a) Hello World!
b)

  Hello World!
  Hello World!

c)

  Hello World!
  Hello World!
  Hello World!

d) None of the mentioned
View Answer

Answer: c
Explanation: Functions, keywords etc in php are case insensitive.

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

  1. <?php
  2. function email()
  3. {
  4.     $email = ’user@yahoo.com’;
  5.     $new = strstr($email,@');
  6.     echo $new;
  7. }
  8. email();
  9. ?>

a) user
b) [email protected]
c) @yahoo.com
d) yahoo.com
View Answer

Answer: c
Explanation: The strstr() function searches for the first occurrence of a string inside another string.

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

  1. <?php
  2. function string()
  3. {
  4.     echo strstr("Hello world!", 111);
  5. }
  6. string();
  7. ?>

a) o world!
b) Hello world!
c) 111
d) No Output
View Answer

Answer: a
Explanation: 111 is the ASCII value of o.

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

  1. <?php
  2. function CalAll($x,$y)
  3. {
  4.     echo ($x + $y);
  5.     echo "<br>";
  6.     echo ($x - $y);
  7.     echo "<br>";
  8.     echo ($x * $y);
  9.     echo "<br>";
  10.     echo ($x / $y); 
  11.     echo "<br>";
  12.     echo ($x % $y);
  13. }
  14. $x = 10; 
  15. $y = 6;
  16. CalcAll(); 
  17. ?>

a)

  4
  60
  1.6666666666667
  4
  16

b)

  16
  4
  60
  1.6666666666667
  4

c)

  4
  16
  4
  60
  1.6666666666667

d)

  1.6666666666667
  4
  16
  4
  60
View Answer
Answer: b
Explanation: Simple usage of all arithmetic operators.

Sanfoundry Global Education & Learning Series – PHP Programming.

To practice all questions papers on 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.