PHP MCQ Questions

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 104

PHP Questions & Answers – Basics – 2

This set of PHP Interview Questions and Answers focuses on “Basics – 2”.
1. Which is the right way of declaring a variable in PHP?
i) $3hello
ii) $_hello
iii) $this
iv) $This
a) Only ii)
b) Only iii)
c) ii), iii) and iv)
d) ii) and iv)
View Answer
Answer: d
Explanation: A variable in PHP can not start with a number, also $this is mainly used to refer properties of a class so
we can’t use $this as a user define variable name.
2. What will be the output of the following code?
<?php
$foo = 'Bob';
$bar = &$foo;
$bar = "My name is $bar";
echo $bar;
echo $foo;
?>
a) Error
b) My name is BobBob
c) My name is BobMy name is Bob
d) My name is Bob Bob
View Answer
Answer: c
Explanation: Firstly, the line $bar = &$foo; will reference $foo via $bar. So $bar is assigned value Bob. Therefore
$bar = “My name is $bar”; will print My name is Bob($bar=Bob as said before).
3. Which of the following PHP statements will output Hello World on the screen?
i) echo (“Hello World”);
ii) print (“Hello World”);
iii) printf (“Hello World”);
iv) sprintf (“Hello World”);
a) i) and ii)
b) i), ii) and iii)
c) All of the mentioned
d) i), ii) and iv)
View Answer
Answer: b
Explanation: echo(), print() and printf() all three can be used to output a statement onto the screen. The sprintf()
statement is functionally identical to printf() except that the output is assigned to a string rather than rendered to the
browser.
4. What will be the output of the following PHP code?
<?php
$color = "maroon";
$var = $color[2];
echo "$var";
?>
a) a
b) Error
c) $var
d) r
View Answer
Answer: d
Explanation: PHP treats strings in the same fashion as arrays, allowing for specific characters to be accessed via
array offset notation. In an array, index always starts from 0. So in the line $var = $color[2]; if we count from start
‘r’ comes at index 2. So the output will be r.
5. What will be the output of the following PHP code?
<?php
$score = 1234;
$scoreboard = (array) $score;
echo $scoreboard[0];
?>
a) 1
b) Error
c) 1234
d) 2
View Answer
Answer: c
Explanation: The (array) is a cast operator which is used for converting values from other data types to array.
6. What will be the output of the following PHP code?
advertisement

<?php
$total = "25 students";
$more = 10;
$total = $total + $more;
echo "$total";
?>
a) Error
b) 35 students
c) 35
d) 25 students
View Answer
Answer: c
Explanation: The integer value at the beginning of the original $total string is used in the calculation. However if it
begins with anything but a numerical value, the value will be 0.
7. Which of the below statements is equivalent to $add += $add ?
a) $add = $add
b) $add = $add +$add
c) $add = $add + 1
d) $add = $add + $add + 1
View Answer
Answer: b
Explanation: a += b is an addition assignment whose outcome is a = a + b. Same can be done with
subtraction,multiplication,division etc.
8. Which statement will output $x on the screen?
a) echo “\$x”;
b) echo “$$x”;
c) echo “/$x”;
d) echo “$x;”;
View Answer
Answer: a
Explanation: A backslash is used so that the dollar sign is treated as a normal string character rather than prompt
PHP to treat $x as a variable. The backslash used in this manner is known as escape character.
9. What will be the output of the following code?
<?php
function track() {
static $count = 0;
$count++;
echo $count;
}
track();
track();
track();
?>
a) 123
b) 111
c) 000
d) 011
View Answer
Answer: a
Explanation: Because $count is static, it retains its previous value each time the function is executed.
10. What will be the output of the following PHP code?
<?php
$a = "clue";
$a .= "get";
echo "$a";
?>
a) get
b) true
c) false
d) clueget
View Answer
Answer: d
Explanation: ‘.’ is a concatenation operator. $a. = “get” is same as $a=$a.”get” where $a is having value of “clue” in
the previous statement. So the output will be clueget
PHP Questions & Answers – Basics – 3
This set of PHP Questions and Answers for Freshers focuses on “Basics – 3”.
1. What will be the output of the following PHP code?
<?php
$a = 5;
$b = 5;
echo ($a === $b);
?>
a) 5 === 5
b) Error
c) 1
d) False
View Answer
Answer: c
Explanation: === operator returns 1 if $a and $b are equivalent and $a and $b have the same type.
2. Which of the below symbols is a newline character?
a) \r
b) \n
c) /n
d) /r
View Answer
Answer: b
Explanation: PHP treats \n as newline character.
3. What will be the output of the following PHP code?
<?php
$num = 10;
echo 'What is her age? \n She is $num years old';
?>
a) What is her age? \n She is $num years old
b)
What is her age?
She is $num years old
c) What is her age? She is 10 years old
d)
What is her age?
She is 10 years old
View Answer
Answer: a
Explanation: When a string is enclosed within single quotes both variables and escape sequences will not be
interpreted when the string is parsed.

4. Which of the conditional statements is/are supported by PHP?


i) if statements
ii) if-else statements
iii) if-elseif statements
iv) switch statements
a) Only i)
b) i), ii) and iv)
c) ii), iii) and iv)
d) i), ii), iii) and iv)
View Answer
Answer: d
Explanation: All are conditional statements supported by PHP as all are used to evaluate different conditions during
a program and take decisions based on whether these conditions evaluate to true of false.
5. What will be the output of the following PHP code?
<?php
$team = "arsenal";
switch ($team) {
case "manu":
echo "I love man u";
case "arsenal":
echo "I love arsenal";
case "manc":
echo "I love manc"; }
?>
a) I love arsenal
b) Error
c) I love arsenalI love manc
d) I love arsenalI love mancI love manu
View Answer
Answer: c
Explanation: If a break statement isn’t present, all subsequent case blocks will execute until a break statement is
located.
advertisement

6. Which of the looping statements is/are supported by PHP?


i) for loop
ii) while loop
iii) do-while loop
iv) foreach loop
a) i) and ii)
b) i), ii) and iii)
c) i), ii), iii) and iv)
d) Only iv)
View Answer
Answer: c
Explanation: All are supported looping statements in PHP as they can repeat the same block of code a given number
of times, or until a certain condition is met.
7. What will be the output of the following PHP code?
<?php
$user = array("Ashley", "Bale", "Shrek", "Blank");
for ($x=0; $x < count($user); $x++) {
if ($user[$x] == "Shrek") continue;
printf ($user[$x]);
}
?>
a) AshleyBale
b) AshleyBaleBlank
c) ShrekBlank
d) Shrek
View Answer
Answer: b
Explanation: The continue statement causes execution of the current loop iteration to end and commence at the
beginning of the next iteration.
8. If $a = 12 what will be returned when ($a == 12) ? 5 : 1 is executed?
a) 12
b) 1
c) Error
d) 5
View Answer
Answer: d
Explanation: ?: is known as ternary operator. If condition is true then the part just after the ? is executed else the part
after : .
9. What is the value of $a and $b after the function call?
<?php
function doSomething( &$arg ) {
$return = $arg;
$arg += 1;
return $return;
}
$a = 3;
$b = doSomething( $a );
?>
a) a is 3 and b is 4
b) a is 4 and b is 3
c) Both are 3
d) Both are 4
View Answer
Answer: b
Explanation: $a is 4 and $b is 3. The former because $arg is passed by reference, the latter because the return value
of the function is a copy of the initial value of the argument.
10. Who is the father of PHP?
a) Rasmus Lerdorf
b) Willam Makepiece
c) Drek Kolkevi
d) List Barely
View Answer
Answer: a
Explanation: PHP was originally created by Rasmus Lerdorf in 1994
PHP Questions & Answers – Functions
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?


<?php
function calc($price, $tax)
{
$total = $price + $tax;
}
$pricetag = 15;
$taxtag = 3;
calc($pricetag, $taxtag);
?>
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?
<?php
function calc($price, $tax="")
{
$total = $price + ($price * $tax);
echo "$total";
}
calc(42);
?>
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?
<?php
function a()
{
function b()
{
echo 'I am b';
}
echo 'I am a';
}
a();
a();
?>
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.
advertisement

7. 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
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?
<?php
$op2 = "blabla";
function foo($op1)
{
echo $op1;
echo $op2;
}
foo("hello");
?>
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 know 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?
<?php
function foo($msg)
{
echo "$msg";
}
$var1 = "foo";
$var1("will this work");
?>
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.
PHP Questions & Answers – In-Built Functions in PHP
This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “In-Built Functions in PHP”.
1. Which of the following PHP functions accepts any number of parameters?
a) func_get_argv()
b) func_get_args()
c) get_argv()
d) get_argc()
View Answer
Answer: b
Explanation: func_get_args() returns an array of arguments provided. One can use func_get_args() inside the
function to parse any number of passed parameters. Here is an example:

function foo()
{
$args = func_get_args();
foreach ($args as $k => $v)
{
echo "arg".($k+1).": $v\n";
}
}
foo();
/* will print nothing */

foo("Hello");
/* will print Hello */

foo("Hello","World","Bye");
/* will print Hello World Bye */
2. Which one of the following PHP functions can be used to find files?
a) glob()
b) file()
c) fold()
d) get_file()
View Answer
Answer: a
Explanation: The function glob() returns an array of filenames or directories which matches a specified pattern. The
function returns an array of files/directories, or it will return FALSE on failure. Here is an example-

// get all php files AND txt files


