PHP Basic MCQ - General Knowledge Question and Answer

PHP Basic MCQ - Question and Answer

This is the questions and answers section on "PHP Basics" with explanation for various interview, competitive examination and entrance test. Solved examples with detailed answer description, explanation are given and it would be easy to understand.

What does PHP stand for?
i) Personal Home Page
ii) Hypertext Preprocessor
iii) Pretext Hypertext Processor
iv) Preprocessor Home Page
 


Both (i) and (ii)
Both (ii) and (iv)
Only (ii)
Both (i) and (iii)

Answer:

PHP previously stood for Personal Home Page now stands for Hypertext Preprocessor.

Share Me:

Who is the father of PHP?  


Rasmus Lerdorf
Willam Makepiece
Drek Kolkevi
List Barely

Answer:

Rasmus Lerdorf

Share Me:

PHP files have a default file extension of.  


.html
.xml
.php
.ph

Answer:

.php

Share Me:

A PHP script should start with ___ and end with ___:  


< php >
< ? php ?>
< ? ? >
< ?php ? >

Answer:

Every section of PHP code starts and ends by turning on and off PHP tags to let the server know that it needs to execute the PHP in between them.

Share Me:

Which of the looping statements is/are supported by PHP?
i) for loop
ii) while loop
iii) do-while loop
iv) foreach loop
 


(i) and (ii)
(i), (ii) and (iii)
All of the mentioned
None of the mentioned

Answer:

All of the mentioned

Share Me:

Which of the following is/are a PHP code editor?
i) Notepad
ii) Notepad++
iii) Adobe Dreamweaver
iv) PDT
 


Only (iv)
All of the mentioned.
(i), (ii) and (iii)
Only (iii)

Answer:

Any of the above editors can be used to type PHP code and run it.

Share Me:

Which of the following must be installed on your computer so as to run PHP script?
i) Adobe Dreamweaver
ii) PHP
iii) Apache
iv) IIS
 


All of the mentioned
Only (ii)
(ii) and (iii)
(ii), (iii) and (iv)

Answer:

To run PHP code you need to have PHP and a web server, both IIS and Apache are web servers.You can choose either one according to your platform.

Share Me:

Which version of PHP introduced Try/catch Exception?  


PHP 4
PHP 5
PHP 5.3
PHP 6

Answer:

Version 5 added support for Exception Handling.

Share Me:

We can use ___ to comment a single line?
i) /?
ii) //
iii) #
iv) /* */
 


Only (ii)
(i), (iii) and (iv)
(ii), (iii) and (iv)
Both (ii) and (iv)

Answer:

/* */ can also be used to comment just a single line although it is used for paragraphs. // and # are used only for single line comment.

Share Me:

Which of the below symbols is a newline character?  


\r
\n
/n
/r

Answer:

PHP treats \n as newline character.

Share Me:

Which of the following php statement/statements will store 111 in variable num?
i) int $num = 111;
ii) int mum = 111;
iii) $num = 111;
iv) 111 = $num;
 


Both (i) and (ii)
All of the mentioned.
Only (iii)
Only (i)

Answer:

You need not specify the datatype in php.

Share Me:

What will be the output of the following php code
< ?php 
$num  = 1;
$num1 = 2;
print $num . "+". $num1 ;
?>
 


3
1+2
1.+.2
Error

Answer:

.(dot) is used to combine two parts of the statement. Example ( $num . “Hello World” ) will output 1Hello World.

Share Me:

What will be the output of the following php code?
< ?php 
$num  = "1";
$num1 = "2";
print $num+$num1 ;
?>
 


3
1+2
Error
12

Answer:

The numbers inside the double quotes are considered as integers and not string, therefore the value 3 is printed and not 1+2.

Share Me:

Which of following variables can be assigned a value to it?
(i) $3hello
(ii) $_hello
(iii) $this
(iv) $This
 


All of the mentioned
nly (ii)
(ii), (iii) and (iv)
(ii) and (iv)

Answer:

A variable can’t start with a number. Also $this is a special variable that can’t be assigned, but $This can be assigned.

Share Me:

What will be the output of the following code?
< ?php 
$foo = 'Bob';              
$bar = &$foo;              
$bar = "My name is $bar";  
echo $bar;
echo $foo;
?>
 


Error
My name is BobBob
My name is BobMy name is Bob
My name is Bob Bob

Answer:

The $bar = &$foo; line will reference $foo via $bar.

Share Me:

If $a = 12 what will be returned when ($a == 12) ? 5 : 1 is executed?  


12
1
Error
5

Answer:

?: is known as ternary operator. If condition is true then the part just after the ? is executed else the part after : .

Share Me:

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”);
 


(i) and (ii)
(i), (ii) and (iii)
All of the mentioned
(i), (ii) and (iv)

Answer:

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.

Share Me:

What will be the output of the following PHP code?
< ?php 
$color = "maroon";
$var = $color[2];
echo "$var" ;
?>
 


a
Error
$var
r

Answer:

PHP treats strings in the same fashion as arrays, allowing for specific characters to be accessed via array offset notation.

Share Me:

What will be the output of the following PHP code?
< ?php 
$score = 1234;
$scoreboard = (array) $score;
echo $scoreboard[0];
?>
 


1
Error
1234
2

Answer:

The (array) is a cast operator which is used for converting values from other data types to array.

Share Me:

What will be the output of the following PHP code?
< ?php 
$total = "25 students";
$more = 10;
$total = $total + $more;
echo "$total" ;
?>
 


Error
35 students
35
25 students

Answer:

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.

Share Me:

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"; }
?>
 


I love arsenal
Error
I love arsenalI love manc
I love arsenalI love mancI love manu

Answer:

If a break statement isn’t present, all subsequent case blocks will execute until a break statement is located.

Share Me:

What will be the output of the following PHP code?
< ?php 
$a = "clue";
$a .= "get";
echo "$a";
?>
 


get
true
false
clueget

Answer:

.= is a concatenation-assignment. $a equals its current value concatenated with “get”.

Share Me:

What will be the output of the following code?
< ?php 
function track() {
static $count = 0;
$count++;
echo $count ;
}
track();
track();
track();
?>
 


123
111
000
011

Answer:

Because $count is static, it retains its previous value each time the function is executed.

Share Me:

Which statement will output $x on the screen?  


echo “\$x”;
echo “$$x”;
echo “/$x”;
echo “$x;

Answer:

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.

Share Me:

Which of the below statements is equivalent to $add += $add ?  


$add = $add
$add = $add +$add
$add = $add + 1
$add = $add + $add + 1

Answer:

a += b is an addition assignment whose outcome is a = a + b. Same can be done with subtraction,multiplication,division etc.

Share Me:

What will be the output of the following PHP code?
< ?php 
    $num = 10;
    echo 'What is her age? \n She is $num years old';
?>
 


What is her age? \n She is $num years old
What is her age? She is $num years old
What is her age? She is 10 years old
What is her age? She is 10 years old

Answer:

When a string is enclosed within single quotes both variables and escape sequences will not be interpreted when the string is parsed.

Share Me: