What will be the output of the following php code php function calc($price $tax= )

What will be the output of the following php code php function calc($price $tax= )

Menu
  • H

    Home
  • A

    Aptitude
  • E

    English
  • R

    Reasoning
  • D

    DI
  • G

    GK
  • C

    Current Affairs
  • I

    Interview
  • Computer
    • C

      Computer Fundamentals
    • N

      Networking
    • S

      SQL
    • D

      Database
  • Programming
    • C

      C Program
    • J

      Java Program
    • H

      HTML
    • C

      CSS
    • J

      Javascript
    • P

      PHP Program
  • Engineering
    • C

      Computer Science
    • E

      Electrical Engineering
    • M

      Mechanical Engineering
    • C

      Civil Engineering
    • C

      Chemical Engineering
  • More
    • B

      Banking Awareness
    • C

      Commerce
    • M

      Management
  • A

    Ask Question

  • Home
  • Aptitude
  • English
  • Reasoning
  • DI
  • GK
  • Current Affairs
  • Interview
  • Computer
    • Computer Fundamentals
    • Networking
    • SQL
    • Database
  • Programming
    • C Program
    • Java Program
    • HTML
    • CSS
    • Javascript
    • PHP Program
  • Engineering
    • Computer Science
    • Electrical Engineering
    • Mechanical Engineering
    • Civil Engineering
    • Chemical Engineering
  • More
    • Banking Awareness
    • Commerce
    • Management
  • Ask Question

What will be the output of the following php code php function calc($price $tax= )

What will be the output of the following PHP code?

A. Error

B. 0

C. 42

D. 84

Answer: Option C

Solution(By Examveda Team)

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


Join The Discussion

Related Questions on Functions

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

2. 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 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()

3. 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: 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.

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

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 the following php code php function calc($price $tax= )

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 of following code

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.

What will be the output of the following PHP code

PHP ECHO CHR(52); ?> Answer: D) ASCII value of 4 is 52, hence the output is 4.

What will be the output

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

What is PHP function with examples?

A function is a piece of code that takes another input in the form of a parameter, processes it, and then returns a value. A PHP Function feature is a piece of code that can be used over and over again and accepts argument lists as input, and returns a value. PHP comes with thousands of built-in features.