$files = glob('*.{php,txt}', GLOB_BRACE);
print_r($files);
/* output looks like:
Array
(
[0] => phptest.php
[1] => pi.php
[2] => post_output.php
.
.
.
)
3. Which of the following PHP functions can be used to get the current memory usage?
a) get_usage()
b) get_peak_usage()
c) memory_get_usage()
d) memory_get_peak_usage()
View Answer
Answer: c
Explanation: memory_get_usage() returns the amount of memory, in bytes, that’s currently being allocated to the
PHP script. We can set the parameter ‘real_usage’ to TRUE to get total memory allocated from system, including
unused pages. If it is not set or FALSE then only the used memory is reported. To get the highest amount of memory
used at any point, we can use the memory_get_peak_usage() function.
4. Which of the following PHP functions can be used for generating unique ids?
a) uniqueid()
b) id()
c) md5()
d) mdid()
View Answer
Answer: a
Explanation: The function uniqueid() is used to generate a unique ID based on the microtime (current time in
microseconds). The ID generated from the function uniqueid() is not optimal, as it is based on the system time. To
generate an ID which is extremely difficult to predict we can use the md5() function.
5. Which one of the following functions can be used to compress a string?
a) zip_compress()
b) zip()
c) compress()
d) gzcompress()
View Answer
Answer: d
Explanation: The function gzcompress() compresses the string using the ZLIB data format. One can achieve upto
50% size reduction using this function. The gzuncompress() function is used to uncompress the string.
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
Answer: d
Explanation: The chr() function returns a character from the specified ASCII value. We can specify ASCII value in
decimal, octal, or hex values. The Octal values are defined as a leading 0, while hex values are defined as a leading
0x. 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
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?
<?php
$str = "Hello World";
echo wordwrap($str,5,"<br>\n");
?>
a) Hello World
b)
Hello
World
c)
Hell
o wo
rld
advertisement

d) World
View Answer
Answer: b
Explanation: The wordwrap() function wraps a string into new lines when it reaches a specific length.
9. What will be the output of the following PHP code?
<?php
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
View Answer
Answer: d
Explanation: The ucwords() function converts the first character of each word in a string to uppercase.
10. What will be the output of the following PHP code?
<?php
echo lcfirst("welcome to India");
?>
a) welcome to India
b) welcome to india
c) Welcome to India
d) Welcome to india
View Answer
Answer: a
Explanation: The lcfirst() function converts the first character of a string to lowercase.
PHP Questions & Answers – Arrays-1
This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Arrays”.
1. PHP’s numerically indexed array begin with position ___________
a) 1
b) 2
c) 0
d) -1
View Answer
Answer: c
Explanation: Like all the other programming languages, the first element of an array always starts with ‘0’.
2. Which of the following are correct ways of creating an array?
i) state[0] = “karnataka”;
ii) $state[] = array(“karnataka”);
iii) $state[0] = “karnataka”;
iv) $state = array(“karnataka”);
a) iii) and iv)
b) ii) and iii)
c) Only i)
d) ii), iii) and iv)
View Answer
Answer: a
Explanation: A variable name should start with $ symbol which is not present in i) and you need not put the square
brackets when you use the array() constructor.
3. What will be the output of the following php code?
<?php
$states = array("Karnataka" => array
("population" => "11,35,000", "capital" => "Bangalore"),
"Tamil Nadu" => array( "population" => "17,90,000",
"capital" => "Chennai") );
echo $states["Karnataka"]["population"];
?>
a) Karnataka 11,35,000
b) 11,35,000
c) population 11,35,000
d) Karnataka population
View Answer
Answer: b
Explanation: In the following PHP code, the variable states are treated as a multidimensional array and accordingly
traverse it to get the value of ‘Karnataka’s population’.
4. Which of the following PHP function will return true if a variable is an array or false if it is not an array?
a) this_array()
b) is_array()
c) do_array()
d) in_array()
View Answer
Answer: b
Explanation: The function is_array() is an inbuilt function in PHP which is used to check whether a variable is an
array or not. Its prototype follows: boolean is_array(mixed variable).
5. Which in-built function will add a value to the end of an array?
a) array_unshift()
b) into_array()
c) inend_array()
d) array_push()
View Answer
Answer: d
Explanation: array_push adds a value to the end of an array, returning the total count of elements in the array after
the new value has been added.
6. What will be the output of the following PHP code?
<?php
$state = array ("Karnataka", "Goa", "Tamil Nadu",
"Andhra Pradesh");
echo (array_search ("Tamil Nadu", $state) );
?>
a) True
b) 1
c) False
d) 2
View Answer
Answer: d
Explanation: The array_search() function searches an array for a specified value, returning its key if located and
FALSE otherwise.
7. What will be the output of the following PHP code?
<?php
$fruits = array ("apple", "orange", "banana");
echo (next($fruits));
echo (next($fruits));
?>
a) orangebanana
b) appleorange
c) orangeorange
d) appleapple
View Answer
Answer: a
Explanation: The next() function returns the value of the next element in the array. In the first ‘next($fruits)’ call, it
will print orange which is next to apple and so on.
advertisement

8. Which of the following function is used to get the value of the previous element in an array?
a) last()
b) before()
c) prev()
d) previous()
View Answer
Answer: c
Explanation: The prev() function returns the previous element in the array.
9. What will be the output of the following PHP code?
<?php
$fruits = array ("apple", "orange", array ("pear", "mango"),
"banana");
echo (count($fruits, 1));
?>
a) 3
b) 4
c) 5
d) 6
View Answer
Answer: d
Explanation: The function count() will return the number of elements in an array. The parameter 1 counts the array
recursively i.e it will count all the elements of multidimensional arrays.
10. Which function returns an array consisting of associative key/value pairs?
a) count()
b) array_count()
c) array_count_values()
d) count_values()
View Answer
Answer: c
Explanation: The function array_count_values() will count all the values of an array. It will return an associative
array, where the keys will be the original array’s values, and the values are the number of occurrences.
PHP Coding Questions and Answers – Arrays – 2
This set of Tricky PHP Questions & Answers focuses on “Arrays – 2”.
1. What will be the output of the following PHP code?
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[2] . ", " . $cars[1] . " and " . $cars[0] . ".";
?>
a) I like Volvo, Toyota and BMW
b) I like Volvo, BMW and Toyota
c) I like BMW, Volvo and Toyota
d) I like Toyota, BMW and Volvo
View Answer
Answer: d
Explanation: The order of elements defined. In the echo statement when we call the elements of array using its
index, it will be printed accordingly. As index ‘0’ indicates ‘Volvo’ , ‘1’ for ‘BMW’ and ‘2’ for Toyota’.
2. What will be the output of the following PHP code?
<?php
$fname = array("Peter", "Ben", "Joe");
$age = array("35", "37", "43");
$c = array_combine($age, $fname);
print_r($c);
?>
a) Array ( Peter Ben Joe )
b) Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
c) Array ( 35 37 43 )
d) Array ( [35] => Peter [37] => Ben [43] => Joe )
View Answer
Answer: d
Explanation: Here “keys” array is $age and “values” array is $fname. The function array_combine() will create an
array by using the elements from one “keys” array and one “values” array. So when variable c is called, it will print
keys and values.
3. What will be the output of the following PHP code?
<?php
$a=array("A","Cat","Dog","A","Dog");
$b=array("A","A","Cat","A","Tiger");
$c=array_combine($a,$b);
print_r(array_count_values($c));
?>
a) Array ( [A] => 5 [Cat] => 2 [Dog] => 2 [Tiger] => 1 )
b) Array ( [A] => 2 [Cat] => 2 [Dog] => 1 [Tiger] => 1 )
c) Array ( [A] => 6 [Cat] => 1 [Dog] => 2 [Tiger] => 1 )
d) Array ( [A] => 2 [Tiger] => 1 )
View Answer
Answer: d
Explanation: The function The array_count_values() counts all the values of an array and the The function
array_combine() will create an array by using the elements from one “keys” array and one “values” array.
4. What will be the output of the following PHP code?
<?php
$a1 = array("a" => "red", "b" => "green", "c" => "blue", "d" => "yellow");
$a2 = array("e" => "red", "f" => "green", "g" => "blue", "h" => "orange");
$a3 = array("i" => "orange");
$a4 = array_merge($a2, $a3);
$result = array_diff($a1, $a4);
print_r($result);
?>
a) Array ( [d] => yellow )
b) Array ( [i] => orange )
c) Array ( [h] => orange )
d) Array ( [d] => yellow [h] => orange )
View Answer
Answer: a
Explanation: The array_diff() function compares the values of two (or more) arrays, and returns the differences. This
function compares the values of two (or more) arrays, and return an array that contains the entries from array1 that
are not present in other arrays (array2, array3, etc).
5. What will be the output of the following PHP code?
<?php
$a1 = array("red", "green");
$a2 = array("blue", "yellow");
$a3 = array_merge($a1, $a2);
$a4 = array("a", "b", "c", "d");
$a = array_combine($a4, $a3);
print_r($a);
?>
a) Array ( [a] => blue [b] => yellow [c] => red [d] => green )
b) Array ( [0] => blue [1] => yellow [2] => red [3] => green )
c) Array ( [0] => red [1] => green [2] => blue [3] => yellow )
d) Array ( [a] => red [b] => green [c] => blue [d] => yellow )
View Answer
Answer: d
Explanation: The function array_merge() merges one or more arrays into one array. If in the function array_merge(),
two or more array elements have the same key, the last one overrides the others. The function array_combine() will
create an array by using the elements from one “keys” array and one “values” array. The program is the basic
combined application of array_combine() and array_merge().
6. What will be the output of the following PHP code?
<?php
$a = array("a" => "india", "b" => "brazil", "c" => "china");
echo array_shift($a);
echo "<br>";
array_pop($a);
print_r($a);
?>
a)
india
Array ( [b] => Brazil )
advertisement

b)
india
Array ( [a] => brazil )
c)
china
Array ( [a] => india )
d)
china
Array ( [a] => brazil )
View Answer
Answer: a
Explanation: The function array_shift() removes the first element from an array, and it returns the value of the
removed element and the function array_pop() deletes the last element of an array. So “a” => “India”, “c” =>
“China” will be deleted and “b” => “Brazil” will be printed.

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


<?php
$a1 = array_fill(1, 4, "hello");
$b1 = array_fill(5, 1, "php");
$a2 = array_merge($a1, $a2);
print_r($a2);
echo "<br>";
print_r($b1);
?>
a)
Array ( [1] => hello [4] => hello [5] => php )
Array ( [5] => php )
b)
Array ( [1] => hello [2] => hello [3] => hello [4] => hello )
Array ( [5] => php )
c)
Array ( [1] => hello [2] => hello [3] => hello [4] => hello [5] => php )
Array ( [5] => php )
d)
Array ( [1] => hello [2] => hello [3] => hello [4] => hello )
Array ( [1] => php )
View Answer
Answer: c
Explanation: Usage of array_fill() and array_merge() functions.

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


<?php
$names = array("Sam", "Bob", "Jack");
echo $names[0] . "is the brother of " . $names[1] . " and " . $names[1] . ".";
?>
a) Sam is the brother of Bob and Jack
b) Samis the brother of Bob and Bob
c) Sam is the brother of Jack and Bob
d) Error
View Answer
Answer: b
Explanation: Simple definition of array and using it in a string. We have used $names[1] twice and hence Bob
appears twice.
9. What will be the output of the following PHP code?
<?php
$names = array("Sam", "Bob", "Jack");
echo $names[0]."is the brother of ".$names[1]." and ".$names[1].".".$brother;
?>
a) Sam is the brother of Bob and Bob) $brother
b) Sam is the brother of Bob and Bob)
c) $brother
d) Error
View Answer
Answer: d
Explanation: $brother undeclared.
10. What will be the output of the following PHP code?
<?php
$place = array("NYC", "LA", "Paris");
array_pop($place);
$place1 = array("Paris");
$place = array_merge($place, $place1);
print_r($place);
?>
a) Array ( [0] => LA [1] => Paris [2] => Paris )
b) Array ( [0] => NYC [1] => LA [2] => Paris)
c) Array ( [0] => NYC [1] => LA [2] => Paris [3] => Paris )
d) Array ( [0] => LA [1] => Paris )
View Answer
Answer: b
Explanation: array_merge() and array_pop() yields that result.
PHP Coding Questions and Answers – Arrays – 3
This set of Tough PHP Questions & Answers focuses on “Arrays – 3”.
1. What will be the output of the following PHP code ?
<?php
$age = array("Harry" => "21", "Ron" => "23","Malfoy" => "21");
array_pop($age);
print_r(array_change_key_case($age, CASE_UPPER));
?>
a) Array ( [Harry] => 21 [Ron] => 23 [Malfoy] => 21 )
b) Array ( [HARRY] => 21 [RON] => 23 [MALFOY] => 21 )
c) Array ( [HARRY] => 21 [RON] => 23 )
d) Array ( [Harry] => 21 [Ron] => 23 )
View Answer
Answer: c
Explanation: The function array_pop() will delete the last element of an array. So Malfoy => 21 will be deleted and
the function array_change_key_case() will change all keys in an array to lowercase or uppercase.
2. What will be the output of the following PHP code ?
<?php
$a1 = array("a" => "red", "b" => "green", "c" => "blue", "d" => "yellow");
$result = array_flip($a1);
print_r($result);
?>
a) Array ( [red] => red [green] => green [blue] => blue [yellow] => yellow )
b) Array ( [a] => a [b] => b [c] => c [d] => d )
c) Array ( [red] => a [green] => b [blue] => c [yellow] => d )
d) Array ( [a] => red [b] => green [c] => blue [d] => yellow )
View Answer
Answer: c
Explanation: The function array_flip() flips/exchanges all keys with their associated values in an array. So, in the
above program “a” will be flipped with “red”, “b” will be flipped with “green” and so on.
3. What will be the output of the following PHP code ?
<?php
$a1 = array("a" => "red", "b" => "green", "c" => "blue", "d" => "yellow");
$a2 = array("e" => "red","f" => "green", "g" => "blue");
$result = array_intersect($a1, $a2);
print_r($result);
?>
a) Array ( [a] => red [b] => green [c] => blue )
b) Array ( [a] => red [b] => green [c] => blue [d] => yellow )
c) Array ( [e] => red [f] => green [g] => blue )
d) Array ( [a] => red [b] => green [c] => blue [d] => yellow [e] => red [f] => green [g] => blue )
View Answer
Answer: a
Explanation: The function array_intersect() compares the values of two (or more) arrays, and returns the matches.
So, in the above program values of a1 and a2 will be compared and the values present in both the arrays will be the
returned.
4. What will be the output of the following PHP code ?
<?php
$a = array(12, 5, 2);
echo(array_product($a));
?>
a) 024
b) 120
c) 010
d) 060
View Answer
Answer: b
Explanation: The array_product() function calculates and returns the product of an array.
5. What will be the output of the following PHP code ?
<?php
$a = array("a" => "Jaguar", "b" => "Land Rover",
"c" => "Audi", "d" => "Maseratti");
echo array_search("Audi", $a);
?>
a) a
b) b
c) c
d) d
View Answer
Answer: c
Explanation: The array_search() function searches for the element and returns the key of that element.
6. What will be the output of the following PHP code ?
<?php
$city_west = array("NYC", "London");
$city_east = array("Mumbai", "Beijing");
print_r(array_replace($city_west, $city_east));
?>
a) Array ( [1] => Mumbai [0] => Beijing )
b) Array ( [0] => NYC [1] => London )
c) Array ( [1] => NYC [0] => London )
d) Array ( [0] => Mumbai [1] => Beijing )
View Answer
Answer: d
Explanation: The function array_replace() replaces the values of the first array with the values from following
arrays. So, in the above program the values of city_west will be replaced with city_east.
advertisement

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


<?php
$people = array("Peter", "Susan", "Edmund", "Lucy");
echo pos($people);
?>
a) Lucy
b) Peter
c) Susan
d) Edmund
View Answer
Answer: b
Explanation: The pos() function returns the value of the current element in an array, and since no operation has been
done, the current element is the first element.
8. What will be the output of the following PHP code ?
<?php
$number = range(0, 5);
print_r ($number);
?>
a) Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 )
b) Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 )
c) Array ( [0] => 5 [1] => 5 [2] => 5 [3] => 5 [4] => 5 [5] => 5 )
d) Array ( [0] => 0 [5] => 5 )
View Answer
Answer: a
Explanation: The range() function creates an array containing a range of elements.
9. What will be the output of the following PHP code ?
<?php
$array = array("red", "green");
array_push($array, "blue", "yellow");
print_r($array);
?>
a) Array ( [0] => red [1] => green [2] => blue [3] => yellow )
b) Array ( [0] => blue [1] => yellow [2] => red [3] => green )
c) Array ( [0] => red [1] => green )
d) Array ( [0] => blue [1] => yellow )
View Answer
Answer: a
Explanation: The function array_push() inserts one or more elements to the end of an array. So, in the above
program blue and yellow will be inserted after previous values.
10. What will be the output of the following PHP code ?
<?php
$age = array("Harry" => "21", "Ron" => "19", "Malfoy" => "23");
ksort($age);
foreach($age as $x => $x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
a)
Key = Harry, Value = 21
Key = Ron, Value = 21
Key = Malfoy, Value = 23
b)
Key = Harry, Value = 21
Key = Ron, Value = 19
Key = Malfoy, Value = 23
c)
Key = Harry, Value = 21
Key = Malfoy, Value = 23
Key = Ron, Value = 19
d)
Key = Ron, Value = 19
Key = Harry, Value = 21
Key = Malfoy, Value = 23
View Answer
Answer: c
Explanation: The ksort() function sorts an associative array in ascending order, according to the key.
PHP Questions & Answers – Basics of Object-Oriented PHP- 1
This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Basics of Object-Oriented PHP”.
1. The practice of separating the user from the true inner workings of an application through well-known interfaces
is known as _________
a) Polymorphism
b) Inheritance
c) Encapsulation
d) Abstraction
View Answer
Answer: c
Explanation: In object-oriented PHP encapsulation is a concept of wrapping up or binding up the data members and
methods in a single module.
2. Which of the following term originates from the Greek language that means “having multiple forms,” defines
OOP’s ability to redefine, a class’s characteristics?
a) Abstraction
b) Polymorphism
c) Inheritance
d) Differential
View Answer
Answer: b
Explanation: The word polymorphism is derived from Greek word poly which means “many” and morphism which
means the property which helps us to assign more than one property.

3. The practice of creating objects based on predefined classes is often referred to as ______________
a) class creation
b) object creation
c) object instantiation
d) class instantiation
View Answer
Answer: d
Explanation: In object-oriented programming, classes are the blueprints of php objects. Classes do not actually
become objects until instantiation is done. When someone instantiates a class, it creates an instance of it, thus
creating the object. In other words, instantiation is the process of creating an instance of an object in memory.
4. Which one of the following property scopes is not supported by PHP?
a) friendly
b) final
c) public
d) static
View Answer
Answer: a
Explanation: PHP supports five class property scopes: public, private, protected, final and static.
5. Which one of the following can be used to instantiate an object in PHP assuming class name to be Foo?
a) $obj = new $foo;
b) $obj = new foo;
c) $obj = new foo ();
d) obj = new foo ();
View Answer
Answer: c
Explanation: To create a new object in PHP we can use the new statement to instantiate a class.
6. Which one of the following is the right way to define a constant?
a) constant PI = “3.1415”;
b) const $PI = “3.1415”;
c) constant PI = ‘3.1415’;
d) const PI = ‘3.1415’;
View Answer
Answer: d
Explanation: Class constants are created like: const NAME = ‘VALUE’;
7. Which one of the following is the right way to call a class constant, given that the class is mathFunction?
a) echo PI;
b) echo mathFunction->PI;
c) echo mathFunction::PI;
d) echo mathFunction=PI;
View Answer
Answer: c
Explanation: The Scope Resolution Operator “::” is a token that allows access to static, constant, and overridden
properties or methods of a class.
8. Which one of the following is the right way to invoke a method?
a) $object->methodName();
b) object->methodName();
c) object::methodName();
d) $object::methodName();
View Answer
Answer: a
Explanation: “->” is a dynamic class method invocation in PHP.
9. Which of the following is/are the right way to declare a method?
i) function functionName() { function body }
ii) scope function functionName() { function body }
iii) method methodName() { method body }
iv) scope method methodName() { method body }
a) Only ii)
b) Only iv)
c) i) and ii)
d) iii) and iv)
View Answer
Answer: c
Explanation: In case of public methods, you can forgo explicitly declaring the scope and just declare the method like
you would a function.
10. Which of the following method scopes is/are not supported by PHP?
i) private
ii) friendly
iii) static
iv) abstract
a) Only ii)
b) Only iv)
c) ii) and iv)
d) Only i)
View Answer
Answer: a
Explanation: PHP supports six method scopes: public, private, final, static, protected and abstract. But it does not
support friendly
PHP Questions & Answers – Basics of Object-Oriented PHP- 2
This set of PHP Questions and Answers for Experienced people focuses on “Basics of Object-Oriented PHP- 2”.
1. Which method scope prevents a method from being overridden by a subclass?
a) Abstract
b) Protected
c) Final
d) Static
View Answer
Answer: c
Explanation: When we declare a method is as final then it is not possible to override that method. Methods should
not be overridden due to some security or any other reasons.
2. Which of the following statements is/are true about Constructors in PHP?
i) PHP 4 introduced class constructors.
ii) Constructors can accept parameters.
iii) Constructors can call class methods or other functions.
iv) Class constructors can call on other constructors.
a) ii)
b) ii) and iii)
c) i), ii), iii) and iv)
d) ii), iii) and iv)
View Answer
Answer: c
Explanation: If a class name and the function name is similar then the function is known as constructor. Constructor
is automatically called when an object will be initialized. Constructors can accept parameters. Constructors can call
class methods or other functions. Class constructors can call on other constructors.
3. PHP recognizes constructors by the name _________
a) classname()
b) _construct()
c) function _construct()
d) function __construct()
View Answer
Answer: d
Explanation: A double underscore followed by the construct keyword. Its syntax is function __construct
([ argument1, argument2,…..]) { Class Initialization code }.
4. Which version of PHP introduced the instanceof keyword?
a) PHP 4
b) PHP 5
c) PHP 5.3
d) PHP 6
View answer
Answer: b
Explanation: Using instanceof keyword we can determine whether an object is an instance of a class. $manager =
new Employee() … if ($manager instanceof Employee) echo “True”;
5. Which one of the following functions is used to determine whether a class exists?
a) exist()
b) exist_class()
c) class_exist()
d) __exist()
View Answer
Answer: c
Explanation: The class_exist() function returns true or false according to whether the class exists within the
currently executing script content.
6. Which one of the following functions is used to determine object type?
a) obj_type()
b) type()
c) is_a()
d) is_obj()
View Answer
Answer: c
Explanation: The is_a() function returns true if object belongs to a class type or if it belongs to a class that is a child
of that class. Or else false is returned.
7. Which one of the following keyword is used to inherit our subclass into a superclass?
a) extends
b) implements
c) inherit
d) include
View Answer
Answer: a
Explanation: When we extend a class then the subclass will inherit all the public and protected methods from the
parent class.
The keyword implements are used with interfaces. With inheritance, we use the keyword extends.
8. In the PHP code given below, what is/are the properties?
<?php
class Example
{
public $name;
function Sample()
{
echo "This is an example";
}
}
?>
a) echo “This is an example”;
b) public $name;
c) class Example
d) function sample()
View Answer
Answer: b
Explanation: Above code is an example of ‘classes’ in PHP. Classes are the blueprints of objects. Classes are the
programmer-defined data type, which includes the local methods and the local variables. Class is a collection of
objects which has properties and behaviour.
advertisement

9. Which keyword is used to refer to properties or methods within the class itself?
a) private
b) public
c) protected
d) $this
View Answer
Answer: d
Explanation: In PHP, the self and ‘this’ keyword are used to refer the class members within the scope of a class
itself. The class members can be either variables or functions.
10. Which keyword allows class members (methods and properties) to be used without needing to instantiate a new
instance of the class?
a) protected
b) final
c) static
d) private
View Answer
Answer: c
Explanation: Sometimes it is very handy to access the methods and properties in terms of a class rather than an
object. But this can be done through static keyword. Any method declared as ‘static’ can be accessed without the
creation of an object
PHP Questions & Answers – Strings and Regular Expressions – 1
This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Strings and Regular Expressions – 1”.
1. PHP has long supported two regular expression implementations known as _______ and _______
i) Perl
ii) PEAR
iii) Pearl
iv) POSIX
a) i) and ii)
b) ii) and iv)
c) i) and iv)
d) ii) and iii)
View Answer
Answer: c
Explanation: None.
2. Which one of the following regular expression matches any string containing zero or one p?
a) p+
b) p*
c) P?
d) p#
View Answer
Answer: c
Explanation: None.

3. [:alpha:] can also be specified as..


a) [A-Za-z0-9].
b) [A-za-z].
c) [A-z].
d) [a-z].
View Answer
Answer: b
Explanation:[:alpha:] is nothing but Lowercase and uppercase alphabetical characters.
4. How many functions does PHP offer for searching strings using POSIX style regular expression?
a) 7
b) 8
c) 9
d) 10
View Answer
Answer: a
Explanation: ereg(), ereg_replace(), eregi(), eregi_replace(), split(), spliti(), and sql_regcase() are the functions
offered.
5. What will be the output of the following PHP code?
<?php
$username = "jasoN";
if (ereg("([^a-z])",$username))
echo "Username must be all lowercase!";
else
echo "Username is all lowercase!";
?>
a) Error
b) Username must be all lowercase!
c) Username is all lowercase!
d) No Output is returned
View Answer
Answer: b
Explanation: Because the provided username is not all lowercase, ereg() will not return FALSE (instead returning
the length of the matched string, which PHP will treat as TRUE), causing the message to output.
6. POSIX implementation was deprecated in which version of PHP?
a) PHP 4
b) PHP 5
c) PHP 5.2
d) PHP 5.3
View Answer
Answer: d
Explanation: None.
7. POSIX stands for
a) Portable Operating System Interface for Unix
b) Portable Operating System Interface for Linux
c) Portative Operating System Interface for Unix
d) Portative Operating System Interface for Linux
View Answer
Answer: a
Explanation: None.
8. What will be the output of the following PHP code?
<?php
$text = "this is\tsome text that\nwe might like to parse.";
print_r(split("[\n\t]",$text));
?>
a) this is some text that we might like to parse.
b) Array ( [0] => some text that [1] => we might like to parse. )
c) Array ( [0] => this is [1] => some text that [2] => we might like to parse. )
d) [0] => this is [1] => some text that [2] => we might like to parse.
View Answer
Answer: d
Explanation: The split() function divides a string into various elements, with the boundaries of each element based
on the occurrence of a defined pattern within the string.
advertisement

9. Which of the following would be a potential match for the Perl-based regular expression /fo{2,4}/ ?
i) fol
ii) fool
iii) fooool
iv) fooooool
a) Only i)
b) ii) and iii)
c) i), iii) and iv)
d) i) and iv)
View Answer
Answer: b
Explanation: This matches f followed by two to four occurrences of o.
10. Which among the following is/are not a metacharacter?
i) \a
ii) \A
iii) \b
iv) \B
a) Only i)
b) i) and iii)
c) ii), iii) and iv)
d) ii) and iv)
View Answer
Answer: a
Explanation: /A, /b and /B are metacharacters. \A: Matches only at the beginning of the string. \b: Matches a word
boundary. \B: Matches anything but a word boundary.
PHP Questions & Answers – Strings and Regular Expressions – 2
This set of PHP Interview Questions and Answers for Experienced people focuses on “Strings and Regular
Expressions – 2”.
1. How many functions does PHP offer for searching and modifying strings using Perl-compatible regular
expressions.
a) 7
b) 8
c) 9
d) 10
View Answer
Answer: b
Explanation: The functions are preg_filter(), preg_grep(), preg_match(), preg_match_all(), preg_quote(),
preg_replace(), preg_replace_callback(), and preg_split().
2. What will be the output of the following PHP code?
<?php
$foods = array("pasta", "steak", "fish", "potatoes");
$food = preg_grep("/^s/", $foods);
print_r($food);
?>
a) Array ( [0] => pasta [1] => steak [2] => fish [3] => potatoes )
b) Array ( [3] => potatoes )
c) Array ( [1] => steak )
d) Array ( [0] => potatoes )
View Answer
Answer: c
Explanation: This function is used to search an array for foods beginning with s.
3. Say we have two compare two strings which of the following function/functions can you use?
i) strcmp()
ii) strcasecmp()
iii) strspn()
iv) strcspn()
a) i) and ii)
b) iii) and iv)
c) None of the mentioned
d) All of the mentioned
View Answer
Answer: d
Explanation: All of the functions mentioned above can be used to compare strings in some or the other way.
4. Which one of the following functions will convert a string to all uppercase?
a) strtoupper()
b) uppercase()
c) str_uppercase()
d) struppercase()
View Answer
Answer: a
Explanation: Its prototype follows string strtoupper(string str).
5. What will be the output of the following PHP code?
<?php
$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!
View Answer
Answer: d
Explanation: The ucwords() function capitalizes the first letter of each word in a string. Its prototype follows: string
ucwords(string str).
6. What will be the output of the following PHP code?
<?php
echo str_pad("Salad", 5)." is good.";
?>
a) SaladSaladSaladSaladSalad is good
b) is good SaladSaladSaladSaladSalad
c) is good Salad
d) Salad is good
View Answer
Answer: d
Explanation: The str_pad() function pads a string with a specified number of characters.
advertisement

7. Which one of the following functions can be used to concatenate array elements to form a single delimited string?
a) explode()
b) implode()
c) concat()
d) concatenate()
View Answer
Answer: b
Explanation: None.
8. Which one of the following functions finds the last occurrence of a string, returning its numerical position?
a) strlastpos()
b) strpos()
c) strlast()
d) strrpos()
View Answer
Answer: d
Explanation: None.
9. What will be the output of the following PHP code?
<?php
$author = "[email protected]";
$author = str_replace("a","@",$author);
echo "Contact the author of this article at $author.";
?>
a) Contact the author of this article at nachiketh@[email protected]
b) Cont@ct the @uthor of this @rticle @t n@chiketh@[email protected]
c) Contact the author of this article at n@chiketh@[email protected]
d) Error
View Answer
Answer: c
Explanation: The str_replace() function case sensitively replaces all instances of a string with another.
10. What will be the output of the following PHP code?
<?php
$url = "[email protected]";
echo ltrim(strstr($url, "@"),"@");
?>
a) [email protected]
b) nachiketh
c) nachiketh@
d) example.com
View Answer
Answer: d
Explanation: The strstr() function returns the remainder of a string beginning with the first occurrence of a
predefined string.
PHP Questions & Answers – HTML Forms
This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “HTML Forms”.
1. Which two predefined variables are used to retrieve information from forms?
a) $GET & $SET
b) $_GET & $_SET
c) $__GET & $__SET
d) GET & SET
View Answer
Answer: b
Explanation: The global variables $_GET is used to collect form data after submitting an HTML form with the
method=”get”. The variable $_SET is also used to retrieve information from forms.
2. The attack which involves the insertion of malicious code into a page frequented by other users is known as
_______________
a) basic sql injection
b) advanced sql injection
c) cross-site scripting
d) scripting
View Answer
Answer: c
Explanation: The cross-site scripting attack is among one of the top five security attacks carried out across the
Internet. It is also known as XSS, this attack is a type of code injection attack which is made possible by incorrectly
validating user data, which usually gets inserted into the page through a web form or using an altered hyperlink.
3. When you use the $_GET variable to collect data, the data is visible to ___________
a) none
b) only you
c) everyone
d) selected few
View Answer
Answer: c
Explanation: The information sent from a form with the method GET is visible to everyone i.e. all variable names
and values are displayed in the URL.
4. When you use the $_POST variable to collect data, the data is visible to ___________
a) none
b) only you
c) everyone
d) selected few
View Answer
Answer: b
Explanation: The information sent from a form with the method POST is invisible to others i.e. all names/values are
embedded within the body of the HTTP request.
5. Which variable is used to collect form data sent with both the GET and POST methods?
a) $BOTH
b) $_BOTH
c) $REQUEST
d) $_REQUEST
View Answer
Answer: d
Explanation: In PHP the global variable $_REQUEST is used to collect data after submitting an HTML form.
6. Which one of the following should not be used while sending passwords or other sensitive information?
a) GET
b) POST
c) REQUEST
d) NEXT
View Answer
Answer: a
Explanation: The information sent from a form with the method GET is visible to everyone i.e. all variable names
and values are displayed in the URL. So, it should not be used while sending passwords or other sensitive
information.
7. Which function is used to remove all HTML tags from a string passed to a form?
a) remove_tags()
b) strip_tags()
c) tags_strip()
d) tags_remove()
View Answer
Answer: b
Explanation: The function strip_tags() is used to strip a string from HTML, XML, and PHP tags.
8. What will be the value of the variable $input in the following PHP code?
<?php
$input = "Swapna<td>Lawrence</td>you are really<i>pretty</i>!";
$input = strip_tags($input,"<i></i>");
echo $input;
?>
a) Swapna Lawrence you are really pretty!
b) Swapna <td>Lawrence</td> you are really<i>pretty</i>!
c) Swapna <td>Lawrence</td> you are really pretty!
d) Swapna Lawrence you are really<i>pretty</i>!
View Answer
Answer: d
Explanation: Italic tags <i></i> might be allowable, but table tags <td></td> could potentially wreak havoc on a
page.
advertisement

9. To validate an email address, which flag is to be passed to the function filter_var()?


a) FILTER_VALIDATE_EMAIL
b) FILTER_VALIDATE_MAIL
c) VALIDATE_EMAIL
d) VALIDATE_MAIL
View Answer
Answer: a
Explanation: The FILTER_VALIDATE_EMAIL is used to validates an e-mail address.
10. How many validation filters like FILTER_VALIDATE_EMAIL are currently available?
a) 5
b) 6
c) 7
d) 8
View Answer
Answer: c
Explanation: There are seven validation filters. They are FILTER_VALIDATE_EMAIL,
FILTER_VALIDATE_BOOLEAN, FILTER_VALIDATE_FLOAT, FILTER_VALIDATE_INT,
FILTER_VALIDATE_IP, FILTER_VALIDATE_REGEXP, FILTER_VALIDATE_URL.
PHP Coding Questions and Answers – Syntax – 1
This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Syntax – 1”.
1. What will be the output of the following PHP code ?
<?php
"Hello World"
?>
a) Error
b) Hello World
c) Nothing
d) Missing semicolon error
View Answer
Answer: c
Explanation: If you need to output something onto the screen you’ll need to use echo or print_r.

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


<?php
print_r "Hello world"
?>
a) Error
b) Hello World
c) Nothing
d) Missing semicolon error
View Answer
Answer: a
Explanation: The statement should be print_r(‘Hello World’) to print Hello world. Also if there is only one line then
there is no requirement of a semicolon, but it is better to use it.
3. What will be the output of the following PHP code ?
<?php
echo 'Hello World';
<html>
Hello world
</html>
?>
a) Hello world
b) Hello World Hello World
c) Hello world
Hello World
d) Syntax Error
View Answer
Answer: d
Explanation: Parse error: syntax error, unexpected ‘<‘ on line 2. You can not use the html tag inside php tags.
4. What will be the output of the following PHP code ?
<?php
Echo "Hello World1";
echo " Hello world2";
ECHO " Hello world3";
?>
a) Hello world1 Hello world2 Hello World3
b) Hello world1
Hello world2
Hello World3
c) Error
d) Hello world1 Hello world3
View Answer
Answer: a
Explanation: In PHP, all user-defined functions, classes, and keywords (e.g. if, else, while, echo, etc.) are case-
insensitive.
5. What will be the output of the following PHP code ?
<?php
$color = "red";
echo "$color";
echo "$COLOR";
echo "$Color";
?>
a) redredred
b) redred
c) red
d) Error
View Answer
Answer: c
Explanation: In PHP, all variables are case-sensitive.
6. What will be the output of the following PHP code ?
<?php
# echo "Hello world";
echo "# Hello world";
?>
a) # Hello world
b) Hello world# Hello world
c) Hello world
d) Error
View Answer
Answer: a
Explanation: # is a single line comment.
advertisement

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


<?php
echo "<i>Hello World</i>"
?>
a) Hello world
b) Hello world in italics
c) Nothing
d) Error
View Answer
Answer: b
Explanation: You can use tags like italics, bold etc. inside php script.
8. What will be the output of the following PHP code ?
<?php
echo "echo "Hello World"";
?>
a) Hello world
b) echo “Hello world”
c) echo Hello world
d) Error
View Answer
Answer: d
Explanation: It would have printed echo “Hello world” if the statement was echo “echo \”Hello World\””;.
9. What will be the output of the following PHP code ?
<?php
<?php
echo "Hello world";
?>
?>
a) HELLO WORLD
b) Hello world
c) Nothing
d) Error
View Answer
Answer: d
Explanation: You can not have php tags inside a php tag.
10. What will be the output of the following PHP code ?
<?php
$color = red;
echo "\$color";
?>
a) red
b) $color
c) \red
d) Error
View Answer
Answer:b
Explanation: To print red remove the \.
PHP Coding Questions and Answers – Syntax – 2
This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Syntax – 2”.
1. What will be the output of the following PHP code ?
<?php
/*
echo "Hello world";
*/
?>
a) Hello world
b) Nothing
c) Error
d) /*
Hello world
*/
View Answer
Answer: b
Explanation: /* */ is used for commenting multiple lines.
2. What will be the output of the following PHP code ?
<?php
$color = red;
echo "$color" . red ;
?>
a) red red
b) red
c) error
d) nothing
View Answer
Answer: c
Explanation: Use of undefined constant red.
3. What will be the output of the following PHP code ?
<?php
$color1 = red;
$color2 = green;
echo "$color1"."$color2";
?>
a) red green
b) red
c) green
d) error
View Answer
Answer: d
Explanation: It has to be $color1 = “red”; and $color2 = “green”; therefore the error.
4. What will be the output of the following PHP code ?
<?php
$color = "red";
$color = "green";
echo "$color";
?>
a) red
b) green
c) red green
d) error
View Answer
Answer: b
Explanation: The variable contains the last value which has been assigned.
5. What will be the output of the following PHP code ?
<?php
$color1 = "red";
$color2 = "green";
echo "$color1" . "$color2";
?>
a) red
b) green
c) red green
d) redgreen
View Answer
Answer: d
Explanation: The . operator is used to join to strings.
6. What will be the output of the following PHP code ?
<?php
$color1 = "red";
$color2 = "green";
echo "$color1" + "$color2";
?>
a) redgreen
b) red green
c) 0
d) error
View Answer
Answer: c
Explanation: + operator does not join both the strings.
advertisement

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


