This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Functions – 1”.
1. What will be the output of the following PHP code?
<?php
function calc($price, $tax="")
{
$total = $price + ($price * $tax);
echo "$total";
}
calc(42);
?>
a) Error
b) 0
c) 42
d) 84
View Answer
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?
<?php
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
View Answer
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?
<?php
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
View Answer
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?
<?php
$op2 = "blabla";
function foo($op1)
{
echo $op1;
echo $op2;
}
foo("hello");
?>
a) helloblabla
b) error
c) hello
d) helloblablablabla
View Answer
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?
<?php
function foo($msg)
{
echo "$msg";
}
$var1 = "foo";
$var1("will this work");
?>
a) error
b) $msg
c) 0
d) will this work
View Answer
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?
<?php
echo "chr(52)";
?>
a) 1
b) 2
c) 3
d) 4
View Answer
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?
<?php
echo ord ("hi");
?>
a) 106
b) 103
c) 104
d) 209
View Answer
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?
<?php
echo(atan(0.50));
?>
a) 0.11845976421345
b) 0.23568451142521
c) 0.46364760900081
d) 1
View Answer
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?
<?php
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?”
View Answer
Explanation: The define() function defines a constant.
10. What will be the output of the following PHP code?
<?php
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
View Answer
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.
- Check Information Technology Books
- Check MCA Books
- Apply for Programming Internship
- Practice MCA MCQs
- Check PHP Books