This set of PHP 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
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 PHP 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
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) i), ii), iii) and iv)
d) i), ii) and iv)
View Answer
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
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
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?
<?php
$total = "25 students";
$more = 10;
$total = $total + $more;
echo "$total";
?>
a) Error
b) 35 students
c) 35
d) 25 students
View Answer
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
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
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 PHP code?
<?php
function track() {
static $count = 0;
$count++;
echo $count;
}
track();
track();
track();
?>
a) 123
b) 111
c) 000
d) 011
View Answer
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
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.
Sanfoundry Global Education & Learning Series – PHP Programming.
To practice all areas of PHP, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.
- Practice MCA MCQs
- Check PHP Books
- Apply for Programming Internship
- Check Information Technology Books
- Practice Programming MCQs