PHP Questions and Answers for Freshers on “Basics – 3”.
1. What will be the output of the following PHP code?
-
-
$a = 5;
-
$b = 5;
-
echo ($a === $b);
-
?>
a) 5 === 5
b) Error
c) 1
d) False
Answer: c
Clarification: === 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
Answer: b
Clarification: PHP treats n as a newline character.
3. What will be the output of the following PHP code?
-
-
$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
Answer: a
Clarification: 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)
Answer: d
Clarification: 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?
-
-
$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
Answer: c
Clarification: If a break statement isn’t present, all subsequent case blocks will execute until a break statement is located.
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)
Answer: c
Clarification: 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?
-
-
$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
Answer: b
Clarification: 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
Answer: d
Clarification: ?: is known as ternary operator. If condition is true then the part just after the ? is executed else the part after : .
9. What will be the value of $a and $b after the function call in the following PHP code?
-
-
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
