PHP Functions | PHP Questions

Que. Which one of the following is the right way of defining a function in PHP?
a. functionName(parameters) { function body }
b. function { function body }
c. function fumctionName(parameters) { function body }
d. data type functionName(parameters) { function body }
Answer: function fumctionName(parameters) { function body }


Que. Which one of the following PHP functions can be used to find files?
a. file()
b. get_file()
c. fold()
d. glob()
Answer: glob()


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


Que. Output will be:
$array = array(0, 1, 2);
$array = array_pad($array, -6, ‘NEW’);
a. Array ( [0] => NEW [1] => NEW [2] => NEW [3] => 0 [4] => 1 [5] => 2 )
b. Array ( [0] => NEW [-1] => NEW [-2] => NEW [-3] => 0 [-4] => 1 [-5] => 2 )
c. Array ( [-1] => NEW [-2] => NEW [-3] => NEW [-4] => 0 [-5] => 1 [-6] => 2 )
d. Array ( [1] => NEW [2] => NEW [3] => NEW [4] => 0 [5] => 1 [6] => 2 )
Answer: Array ( [0] => NEW [1] => NEW [2] => NEW [3] => 0 [4] => 1 [5] => 2 )


Que. Which of the following PHP functions can be used for generating unique id’s?
a. id()
b. md5()
c. uniqueid()
d. mdid()


uniqueid()


Que. The below statement will return.
$Var = substr(""abcdef"", -4, 1);
a. returns "d"
b. returns "c"
c. returns "f"
d. returns "cd"
Answer: returns "c"


Que. What will be the output of the following PHP code?
echo ucwords("i love my country");
?>
a. I Love My Country
b. I love my country
c. I love my Country
d. i love my Country
Answer: I Love My Country


Que. What will be the output of the following PHP code?
span>
function one()
{
echo " this works";
function two()
{
echo "this too works";
}
}
one();
two();
?>
a. this worksthis too works
b. error
c. this works this too works
d. this works
Answer: this worksthis too works


Que. What will be the output of the following PHP code ?
function _func()
{
echo "Hello World";
}
_func();
?>
a. ERROR
b. No Output
c. Hello World
d. None of the mentioned
Answer: Hello World


Que. What will be the output of the following PHP code ?
function calc($num1, $num2)
{
$total = $num1 * $num2;
return $total;
}
$result = calc(42, 0);
echo $result;
?>
a. 42
b. Error
c. 0
d. 84


0


Que. What will be the output of the following PHP code ?
function mine($num)
{
$num = 2 + $num;
echo "$num";
}
mine(3);
?>
a. None of the mentioned
b. 5
c. $num
d. 3
Answer: $num


Que. Which of the following PHP functions can be used to get the current memory usage?
a. get_peak_usage()
b. get_memory_peak_usage()
c. get_memory_usage()
d. get_usage()
Answer: get_memory_usage()


Que. What will be the output of the following PHP code?
function do($myString)
{
echo strpos($myString, "donkey",0);
}
do("The donkey looks like a horse.");
?>
a. 4
b. 2
c. 5
d. None of the mentioned
Answer: 4


Que. What will be the output of the following PHP code ?
function movie($int)
{
$movies = array("Fight Club", "Kill Bill", "Pulp Fiction");
echo "You Do Not Talk About ". $movie[$integer];
}
movie(0);
?>
a. I
b. You Do Not Talk About Pulp Fiction
c. You Do Not Talk About Kill Bill
d. You Do Not Talk About Fight Club
Answer: mentioned