<?php
$color1 = "red";
$color2 = "red";
echo "$color1" + "$color2";
?>
a) redgreen
b) red green
c) 0
d) 1
View Answer
Answer: c
Explanation: + does not return 1 if the variables are equal.
8. What will be the output of the following PHP code ?
<?php
$color1 = "red";
$color2 = "1";
echo "$color1" + "$color2";
?>
a) red1
b) red 1
c) 0
d) 1
View Answer
Answer: d
Explanation: + just returns the numeric value even though it is inside double quotes.

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


<?php
$color1 = "1";
$color2 = "1";
echo "$color1" + "$color2";
?>
a) 11
b) 2
c) 0
d) 1
View Answer
Answer: b
Explanation: + can be used to add to integer values which are enclosed by double-quotes.

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


<?php
$color1 = "red";
$color2 = "1";
$color3 = "grey"
echo "$color1" + "$color2" . "$color3";
?>
a) 1grey
b) grey
c) 0
d) red1grey
View Answer
Answer: a
Explanation: + gives the value 1 and . is used to give join 1 and grey.
PHP Coding Questions and Answers – Variables – 1
This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Variables – 1”.
1. What will be the output of the following PHP code ?
<?php
$x = 5;
$y = 10;
$z = "$x + $y";
echo "$z";
?>
a) 15
b) 10 + 5
c) $z
d) $x + $y
View Answer
Answer: b
Explanation: Variable z will store 10 + 5 because 10 + 5 is given in double-quotes.
2. What will be the output of the following PHP code ?
<?php
$x = 4;
$y = 3;
$z = 1;
echo "$x = $x + $y + $z";
?>
a) 4 = 4 + 3 + 1
b) 8
c) 8 = 4 + 3 +1
d) Error
View Answer
Answer: a
Explanation: Again since the variables are inside double quotes we get this result.
3. What will be the output of the following PHP code ?
<?php
$x = 4;
$y = 3
$z = 1;
$z = $z + $x + $y;
echo "$z";
?>
a) $z
b) 15
c) 8
d) 1
View Answer
Answer: c
Explanation: Normal addition of variables x, y and z occurs and result of 8 will be displayed.
4. What will be the output of the following PHP code ?
<?php
$x = 3.3;
$y = 2;
echo $x % $y;
?>
a) 0
b) 1
c) 2
d) Error
View Answer
Answer: b
Explanation: % is the modulo operator. Unlike in C we can use it get reminder or floating point numbers in PHP.
5. What will be the output of the following PHP code ?
<?php
$x = 10;
$y = 4;
$z = 3;
echo $x % $y % $z;
?>
a) 0
b) 1
c) 2
d) Error
View Answer
Answer: c
Explanation: The expression is considered as ($x%$y)%z in this case (10%4)%3 which is 2.
6. What will be the output of the following PHP code ?
<?php
$x = 10;
$y = 4;
$z = 3;
echo ($x % ($y) + $z);
?>
a) 5
b) 3
c) 0
d) 1
View Answer
Answer: a
Explanation: The innermost bracket is evaluated first, since it covers only variable y it is as good as not using
brackets.
advertisement

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


