PHP Coding Questions and Answers – Functions – 1

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

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

  1.     <?php
  2.     function calc($price, $tax="")
  3.     {
  4.         $total = $price + ($price * $tax);
  5.         echo "$total"; 
  6.     }
  7.     calc(42);    
  8.     ?>

a) Error
b) 0
c) 42
d) 84
View Answer

Answer: c
Explanation: You can designate certain arguments as optional by placing them at the end of the list and assigning them a default value of nothing.
advertisement
advertisement

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

  1.     <?php
  2.     function a()
  3.     {
  4.         function b()
  5.         {
  6.             echo 'I am b';
  7.         }
  8.         echo 'I am a';
  9.     }
  10.     a();
  11.     a();
  12.     ?>

a) I am b
b) I am bI am a
c) Error
d) I am a Error
View Answer

Answer: d
Explanation: This will be the output- I am a Fatal error: Cannot redeclare b()
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

advertisement
  1.     <?php
  2.     function a()  
  3.     {
  4.         function b()
  5.         {
  6.             echo 'I am b';
  7.         }
  8.         echo 'I am a';
  9.     }
  10.     b();
  11.     a();
  12.     ?>

a) I am b
b) I am bI am a
c) Error
d) I am a Error
View Answer

Answer: c
Explanation: This will be the output- Fatal error: Call to undefined function b(). You cannot call a function which is inside a function without calling the outside function.
advertisement

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

  1.     <?php
  2.     $op2 = "blabla";
  3.     function foo($op1)
  4.     {
  5.         echo $op1;
  6.         echo $op2;
  7.     }
  8.     foo("hello");
  9.     ?>

a) helloblabla
b) error
c) hello
d) helloblablablabla
View Answer

Answer: c
Explanation: If you want to put some variables in function that was not passed by it, you must use “global”. Inside the function type global $op2.

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

  1.     <?php
  2.         function foo($msg)
  3.         {
  4.             echo "$msg";
  5.         }
  6.         $var1 = "foo";
  7.         $var1("will this work");
  8.     ?>

a) error
b) $msg
c) 0
d) will this work
View Answer

Answer: d
Explanation:It is possible to call a function using a variable which stores the function name.

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

  1.     <?php
  2.        echo "chr(52)";
  3.     ?>

a) 1
b) 2
c) 3
d) 4
View Answer

Answer: d
Explanation: The chr() function returns a character from the specified ASCII value. Since the ASCII value of 4 is 52, thus 4 was displayed.

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

  1.     <?php
  2.         echo ord ("hi");
  3.     ?>

a) 106
b) 103
c) 104
d) 209
View Answer

Answer: c
Explanation: The ord() function returns the ASCII value of the first character of a string. The ASCII value of h is 104, thus 104 was displayed.

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

  1.   <?php
  2.       echo(atan(0.50));
  3.   ?>

a) 0.11845976421345
b) 0.23568451142521
c) 0.46364760900081
d) 1
View Answer

Answer: c
Explanation: The atan() function returns the arc tangent of arg as a numeric value between -Pi/2 and Pi/2 radians.

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

  1.    <?php
  2.        define("GREETING","Hello you! How are you today?");
  3.        echo constant("GREETING");
  4.    ?>

a) Hello you! How are you today?
b) GREETING
c) GREETING, Hello you! How are you today?
d) “GREETING”,”Hello you! How are you today?”
View Answer

Answer: a
Explanation: The define() function defines a constant.

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

  1.     <?php
  2.         define("GREETING1","Hello you! How are you today?");
  3.         define("GREETING2","Hello you! How are you today?");
  4.         define("GREETING3","Hello you! How are you today?");
  5.         echo defined("GREETING");
  6.      ?>

a) 1
b) 0
c) 3
d) 4
View Answer

Answer: b
Explanation: The defined() function checks whether a constant exists.

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.