Que. What will be the output of the following PHP code ?
function string()
{
echo strstr("Hello world!", ;
}
string();
?>
a. o world!
b. Hello world!
c. No Output
d. 111


o world!


Que. What will be the output of the following PHP code?
echo "chr(";
?>
a. 4
b. 3
c. 1
d. 2
Answer: 4


Que. What will be the output of the following PHP code?
echo "chr(";
?>
a. 2
b. 4
c. 3
d. 1
Answer: 4


Que. Above usleep() function pauses PHP for .
usleep(1000;
a. 1 second
b. 1 microseconds
c. 10 seconds
d. 100 microseconds
Answer: 1 second


Que. 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. Error
b. I am bI am a
c. I am a Error
d. I am b
Answer: I am a Error


Que. The arguments in a function are evaluated from .....
a. right to left
b. left to right
c. sometimes left to right and sometimes right to left
d. Always right to left


left to right


Que. What will be the output of the following PHP code?
$op2 = "blabla";
function foo($op1)
{
echo $op1;
echo $op2;
}
foo("hello");
?>
a. Error
b. helloblablablabla
c. hello
d. helloblabla
Answer: hello


Que. What will be the output of the following PHP code?
$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));
?>
a. Array ( [0] => Hello [0] => world. [0] => It’s [0] => a [0] => beautiful [0] => day. )
b. Array ( [0] => Hello [1] => world. [2] => It’s [3] => a [4] => beautiful [5] => day. )
c. Array ( [1] => Hello [2] => world. [3] => It’s [4] => a [5] => beautiful [6] => day. )
d. Hello world. It’s a beautiful day
Answer: Array ( [0] => Hello [1] => world. [2] => It’s [3] => a [4] => beautiful [5] => day. )


Que. What will be the output of the following PHP code?
$str = addslashes('What does "yolo" mean?');
echo($str);
?>
a. What does ”yolo” mean?
b. What does ”yolo” mean?
c. What does ”yolo” mean?
d. What does /”yolo/” mean?
Answer: What does ”yolo” mean?


Que. Returning values from functions may include .....
a. None of above
b. Both A & B
c. Arrays
d. Objects
Answer: Both A & B


Que. What will be the output of the following PHP code?
define("GREETING","Hello you! How are you today?");
echo constant("GREETING");
?>
a. GREETING
b. “GREETING”,”Hello you! How are you today?”
c. Hello you! How are you today?
d. GREETING, Hello you! How are you today?


Hello you! How are you today?


Que. What will be the output of the following PHP code ?
function A1($x)
{
switch($x)
{
case 1:
//this statement is the same as if($x == 1)
echo 'Case 1 was executed.';
break;
case 2:
//this statement is the same as if($x == 2)
echo 'Case 2 was executed.';
break;
case 3:
//this statement is the same as if($x == 3)
echo 'Case 3 was executed.';
break;
case 4:
//this statement is the same as if($x == 4)
echo 'Case 4 was executed.';
break;
default:
//this statement is the same as if $x does not equal the other conditions
echo 'Default was executed.';
break;
}
}
A1(9);
?>
a. Case 4 was executed
b. Case 2 was executed
c. Default was executed
d. Case 1 was executed
Answer: Case 4 was executed


Que. Which one of the following functions can be used to compress a string?
a. zip_compress()
b. compress()
c. zip()
d. gzcompress()
Answer: gzcompress()


Que. What will be the output of the following PHP code ?
$x = 75;
$y = 25;
function addition()
{
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}
addition();
echo $z;
?>
a. 100
b. 75
c. 25
d. error
Answer: 100


Que. What will be the output of the following PHP code ?
function uppercase($string)
{
echo ucwords($string);
}
$wow = "uppercase";
$wow("Time to live king size");
?>
a. Uppercase
b. TIME TO LIVE KING SIZE
c. Time To Live King Size
d. Time to live king size
Answer: Time To Live King Size


Que. strlen() returns the length of the string on success and . . . . if the string is empty.
a. Garbage value
b. 0
c. NULL
d. -1


0


Que. What will be the output of the following PHP code ?
function test($int)
{
if ($int == 1)
echo "This Works";
if ($int == 2)
echo "This Too Seems To Work";
}
test(1);
TEST(2);
?>
a. This WorksThis Too Seems To Work
b. This Too Seems To Work
c. ERROR
d. This Works
Answer: This WorksThis Too Seems To Work


