PHP Multiple Choice Questions on “Functions – 1”.
1. What will be the output of the following PHP code?
-
-
function calc($price, $tax="")
-
{ -
$total = $price + ($price * $tax);
-
echo "$total";
-
} -
calc(42);
-
?>
a) Error
b) 0
c) 42
d) 84
Answer: c
Clarification: 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?
-
-
function a()
-
{ -
function b()
-
{ -
echo 'I am b';
-
} -
echo 'I am a';
-
} -
a();
-
a();
-
?>
a) I am b
b) I am bI am a
c) Error
d) I am a Error
Answer: d
Clarification: This will be the output- I am a Fatal error: Cannot redeclare b()
3. What will be the output of the following PHP code?
-
-
function a()
-
{ -
function b()
-
{ -
echo 'I am b';
-
} -
echo 'I am a';
-
} -
b();
-
a();
-
?>
a) I am b
b) I am bI am a
c) Error
d) I am a Error
Answer: c
Clarification: 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?
-
-
$op2 = "blabla";
-
function foo($op1)
-
{ -
echo $op1;
-
echo $op2;
-
} -
foo("hello");
-
?>
a) helloblabla
b) error
c) hello
d) helloblablablabla
Answer: c
Clarification: 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?
-
-
function foo($msg)
-
{ -
echo "$msg";
-
} -
$var1 = "foo";
-
$var1("will this work");
-
?>
a) error
b) $msg
c) 0
d) will this work
Answer: d
Clarification: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?
a) 1
b) 2
c) 3
d) 4
Answer: d
Clarification: 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?
-
-
echo ord ("hi");
-
?>
a) 106
b) 103
c) 104
d) 209
Answer: c
Clarification: 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?
-
-
echo(atan(0.50));
-
?>
a) 0.11845976421345
b) 0.23568451142521
c) 0.46364760900081
d) 1
Answer: c
Clarification: 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?
-
-
define("GREETING","Hello you! How are you today?");
-
echo constant("GREETING");
-
?>
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?”
Answer: a
Clarification: The define() function defines a constant.
10. What will be the output of the following PHP code?
-
-
define("GREETING1","Hello you! How are you today?");
-
define("GREETING2","Hello you! How are you today?");
-
define("GREETING3","Hello you! How are you today?");
-
echo defined("GREETING");
-
?>
a) 1
b) 0
c) 3
d) 4
