What will be the output of following code

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

1. How to define a function in PHP?
a) function {function body}
b) data type functionName(parameters) {function body}
c) functionName(parameters) {function body}
d) function functionName(parameters) {function body}
View Answer

Answer: d
Explanation: PHP allows us to create our own user-defined functions. Any name ending with an open and closed parenthesis is a function. The keyword function is always used to begin a function.

2. Type Hinting was introduced in which version of PHP?
a) PHP 4
b) PHP 5
c) PHP 5.3
d) PHP 6
View Answer

Answer: b
Explanation: PHP 5 introduced the feature of type hinting. With the help of type hinting, we can specify the expected data type of an argument in a function declaration. First valid types can be the class names for arguments that receive objects and the other are array for those that receive arrays.

3. Which type of function call is used in line 8 in the following PHP code?

  1.     
  2.     function calc($price, $tax)	
  3.     {
  4.         $total = $price + $tax;
  5.     }
  6.     $pricetag = 15;
  7.     $taxtag = 3;
  8.     calc($pricetag, $taxtag);	
  9.     ?>

a) Call By Value
b) Call By Reference
c) Default Argument Value
d) Type Hinting
View Answer

Answer: a
Explanation: If we call a function by value, we actually pass the values of the arguments which are stored or copied into the formal parameters of the function. Hence, the original values are unchanged only the parameters inside the function changes.

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

  1.     
  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.

5. Which of the following are valid function names?

i) function()
ii)()
iii) .function()
iv) $function()

a) Only i)
b) Only ii)
c) i) and ii)
d) iii) and iv)
View Answer

Answer: b
Explanation: A valid function name can start with a letter or underscore, followed by any number of letters, numbers, or underscores. According to the specified regular expression ([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*), a function name like this one is valid.

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

  1.     
  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 a
b) I am bI am a
c) Error
d) I am a Error
View Answer

Answer: a
Explanation: The output will be “I am a” as we are calling a(); so the statement outside the block of function b() will be called.

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

  1.     
  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: The output will be Fatal error: Call to undefined function b(). You cannot call a function which is inside a function without calling the outside function first. It should be a(); then b();

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

  1.     
  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 u want to put some variables in function that was not passed by it, you must use “global”. Inside the function type global $op2.

9. A function in PHP which starts with __ (double underscore) is known as __________
a) Magic Function
b) Inbuilt Function
c) Default Function
d) User Defined Function
View Answer

Answer: a
Explanation: PHP functions that start with a double underscore – a “__” – are called magic functions in PHP. They are functions that are always defined inside classes, and are not stand-alone functions.

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

  1.     
  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.

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.

Next Steps:

  • Get Free Certificate of Merit in PHP Programming
  • Participate in PHP Programming Certification Contest
  • Become a Top Ranker in PHP Programming
  • Take PHP Programming Tests
  • Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  • Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

What will be the output of following code

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 & technical discussions at Telegram SanfoundryClasses.

What will be the output

1. What will be the output of the following PHP code? Explanation: If you need to output something onto the screen you'll need to use echo or print_r.

What will be the output of following PHP code ?

Solution(By Examveda Team) The chr() function returns a character from the specified ASCII value. Since the ASCII value of 4 is 52, thus 4 was displayed.

What will be the output

Explanation: This will be the output- Fatal error: Call to undefined function b().

What is an inner function in PHP?

In PHP inner functions are global and hence behave in the same way as if they had been declared outside of any containing function.