250+ TOP PHP Coding MCQs on Functions and Answers

PHP Question Paper on “Functions – 6”.

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

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

a)

  red 
  green 
  blue 
  yellow 

b)

  green 
  blue 
  yellow 
  red

c)

  red  
  blue 
  yellow
  green

d)

  red 
  green  
  yellow  
  blue

Answer: a
Clarification: For each function causes the program to loop through each array value once.

 
 

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

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

a) Returned value from the function : $return_value
b) Error
c) Returned value from the function : 30
d) Returned value from the function :
Answer: c
Clarification: Function returns 30.

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

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

a) Time Pa55
b) Towe Pa55
c) Towe Pass
d) Time Pass
Answer: d
Clarification: The strtr() function translates certain characters in a string.

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

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

a) Welcome to Narnia
b) greeting
c) GREETING
d) ERROR
Answer: d
Clarification: By default constants are case sensitive. Hence an error will arise.

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

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

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

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

a) Have a good day!
b) Have a good night!
c) ERROR
d) None of the mentioned
Answer: b
Clarification: Function returns 40. This is greater than 20, hence the output.

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

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

a) Hello World!
b)

  Hello World!
  Hello World!

c)

  Hello World!
  Hello World!
  Hello World!

d) None of the mentioned
Answer: c
Clarification: Functions, keywords etc in php are case insensitive.

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

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

a) user
b) [email protected]
c) @yahoo.com
d) yahoo.com
Answer: c
Clarification: 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. function string()
  2. {
  3.     echo strstr("Hello world!", 111);
  4. }
  5. string();
  6. ?>

a) o world!
b) Hello world!
c) 111
d) No Output
Answer: a
Clarification: 111 is the ASCII value of o.

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

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

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

Answer: b
Clarification: Simple usage of all arithmetic operators.

To practice all questions papers on PHP,

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top