<?php
$x = 30;
$y = 20;
$z = 10;
echo $x + $y - $z / ($z - $y);
?>
a) 41
b) -4
c) -5
d) 51
View Answer
Answer: d
Explanation: First ($z – $y) is evaluated then -$z/($z – $y) is evaluated this results in 1 which is added to $x + $y
therefore we get 51.
8. What will be the output of the following PHP code ?
<?php
$x = -1;
$y = 1;
$z = $x * $y + $z;
echo $z;
?>
a) Undefined variable z
b) -1
c) Undefined variable z
-1
d) None of the mentioned
View Answer
Answer: c
Explanation: Since the variable z is not defined it returns the error also it takes z as 0 and returns the value -1.
9. What will be the output of the following PHP code ?
<?php
$x = 4;
$y = -3;
$z = 11;
echo 4 + $y * $z / $x;
?>
a) 4.25
b) 3.25
c) -3.25
d) -4.25
View Answer
Answer: d
Explanation: First the * is evaluated then / followed by + therefore we can rewrite this expression as 4 +((- 3 * 11) /
4) which results in -4.25.
10. What will be the output of the following PHP code ?
<?php
$x = 3.5;
$y = 2;
$z = 2;
echo $x / $y / $z;
?>
a) 1.75
b) 0.875
c) 3.5
d) Error
View Answer
Answer: b
Explanation: First $x / $y is evaluated then this is divided by $z therefore we get 0.875.
PHP Coding Questions and Answers – Variables – 2
This set of PHP Technical Interview Questions & Answers focuses on “Variables – 2”.
1. What will be the output of the following PHP code ?
<?php
one = 1;
two = 2;
three = 3;
four = 4;
echo "one / two + three / four";
?>
a) 0.75
b) 0.05
c) 1.25
d) Error
View Answer
Answer: d
Explanation: Variables should start with a $ symbol, since one, two, three, four don’t begin with $ symbol we’ll get
an error.
2. What will be the output of the following PHP code ?
<?php
$on$e = 1;
$tw$o = 2;
$thre$e = 3;
$fou$r = 4;
echo "$on$e / $tw$o + $thre$e / $fou$r";
?>
a) 0.75
b) 0.05
c) 1.25
d) Error
View Answer
Answer: d
Explanation: You can not use the $ in between the variable name.
3. What will be the output of the following PHP code ?
<?php
$on_e = 1;
$tw_o = 2;
$thre_e = 3;
$fou_r = 4;
echo $on_e / $tw_o + $thre_e / $fou_r;
?>
a) 0.75
b) 0.05
c) 1.25
d) Error
View Answer
Answer: c
Explanation: You can use _ in a variable name.
4. What will be the output of the following PHP code ?
<?php
$On_e = 1;
$tw_o = 2;
$thre_e = 3;
$fou_r = 4;
echo $on_e / $tw_o + $thre_e / $fou_r;
?>
a) 0.75
b) 0.05
c) 1.25
d) Error
View Answer
Answer: a
Explanation: Since the variable initialised is $On_e and the variable in the echo statement is $on_e the echo
statement treats $on_e as 0;
5. What will be the output of the following PHP code ?
<?php
echo $red;
?>
a) 0
b) Nothing
c) True
d) Error
View Answer
Answer: b
Explanation: There will no output returned as the variable $red does not hold any value.

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


