PHP Coding Questions and Answers – Functions – 2

This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Functions – 2”.

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

  1. <?php
  2.     function sum($num1, $num2)
  3.     {
  4.         $total = $num1 + $num2;
  5.         echo "chr($total)"; 
  6.     }
  7.     $var1 = "sum";
  8.     $var1(5, 44);    
  9. ?>

a) Error
b) 49
c) 1
d) Sum
View Answer

Answer: c
Explanation: It is possible to call a function using a variable which stores the function name also the chr() function returns a character from the specified ASCII value.
advertisement
advertisement

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

  1. <?php
  2.     function sum($num1, $num2)
  3.     {
  4.         $total = $num1 + $num2;
  5.         echo "cos($total)"; 
  6.     }
  7.     sum(5,-5);    
  8. ?>

a) 0
b) 1
c) 0.5
d) -0.5
View Answer

Answer: b
Explanation: cos() gives the cos value of the argument. Here the function returns 1.

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

advertisement
  1. <?php
  2.     function b()
  3.     {
  4.         echo "b is executed";
  5.     }
  6.     function a()
  7.     {
  8.         b();
  9.         echo "a is executed";
  10.         b();
  11.     }
  12.     a();
  13.   ?>

a) b is executedb is executedb is executed
b) b is executeda is executed
c) a is executed
d) b is executeda is executedb is executed
View Answer

Answer: d
Explanation: Simple order of execution.
advertisement

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

  1. <?php
  2. function sum($x, $y)
  3. {
  4.     $z = $x + $y;
  5.     return $z;
  6. }
  7. echo "5 + 10 = " . sum(7,13) . "<br>";
  8. echo "7 + 13 = " . sum(2,4) . "<br>";
  9. echo "2 + 4 = " . sum(5,10);
  10. ?>

a)

  5 + 10 = 15
  2 + 4 = 6
  7 + 13 = 20

b)

  7 + 13 = 20
  5 + 10 = 15
  2 + 4 = 6

c)

  5 + 10 = 15
  7 + 13 = 20
  2 + 4 = 6

d)

  5 + 10 = 20
  7 + 13 = 6
  2 + 4 = 15
View Answer
Answer: d
Explanation: The function calls are jumbled.
 
 

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

  1. <?php
  2. function addFive($num)
  3. {
  4.     $num += 5;
  5. }
  6. function addSix(&$num)
  7. {
  8.     $num += 6;
  9. }
  10. $orignum = 10;
  11. addFive( &$orignum );
  12. echo "Original Value is $orignum<br />";
  13. addSix( $orignum );
  14. echo "Original Value is $orignum<br />";
  15. ?>

a)

Original Value is 15
Original Value is 21

b)

Original Value is 15
Original Value is 21 

c)

Original Value is 15
Original Value is 15

d) None Of The mentioned
View Answer

Answer: b
Explanation: addSix() passes value of the variable by reference.

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

  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: Functions returns value 30.

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

  1. <?php
  2. function sayHello()
  3. {
  4.    echo "HelloWorld<br />";
  5. }
  6. $function_holder = "sayHello";
  7. $function_holder();
  8. ?>

a) No Output
b) Error
c) sayHello
d) HelloWorld
View Answer

Answer: d
Explanation: It is possible to assign function names as strings to variables and then treat these variables exactly as you would the function name itself.

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

  1. <?php
  2. function one()
  3. {
  4.     echo " this works";
  5.     function two()
  6.     {
  7.         echo "this too works";
  8.     }
  9. }
  10. one();
  11. two();
  12. ?>

a) error
b) this works
c) this worksthis too works
d) this works this too works
View Answer

Answer: c
Explanation: Two is declared in one and is called after one. Hence it works.

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

  1. <?php
  2. function do($myString)
  3. {
  4.     echo strpos($myString, "donkey",0);
  5. }
  6. do("The donkey looks like a horse.");
  7. ?>

a) 4
b) 5
c) 2
d) None of the mentioned
View Answer

Answer: a
Explanation: Donkey starts from position 4 in string.

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

  1. <?php
  2. function one()
  3. {
  4.     define("const","I am awesome!");
  5.     echo constant("const");
  6. }
  7. one();
  8. ?>

a) I am awesome!!
b) const
c) const, I am awesome!!
d) “const”,”I am awesome!”
View Answer

Answer: a
Explanation: Using the define function to define the constant “const”.

Sanfoundry Global Education & Learning Series – PHP Programming.

To practice all questions on PHP Programming, 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.