PHP Questions & Answers – Basics – 2

This set of PHP multiple choice questions and answers concentration on PHP basics. It will be excellent for anyone learning PHP Basics and groundwork

1. 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.

2. What will be the output of the following PHP code?
<?php
$score = 1234;
$scoreboard = (array) $score;
echo $scoreboard[0];
?>
a) 1
b) Error
c) 2
d) 1234
View Answer

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

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

<?php
$total = “25 students”;
$more = 10;
$total = $total + $more;
echo “$total”;
?>
a) Error
b) 35
c) 35 students
d) 25 students
View Answer

Answer: b
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.

4. Which of the below statements is equivalent to $add += $add ?
a) $add = $add
b) $add = $add + $add + 1
C) $add = $add + 1
d) $add = $add +$add
View Answer
Answer: d
Explanation: a += b is an addition assignment whose outcome is a = a + b. Same can be done with subtraction,multiplication,division etc.

5. Which of following variables can be assigned a value to it?
i) $3hello
ii) $_hello
iii) $this
iv) $This
a) All of the mentioned
b) Only ii)
c) ii), iii) and iv)
d) ii) and iv)
View Answer

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

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

<?php
$a = “clue”;
$a .= “get”;
echo “$a”;
?>
a) clueget
b) true
c) false
d) get
View Answer

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

7. What will be the output of the following code?
<?php
function track() {
static $count = 0;
$count++;
echo $count;
}
track();
track();
track();
?>
a) 000
b) 111
c) 123
d) 011
View Answer

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

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. 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.

10. 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:The $bar = &$foo; line will reference $foo via $bar.