Que. What will be the output of the following PHP code ?
function addFunction($num1, $num2)
{
$sum = $num1 + $num2;
return $sum;
}
$return_value = addFunction(10, ;
echo "Returned value from the function : " .$return_value
?>
a. Returned value from the function : 30
b. Error
c. Returned value from the function :
d. Returned value from the function : $return_value
Answer: Returned value from the function : 30


Que. What will happen in this function call?
function calc($price, $tax)
{
$total = $price + $tax;
}
$pricetag = 15;
$taxtag = 3;
calc($pricetag, $taxtag);
?>
a. Type Hinting
b. Call By Value
c. Call By Reference
d. Default Argument Value
Answer: Call By Value


Que. What will be the output of the following PHP code?
function foo($msg)
{
echo "$msg";
}
$var1 = "foo";
$var1("will this work");
?>
a. $msg
b. 0
c. Error
d. Will this work
Answer: Will this work


Que. What will be the output of the following PHP code ?
function calc($num1, $num2)
{
$total = $num1 * $num2;
}
$result = calc(42, 0);
echo $result;
?>
a. 42
b. 84
c. 0
d. Error


Error


Que. What will be the output of the following PHP code ?
function mine($num)
{
$num = 2 + $num;
echo $num;
}
mine(3);
?>
a. 5
b. 3
c. None of the mentioned
d. $num
Answer: 5


Que. What will be the output of the following PHP code ?
function 2myfunc()
{
echo "Hello World";
}
2myfunc();
?>
a. Hello World
b. ERROR
c. No Output
d. None of the mentioned
Answer: ERROR


Que. What will be the output of the following PHP code?
echo ord ("hi");
?>
a. 106
b. 103
c. 104
d. 209
Answer: 104


Que. What will be the output of the following PHP code ?
function mine($m)
{
if ($m < 0)
echo "less than 0";
if ($ >= 0)
echo "Not True";
}
mine(0);
?>
a. Less Than 0
b. No Output
c. Not True
d. None of the Mentioned
Answer: Not True


Que.  . . . . . converts the keys of an array into values and the values into keys.
a. array_flip()
b. array_flips()
c. array_transpose()
d. array_trans()


array_flip()


Que. What will be the output of the following PHP code?
echo lcfirst("welcome to India");
?>
a. welcome to india
b. welcome to India
c. Welcome to India
d. Welcome to india
Answer: welcome to India


Que. 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. Error
b. I am b
c. I am bI am a
d. I am a Error
Answer: Error


Que. What will be the output of the following PHP code?
$title = "O'malley wins the heavyweight championship!";
echo ucwords($title);
?>
a. O’Malley wins the heavyweight championship!
b. O’Malley Wins The Heavyweight Championship!
c. O’malley Wins The Heavyweight Championship!
d. o’malley wins the heavyweight championship!
Answer: o’malley wins the heavyweight championship!


Que. What will be the output of the following PHP code?
function calc($price, $tax="")
{
$total = $price + ($price * $tax);
echo "$total";
}
calc(;
?>
a. Error
b. 0
c. 84
d. 42
Answer: 42


Que. What will be the output of the following PHP code?
$str = "Hello World"
echo wordwrap($str,5,"n");
?>
a. World
b. Hello World
c. Hell 0 Wo rld
d. HelloWorld


HelloWorld


Que. 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. Error
c. I am bI am a
d. I am a Error
Answer: Error


Que. Which one of the following PHP functions can be used to build a function that accepts any number of arguments?
a. get_argc()
b. func_get_argc()
c. get_argv()
d. func_get_argv()
Answer: func_get_argc()


Que. What will be the output of the following PHP code?
echo strtr("Hilla Warld","ia","eo");
?>
a. ia
b. Hello World
c. eo
d. Hilla Warld
Answer: Hello World


Que. What will be the output of the following PHP code ?
$var = 10;
function one()
{
echo $var;
}
one();
?>
a. 10
b. None of the Mentioned
c. Error
d. No Output
Answer: No Output


Que. What will be the output of the following PHP code?
function addFunction($num1, $num2)
{
$sum = $num1 + $num2;
return $sum;
}
$return_value = addFunction(10, ;
echo "Returned value from the function : $return_value"
?>
a. Error
b. Returned value from the function :
c. Returned value from the function : $return_value
d. Returned value from the function : 30


Returned value from the function : 30


Que. What will be the output of the following PHP code?
echo ord ("hi");
?>
a. 104
b. 106
c. 103
d. 209
Answer: 104


Que. What will be the output of the following PHP code?
function sum($num1, $num2)
{
$total = $num1 + $num2;
echo "chr($total)";
}
$var1 = "sum";
$var1(5, ;
?>
a. Error
b. Sum
c. 1
d. 49
Answer: 1


Que. What will be the output of the following PHP code?
echo ucwords("i love my country");
?>
a. I love my Country
b. I love my country
c. i love my Country
d. I Love My Country
Answer: I Love My Country


Que. A function in PHP which starts with ______ (double underscore) is know as.
a. Default Function
b. Inbuilt Function
c. Magic Function
d. User Defined Function
Answer: Magic Function


Que. What will be the output of the following PHP code?
echo stripos("I love php, I love php too!","PHP");
?>
a. 10
b. 7
c. 3
d. 8


7


Que. It is always necessary to use parentheses with the print function.
a. NA
b. Option B
c. False
d. True
Answer: na


Que. What will be the output of the following PHP code?
function sayHello()
{
echo "HelloWorld
";
}
$function_holder = "sayHello";
$function_holder();
?>
a. HelloWorld
b. sayHello
c. No Output
d. Error
Answer: HelloWorld


Que. What will be the output of the following PHP code ?
function TV($string)
{
echo "my favourite TV show is ".$string;
function b()
{
echo " I am here to spoil this code";
}
}
a("Sherlock");
b();
?>
a. Error
b. None of the mentioned
c. I am here to spoil this code
d. my favourite TV show is SherlockI am here ro spoil this code
Answer: my favourite TV show is SherlockI am here ro spoil this code


Que. 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. Error
c. I am a Error
d. I am bI am a
Answer: I am a Error


Que. What will be the output of the following PHP code?
function calc($price, $tax="")
{
$total = $price + ($price * $tax);
echo "$total";
}
calc(;
?>
a. Error
b. 0
c. 42
d. 84


42


Que.  . . . . returns a new DateTime object.
a. date_sunrise()
b. date()
c. getdate()
d. date_create()
Answer: date_create()


Que. What will be the output of the following PHP code?
function b()
{
echo "b is executed";
}
function a()
{
b();
echo "a is executed";
b();
}
a();
?>
a. b is executeda is executed
b. b is executedb is executedb is executed
c. b is executeda is executedb is executed
d. a is executed
Answer: b is executeda is executedb is executed


Que. What will be the output of the following PHP code?
echo hex2bin("48656c6c6f20576f726c6421");
?>
a. Hello World!
b. welcome to india
c. MCQ questons
d. This is PHP!
Answer: Hello World!


Que. What will be the output of the following PHP code?
echo str_pad("Salad", 5)." is good.";
?>
a. Salad is good
b. is good SaladSaladSaladSaladSalad
c. SaladSaladSaladSaladSalad is good
d. is good Salad
Answer: Salad is good


Que. 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. helloblablablabla
d. hello


hello


Que. What will be the output of the following PHP code?
echo lcfirst("welcome to India");
?>
a. Welcome to india
b. welcome to India
c. Welcome to India
d. welcome to india
Answer: welcome to India


Que. What will be the output of the following PHP code?
function sum($num1, $num2)
{
$total = $num1 + $num2;
echo "cos($total)";
}
sum(5,-5);
?>
a. 0
b. 1
c. -0.5
d. 0.5
Answer: 1


Que. phpinfo() will display about?
1. OS version information
2. PHP installation
3. Variables
4. HTTP headers

a. Option 3
b. Option 4
c. Option 1
d. Option 2
Answer:


Que. What will be the output of the following PHP code ?
function TV($string)
{
echo "my favourite TV show is ".$string;
function b()
{
echo " I am here to spoil this code";
}
}
function b()
{
echo " I am here to spoil this code";
}
b();
?>
a. I am here to spoil this code
b. my favourite TV show isI am here to spoil this code
c. None of the mentioned
d. Error
Answer: I am here to spoil this code


Que. 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. 3
c. 4
d. 0




Que. What will be the output of the following PHP code ?
function TV($string)
{
echo "my favourite TV show is ".$string;
function b()
{
echo " I am here to spoil this code";
}
}
function b()
{
echo " I am here to spoil this code";
}
b();
TV("Sherlock");
?>
a. Error
b. I am here to spoil this code
c. None of the mentioned
d. My favourite TV show isI am here to spoil this code
Answer: Error


Que. What will be the output of the following PHP code ?
function TV($string)
{
echo "my favourite TV show is ".$string;
function b()
{
echo " I am here to spoil this code";
}
}
b();
?>
a. Error
b. None of the mentioned
c. I am here to spoil this code
d. My favourite TV show isI am here to spoil this code
Answer: Error


Que. Which of the following are valid function names?
1. function()
2. €()
3. .function()
4. $function()
a. Only 2
b. All of the mentioned
c. None of the mentioned
d. 3 and 4
Answer: Only 2


Comments