<?php
$four4 = 4;
$three3 = 3;
$two2 = 2;
echo $four4 + $three3 / $two2 - 1;
?>
a) 4.5
b) 7
c) 3.5
d) Error
View Answer
Answer: a
Explanation: You can use numbers in a variable name.
advertisement

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


<?php
$4four = 4;
$3three = 3;
$2two = 2;
echo $4four + $3three / $2two - 1;
?>
a) 4.5
b) 7
c) 3.5
d) Error
View Answer
Answer: d
Explanation: A variable name can not start with a numeric value.
8. What will be the output of the following PHP code ?
<?php
int $one = 1;
echo "$one";
?>
a) 0
b) 1
c) $one
d) Error
View Answer
Answer: d
Explanation: Unlike other programming languages there are no data types in PHP.
9. What will be the output of the following PHP code ?
<?php
var $one = 1;
var $two = 2;
echo $one / $two * $one / $two * $two;
?>
a) 1
b) 0
c) 0.5
d) Error
View Answer
Answer: d
Explanation: You can not use var before a variable name.
10. What will be the output of the following PHP code ?
<?php
$hello = "Hello World";
$bye = "Bye";
echo $hello;"$bye";
?>
a) Hello World
b) Bye
c) Hello worldBye
d) Error
View Answer
Answer: a
Explanation: Since there is a semi-colon in between $hello and $bye, the line ends at $hello. However $bye would
have printed if a echo was present before “$bye”.
PHP Coding Questions and Answers – Variables – 3
This set of PHP Problems focuses on “Variables – 3”.
1. What will be the output of the following PHP code ?
<?php
$x;
echo "$x";
?>
a) 0
b) 1
c) Nothing
d) Error
View Answer
Answer: c
Explanation: Since the variable x is not initialised it is not storing any value, therefore nothing will be printed on the
screen.
2. What will be the output of the following PHP code ?
<?php
$x = 5;
{
$x = 10;
echo "$x";
}
echo "$x";
?>
a) 1010
b) 105
c) 510
d) error
View Answer
Answer: a
Explanation: Variable x stores the value 10 and not 5.
3. What will be the output of the following PHP code ?
<?php
$x = 5;
{
echo "$x";
}
?>
a) 0
b) 5
c) Nothing
d) Error
View Answer
Answer: b
Explanation: The variable x stores the value 5 and therefore the value 5 is printed on the screen.
4. What will be the output of the following PHP code ?
<?php
$x = 5;
function fun()
{
echo "$x";
}
fun();
?>
a) 0
b) 5
c) Nothing
d) Error
View Answer
Answer: c
Explanation: The variable x is not defined inside the function fun(), therefore nothing is printed on the screen.
5. What will be the output of the following PHP code ?
<?php
$x = 5;
function fun()
{
$x = 10;
echo "$x";
}
fun();
echo "$x";
?>
a) 0
b) 105
c) 510
d) Error
View Answer
Answer: b
Explanation: First when the function is called variable x is initialised to 10 so 10 is printed later the global value 5 is
printed.
6. What will be the output of the following PHP code ?
<?php
$x = 4;
$y = 3;
function fun($x = 3, $y = 4)
{
$z = $x+$y/$y+$x;
echo "$z";
}
echo $x;
echo $y;
echo $z;
fun($x, $y);
?>
a) 43
b) 943
c) 349
d) 439
View Answer
Answer: d
Explanation: Firstly, the statements outside the function are printed, since z is not defined it’ll no value is printed for
z. Next the function is called and the value of z inside the function is printed.
advertisement

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


