250+ TOP PHP Coding MCQs on Functions and Answers

PHP Assessment Questions and Answers on “Functions – 5”.

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

  1.  
  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
Answer: a
Clarification: z is a variable present within the $GLOBALS array, it is also accessible from outside the function!

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

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

a) Hello World
b) No Output
c) ERROR
d) None of the mentioned
Answer: c
Clarification: Function cannot begin with a number.

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

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

a) Hello World
b) No Output
c) ERROR
d) None of the mentioned
Answer: a
Clarification: Function Beginning with “_” is valid.

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

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

a) This Works
b) This Too Seems To Work
c) This WorksThis Too Seems To Work
d) ERROR
Answer: c
Clarification: Function Is case Insensitive.

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

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

a) 3
b) $num
c) 5
d) None of the mentioned
Answer: c
Clarification: Simple arithmetic operation.

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

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

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

Answer: b
Clarification: 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. function one($string)
  2. {
  3.     echo "I am ". $String;
  4. }
  5. one("Batman");
  6. ?>

a)

I
 am Batman

b) I am
c) Batman
d) ERROR
Answer: d
Clarification: Variable Undeclared) $string is not the same as $String.

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

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

a) You went full retard
b) You Went Full Retard
c) YOU WENT FULL RETARD
d) you Went Full Retard
Answer: d
Clarification: 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.     function multi($num)
  2.     {
  3.         if ($num == 3)
  4.             echo "I Wonder";
  5.         if ($num == 7)
  6.             echo "Which One";
  7.         if ($num == 8)
  8.             echo "Is The";
  9.         if ($num == 19)
  10.             echo "Correct Answer";
  11.     }
  12.     $can = stripos("I love php, I love php too!","PHP");
  13.     multi($can);
  14. ?>

a) I Wonder
b) Which One
c) Is The
d) Correct Answer
Answer: b
Clarification: 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. function movie($int)
  2. {
  3.     $movies = array("Fight Club", "Kill Bill", "Pulp Fiction");
  4.     echo "You Do Not Talk About ". $movie[$integer];
  5. }
  6. movie(0);
  7. ?>

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

Answer: a
Clarification: Simple use of arrays.

PHP Assessment Questions,

Leave a Comment

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

Scroll to Top