<?php
$x = 4;
$y = 3;
function fun($x, $y)
{
$z = $x + $y / $y + $x;
echo "$z";
}
echo $x;
echo $y;
echo $z;
fun(3, 4);
?>
a) 437
b) 439
c) 349
d) 347
View Answer
Answer: a
Explanation: It is same as above but the value passed into the function is 3,4 and not 4,3. Therefore the difference in
answer.
8. What will be the output of the following PHP code ?
<?php
function fun($x,$y)
{
$x = 4;
$y = 3;
$z = $x + $y / $y + $x;
echo "$z";
}
fun(3, 4);
?>
a) 7
b) 9
c) 0
d) Error
View Answer
Answer: b
Explanation: Value 3, 4 is passed to the function but that is lost because x and y are initialised to 4 and 3 inside the
function. Therefore we get the given result.
9. What will be the output of the following PHP code ?
<?php
$x = 3, 4, 5, 6;
echo "$x";
?>
a) 3
b) 4
c) 6
d) Error
View Answer
Answer: d
Explanation: In C you won’t get an error but in PHP you’ll get a syntax error.
10. What will be the output of the following PHP code ?
<?php
$a = 10;
$b = 4;
$c = fun(10,4);
function fun($a,$b)
{
$b = 3;
return $a - $b + $b - $a;
}
echo $a;
echo $b;
echo $c;
?>
a) 104
b) 410
c) 1400
d) 4100
View Answer
Answer: c
Explanation: The value returned from the function is 0, and value of a is 10, value of b is 4 and c is 0.
PHP Coding Questions and Answers – Operators – 1
This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Operators – 1”.
1. What will be the output of the following PHP code ?
<?php
$a = 10;
echo ++$a;
echo $a++;
echo $a;
echo ++$a;
?>
a) 11111213
b) 11121213
c) 11111212
d) 11111112
View Answer
Answer: a
Explanation: ++$a increments a and then prints it,$a++ prints and then increments.
2. What will be the output of the following PHP code ?
<?php
$a = 12;
--$a;
echo $a++;
?>
a) 11
b) 12
c) 10
d) error
View Answer
Answer: a
Explanation: The + operator does union of arrays in that order, then the === operator compares key and value pairs.
3. What will be the output of the following PHP code ?
<?php
$x = "test";
$y = "this";
$z = "also";
$x .= $y .= $z ;
echo $x;
echo $y;
?>
a) testthisthisalso
b) testthis
c) testthisalsothisalso
d) error at line 4
View Answer
Answer: c
Explanation: The x .= y is a shorthand for x = x.y and this is evaluated from right to left.
4. What will be the output of the following PHP code ?
<?php
$x = 1;
$y = 2;
if (++$x == $y++)
{
echo "true ", $y, $x;
}
?>
a) no output
b) true 23
c) true 22
d) true 33
View Answer
Answer: b
Explanation: x is preincremented and y is post incremented thus both are 2 in the if condition, later y is increment.
5. What will be the output of the following PHP code ?
<?php
$y = 2;
$w = 4;
$y *= $w /= $y;
echo $y, $w;
?>
a) 80.5
b) 44
c) 82
d) 42
View Answer
Answer: d
Explanation: Expression is evaluated from right to left.
6. What will be the output of the following PHP code ?
<?php
$y = 2;
if ($y-- == ++$y)
{
echo $y;
}
?>
a) 2
b) 1
c) 3
d) no output
View Answer
Answer: a
Explanation: First $y = 2 is compared to and then decremented, then incremented and compared to $y = 2.
advertisement

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


<?php
$y = 2;
if (**$y == 4)
{
echo $y;
}
?>
a) 4
b) 2
c) error at line2
d) no output
View Answer
Answer: c
Explanation: The ** is not a valid operator,only ++ and — exist.
8. What will be the output of the following PHP code ?
<?php
$y = 2;
if (--$y == 2 || $y xor --$y)
{
echo $y;
}
?>
a) 1
b) 0
c) 2
d) no output
View Answer
Answer: b
Explanation: –$y == 2 is false but y is decremented, the xor gives true if only one of the operands are true, thus 1
xor 0 is true.
9. What will be the output of the following PHP code ?
<?php
$y = 2;
if (--$y <> ($y != $y++))
{
echo $y;
}
?>
a) 1
b) 0
c) 2
d) no output
View Answer
Answer: b
Explanation: –$y == 2 is false but y is decremented, the xor gives true if only one of the operands are true, thus 1
xor 0 is true.
10. What will be the output of the following PHP code ?
<?php
echo $x-- != ++$x;
?>
a) 1
b) 0
c) error
d) no output
View Answer
Answer: a
Explanation: Automatically x is declared and initialized to 0,then decremented and compared with its increments,
thus returns 1.
11. What will be the output of the following PHP code ?
<?php
$auth = 1;
$status = 1;
if ($result = (($auth == 1) && ($status != 0)))
{
print "result is $result<br />";
}
?>
a) result is true
b) result is 1
c) error
d) no output
View Answer
Answer: b
Explanation: Result is x&&y which returns 1 if both x and y are true.
PHP Coding Questions and Answers – Operators – 3
This set of PHP Questions and Answers for Entrance exams focuses on “Operators – 3”.
1. What will be the output of the following PHP code ?
<?php
echo 5 * 9 / 3 + 9;
?>
a) 24
b) 3.7
c) 3.85
d) 0
View Answer
Answer: a
Explanation: Operator precedence order must be followed.
2. What will be the output of the following PHP code ?
<?php
echo 5 * 9 / 3 + 9
?>
a) 24
b) 3.7
c) 3.85
d) 0
View Answer
Answer: a
Explanation: Operator precedence order must be followed.
3. What will be the output of the following PHP code ?
<?php
$i = 0;
$j = 0;
if ($i && ($j = $i + 10)) {
echo "true";
}
echo $j;
?>
a) 10
b) 0
c) true0
d) true10
View Answer
Answer: b
Explanation: In if condition when the first case is 0 and is an && operation then the second command is not
executed.
4. What will be the output of the following PHP code ?
<?php
$i = 10;
$j = 0;
if ($i || ($j = $i + 10)) {
echo "true";
}
echo $j;
?>
a) 20
b) true0
c) 0
d) true20
View Answer
Answer: b
Explanation: In if condition when the first case is 1 and is an || operation then the second command is not executed.
5. What will be the output of the following PHP code ?
<?php
$i = 1;
if ($i++ && ($i == 1))
printf("Yes\n$i");
else
printf("No\n$i");
?>
a) No 2
b) Yes 1
c) Yes 2
d) No 1
View Answer
Answer: a
Explanation: The first condition returns true and increments but the second condition is false.
6. What will be the output of the following PHP code ?
<?php
$a = 1; $b = 3;
$d = $a++ + ++$b;
echo $d;
?>
a) 5
b) 4
c) 3
d) error
View Answer
Answer: a
Explanation: Post increment of a is done after expression evaluation.
advertisement

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


<?php
$a = 1; $b = 1; $d = 1;
print ++$a + ++$a+$a++; print $a++ + ++$b; print ++$d + $d++ + $a++;
?>
a) 869
b) 742
c) 368
d) error
View Answer
Answer: a
Explanation: Follow the order of post and pre increments.
8. What will be the output of the following PHP code ?
<?php
$a = 10; $b = 10;
if ($a = 5)
$b--;
print $a;print $b--;
?>
a) 58
b) 59
c) 109
d) 108
View Answer
Answer: b
Explanation: a is set to 5 in the if condition and b is post decremented in the print statement.
9. What will be the output of the following PHP code ?
<?php
$i = 0;
$x = $i++; $y = ++$i;
print $x; print $y;
?>
a) 02
b) 12
c) 01
d) 21
View Answer
Answer: a
Explanation: First case i is incremented after setting x to i.
10. What will be the output of the following PHP code ?
<?php
$a = 5; $b = -7; $c =0;
$d = ++$a && ++$b || ++$c;
print $d; print $a;
?>
a) 16
b) 06
c) 15
d) 05
View Answer
Answer: a
Explanation: 1&&0||1 is evaluated to 1 and the a is also preincremented to 6.
11. What will be the output of the following PHP code ?
<?php
$b = 1; $c = 4; $a = 5;
$d = $b + $c == $a;
print $d;
?>
a) 5
b) 0
c) 10
d) 1
View Answer
Answer: d
Explanation: First b and c are added and then tested if d=5,which is true thus return 1.
PHP Coding Questions and Answers – Operators – 5
This set of Tough PHP Interview Questions & Answers focuses on “Operators – 5”.
1. What will be the output of the following PHP code ?
<?php
$i = 0; $j = 1; $k = 2;
print !(($i + $k) < ($j - $k));
?>
a) 1
b) true
c) false
d) 0
View Answer
Answer: a
Explanation: True is 1.
2. What will be the output of the following PHP code ?
<?php
$i = 0;$j = 1;$k = 2;
print !(( + + $i + $j) > ($j - $k));
?>
a) 1
b) no output
c) error
d) 0
View Answer
Answer: b
Explanation: The equation outputs false .
3. What will be the output of the following PHP code ?
<?php
$i = 0;$j = 1;$k = 2;
print (( + + $i + $j) >! ($j - $k));
?>
a) 1
b) no output
c) error
d) 0
View Answer
Answer: a
Explanation: Negation of a number is 0.
4. What will be the output of the following PHP code ?
<?php
$a = 0x6db7;
print $a<<6;
?>
a) 1797568
b) no output
c) error
d) 0x6dc0
View Answer
Answer: a
Explanation: The output is in decimal.
5. What will be the output of the following PHP code ?
<?php
$a = 'a' ;
print $a * 2;
?>
a) 192
b) 2
c) error
d) 0
View Answer
Answer: d
Explanation: Characters cannot be multiplied.
6. What will be the output of the following PHP code ?
<?php
$a = '4' ;
print + + $a;
?>
a) no output
b) error
c) 5
d) 0
View Answer
Answer: c
Explanation: The character is type casted to integer before multiplying.
advertisement

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


<?php
$a = '12345';
print "qwe{$a}rty";
?>
a) qwe12345rty
b) qwe{$a}rty
c) error
d) no output
View Answer
Answer: a
Explanation: {$}dereferences the variable within.
8. What will be the output of the following PHP code ?
<?php
$a = '12345';
print "qwe".$a."rty";
?>
a) qwe12345rty
b) qwe$arty
c) error
d) no output
View Answer
Answer: a
Explanation: . dereferences the variable/string within.
9. What will be the output of the following PHP code ?
<?php
$a = '12345';
echo 'qwe{$a}rty';
?>
a) qwe12345rty
b) qwe{$a}rty
c) error
d) no output
View Answer
Answer: b
Explanation: qwe{$a}rty, single quotes are not parsed.
10. What will be the output of the following PHP code ?
<?php
$a = '12345';
echo "qwe$arty";
?>
a) qwe12345rty
b) qwe$arty
c) qwe
d) error
View Answer
Answer: c
Explanation: qwe, because $a became $arty, which is undefined.
PHP Coding Questions and Answers – If – Else – If – 1
This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “If – Else – If – 1”.
1. What will be the output of the following PHP code?
<?php
$x;
if ($x)
print "hi" ;
else
print "how are u";
?>
a) how are u
b) hi
c) error
d) no output
View Answer
Answer: a
Explanation: Uninitialized x is set to 0, thus if condition fails.
2. What will be the output of the following PHP code ?
<?php
$x = 0;
if ($x++)
print "hi";
else
print "how are u";
?>
a) hi
b) no output
c) error
d) how are u
View Answer
Answer: d
Explanation: x is incremented after if which evaluates to false.
3. What will be the output of the following PHP code ?
<?php
$x;
if ($x == 0)
print "hi" ;
else
print "how are u";
print "hello"
?>
a) how are uhello
b) hihello
c) hi
d) no output
View Answer
Answer: b
Explanation: else condition without brackets performs the following statements only.
4. What will be the output of the following PHP code ?
<?php
$x = 0;
if ($x == 1)
if ($x >= 0)
print "true";
else
print "false";
?>
a) true
b) false
c) error
d) no output
View Answer
Answer: d
Explanation: The nested for loop is not entered if outer condition is false.
5. What will be the output of the following PHP code ?
<?php
$a = 1;
if ($a--)
print "True";
if ($a++)
print "False";
?>
a) true
b) false
c) error
d) no output
View Answer
Answer: a
Explanation: Due to post increment and post decrement only the first condition is satisfied.
6. What will be the output of the following PHP code ?
<?php
$a = 1;
if (echo $a)
print "True";
else
print "False";
?>
a) true
b) false
c) error
d) no output
View Answer
Answer: c
Explanation: echo does not return anything so if condition is empty.
advertisement

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


<?php
$a = 1;
if (print $a)
print "True";
else
print "False";
?>
a) true
b) false
c) error
d) no output
View Answer
Answer: a
Explanation: print returns 1 if it prints anything.
8. What will be the output of the following PHP code ?
<?php
$a = 10;
if (1)
print "all";
else
print "some"
else
print "none";
?>
a) all
b) some
c) error
d) none
View Answer
Answer: c
Explanation: Hanging else statement.
9. What will be the output of the following PHP code ?
<?php
$a = 10;
if (0)
print "all";
if
else
print "some"
?>
a) all
b) some
c) error
d) no output
View Answer
Answer: c
Explanation: No else statement to end the if statement.
10. What will be the output of the following PHP code ?
<?php
$a = "";
if ($a)
print "all";
if
else
print "some";
?>
a) all
b) some
c) error
d) no output
View Answer
Answer: b
Explanation: Empty string is evaluated to 0.
11. What will be the output of the following PHP code ?
<?php
$a = "a";
if ($a)
print "all";
else
print "some";
?>
a) all
b) some
c) error
d) no output
View Answer
Answer: a
Explanation: The value of a is evaluated to 1 as it has a value.
PHP Coding Questions and Answers – If-Else-If-2
This set of PHP Question Bank focuses on ” If-Else-If-2″.
1. What will be the output of the following PHP code ?
<?php
$x = 10;
$y = 20;
if ($x > $y + $y != 3)
print "hi" ;
else
print "how are u";
?>
a) how are u
b) hi
c) error
d) no output
View Answer
Answer: b
Explanation: Expression evaluates to true.
2. What will be the output of the following PHP code ?
<?php
$x = 10;
$y = 20;
if ($x > $y && 1||1)
print "hi" ;
else
print "how are u";
?>
a) how are u
b) hi
c) error
d) no output
View Answer
Answer: b
Explanation: Expression evaluates to true.
3. What will be the output of the following PHP code ?
<?php
$x = 10;
$y = 20;
if ($x > $y && 1||1)
print "hi" ;
else
print "how are u";
?>
a) how are u
b) hi
c) error
d) no output
View Answer
Answer: b
Explanation: Expression evaluates to true.
4. What will be the output of the following PHP code ?
<?php
if (-100)
print "hi" ;
else
print "how are u";
?>
a) how are u
b) hi
c) error
d) no output
View Answer
Answer: b
Explanation: Expression evaluates to true.
5. What will be the output of the following PHP code ?
<?php
if (0.1)
print "hi" ;
else
print "how are u";
?>
a) how are u
b) hi
c) error
d) no output
View Answer
Answer: b
Explanation: Expression evaluates to true.
6. What will be the output of the following PHP code ?
<?php
if (0.0)
print "hi" ;
else
print "how are u";
?>
a) how are u
b) hi
c) error
d) no output
View Answer
Answer: a
Explanation: Expression evaluates to false.
advertisement

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


<?php
if (print "0")
print "hi" ;
else
print "how are u";
?>
a) 0how are u
b) 0hi
c) hi
d) how are u
View Answer
Answer: b
Explanation: Expression evaluates to true as print returns 1.
8. What will be the output of the following PHP code ?
<?php
$x = 1;
if ($x == 2)
print "hi" ;
else if($x = 2)
print $x;
else
print "how are u";
?>
a) error
b) 2
c) hi
d) how are u
View Answer
Answer: b
Explanation: Enters if else as first condition is false and thus x is set to 2.
9. What will be the output of the following PHP code ?
<?php
$x = 1;
if ($x = $x&0)
print $x ;
else
print "how are u";
?>
a) 0
b) 1
c) error
d) how are u
View Answer
Answer: d
Explanation: x&0 is 0,thus evaluated to false.
10. What will be the output of the following PHP code ?
<?php
$x = 1;
if ($x = $x&0)
print $x ;
else
print "how are u";
?>
a) 0
b) 1
c) error
d) how are u
View Answer
Answer: d
Explanation: x&0 is 0,thus evaluated to false.
11. What will be the output of the following PHP code ?
<?php
$x = 1;
if ($x = $x&0)
print $x;
else
break;
?>
a) 0
b) 1
c) error
d) no output
View Answer
Answer: c
Explanation: break is not defined for a if else ladder.
12. What will be the output of the following PHP code ?
<?php
$a = 100;
if ($a > 10)
printf("M.S. Dhoni");
else if ($a > 20)
printf("M.E.K Hussey");
else if($a > 30)
printf("A.B. de villiers");
?>
a) M.S.Dhoni
b) M.E.K.Hussey
c) M.S.Dhoni
M.E.K.Hussey
A.B.de villiers
d) No output
View Answer
Answer: a
Explanation: In if else if one condition is satisfied then no other condition is checked.
PHP Coding Questions and Answers – While Loops – 1
This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “While Loops – 1”.
1. What will be the output of the following PHP code ?
<?php
while()
{
print "hi";
}
?>
a) infinite loop
b) hi
c) no output
d) error
View Answer
Answer: d
Explanation: The while loop cannot be defined without a condition.
2. What will be the output of the following PHP code ?
<?php
do
{
print "hi";
}
while(0);
print "hello";
?>
a) infinite loop
b) hihello
c) hello
d) error
View Answer
Answer: b
Explanation: The do while loop executes at least once as the condition is in the while loop.
3. What will be the output of the following PHP code ?
<?php
$i = 0
do
{
print "hi";
$i++;
}
while ($i != 3);
?>
a) hi
hi
b) hi
c) hi
hi
hi
hi
d) no output
View Answer
Answer: c
Explanation: The check happens after the increment,thus it prints until i = 4.
4. What will be the output of the following PHP code ?
<?php
$i = 0
while ($i != 3)
{
print "hi";
$i++;
}
?>
a) hi
hi
b) hi
hi
hi
c) hi
hi
hi
hi
d) no output
View Answer
Answer: b
Explanation: The check happens before the increment, thus it prints until i = 3.
5. What will be the output of the following PHP code ?
<?php
$i = 0
while ($i < 3)
{
print "hi";
$i--;
}
print "hello"
?>
a) hi
hi
hello
b) hi
hi
hi
hello
c) hi
hi
hi
hi
hello
d) infinite loop
View Answer
Answer: d
Explanation: There is no increment of i making it infinite.
6. What will be the output of the following PHP code ?
<?php
$i = 0
while ($i < 3)
{
$i++;
}
print $i;
?>
a) 2
b) 3
c) 0
d) 1
View Answer
Answer: b
Explanation: The increment happens and then the check happens.
advertisement

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


<?php
$i = 0
do
{
$i++;
}
while ($i < 3);
print $i;
?>
a) 2
b) 3
c) 0
d) 1
View Answer
Answer: b
Explanation: The increment happens and then the check happens.
8. What will be the output of the following PHP code ?
<?php
$i = 0
while ($i++)
{
print $i;
}
print $i;
?>
a) 0
b) infinite loop
c) 01
d) 1
View Answer
Answer: d
Explanation: As it is a post increment, it checks and then does not enter the loop, thus prints only 1.
9. What will be the output of the following PHP code ?
<?php
$i = "";
while($i)
{
print "hi";
}
print "hello";
?>
a) hello
b) infinite loop
c) hihello
d) error
View Answer
Answer: a
Explanation: While accept does not accept anything other than a 0 or any other number as false and true.
10. What will be the output of the following PHP code ?
<?php
$i = "";
while ($i)
{
print "hi";
}
while($i < 8)
$i++;
print "hello";
?>
a) Hi is printed 8 times, hello 7 times and then hi 2 times
b) Hi is printed 10 times, hello 7 times
c) Hi is printed once, hello 7 times
d) Hi is printed once, hello 7 times and then hi 2 times
View Answer
Answer: d
Explanation: The while loop ends only when a } is encountered.
PHP Coding Questions and Answers – While Loops – 2
This set of Tricky PHP Interview Questions & Answers focuses on “While Loops – 2”.
1. What will be the output of the following PHP code ?
<?php
$i = 0;
while($i = 10)
{
print "hi";
}
print "hello";
?>
a) hello
b) infinite loop
c) hihello
d) error
View Answer
Answer: b
Explanation: While condition always gives 1.
2. What will be the output of the following PHP code ?
<?php
$i = "";
while ($i = 10)
{
print "hi";
}
print "hello";
?>
a) hello
b) infinite loop
c) hihello
d) error
View Answer
Answer: b
Explanation: While condition always gives 1.
3. What will be the output of the following PHP code ?
<?php
$i = 5;
while (--$i > 0)
{
$i++; print $i; print "hello";
}
?>
a) 4hello4hello4hello4hello4hello…..infinite
b) 5hello5hello5hello5hello5hello…..infinite
c) no output
d) error
View Answer
Answer: b
Explanation: i is decremented in the while loop in the condition check and then incremented back.
4. What will be the output of the following PHP code ?
<?php
$i = 5;
while (--$i > 0 && ++$i)
{
print $i;
}
?>
a) 5
b) 555555555…infinitely
c) 54321
d) error
View Answer
Answer: b
Explanation: As it is && operator it is being incremented and decremented continuously.
5. What will be the output of the following PHP code ?
<?php
$i = 5;
while (--$i > 0 || ++$i)
{
print $i;
}
?>
a) 54321111111….infinitely
b) 555555555…infinitely
c) 54321
d) 5
View Answer
Answer: a
Explanation: As it is || operator the second expression is not evaluated till i becomes 1 then it goes into a loop.
6. What will be the output of the following PHP code ?
<?php
$i = 0;
while(++$i || --$i)
{
print $i;
}
?>
a) 1234567891011121314….infinitely
b) 01234567891011121314…infinitely
c) 1
d) 0
View Answer
Answer: a
Explanation: As it is || operator the second expression is not evaluated and i is always incremented, in the first case
to 1.
advertisement
7. What will be the output of the following PHP code ?
<?php
$i = 0;
while (++$i && --$i)
{
print $i;
}
?>
a) 1234567891011121314….infinitely
b) 01234567891011121314…infinitely
c) no output
d) error
View Answer
Answer: c
Explanation: The first condition itself fails thus the loop exists.
8. What will be the output of the following PHP code ?
<?php
$i = 0;
while ((--$i > ++$i) - 1)
{
print $i;
}
?>
a) 00000000000000000000….infinitely
b) -1-1-1-1-1-1-1-1-1-1…infinitely
c) no output
d) error
View Answer
Answer: a
Explanation: (–$i > ++$i) evaluates to 0 but -1 makes it enters the loop and prints i.
9. What will be the output of the following PHP code ?
<?php
$i = 2;
while (++$i)
{
while ($i --> 0)
print $i;
}
?>
a) 210
b) 10
c) no output
d) infinite loop
View Answer
Answer: a
Explanation: The loop ends when i becomes 0.
10. What will be the output of the following PHP code ?
<?php
$i = 2;
while (++$i)
{
while (--$i > 0)
print $i;
}
?>
a) 210
b) 10
c) no output
d) infinite loop
View Answer
Answer: d
Explanation: The loop never ends as i is always incremented and then decremented.

You might also like