Are you ready?
15 questions to solve
Instructions
- This quiz goes to full-screen once you press the Start button.
- At the end of the quiz, you are able to review all the questions that you answered wrong and see their explanations.
What's the problem in the code below?
<?php
echo 'Hello World!';
There is absolutely no problem in the given code — it's perfectly fine. Hence, the correct choice is (D).
$1str
is a valid variable name in PHP. Yes or no?$1str
is not a valid name for a variable because it has a digit at its beginning. Refer to PHP Variables for more information.Which of the following denotes a comment in PHP?
There are three ways to denote a comment in PHP: using
//
, #
and /* */
. Hence, the correct choice is (D).Variable names in PHP are case-sensitive. True or false?
That's completely true — variable names are case-sensitive in PHP. That is,
$greeting
is different than $GREETING
and $Greeting
. Refer to PHP Variables for more information.A unary operator takes how many operands?
The term 'unary' stands for 'one' and that means that a unary operator takes one operand. Hence, the correct choice is (A). Refer to PHP Operations for more information.
What does the following code output?
<?php
$x = true;
$y = false;
var_dump($x && $y);
var_dump($x || $y);
The
&&
operator is the logical AND operator whereas ||
is the logical OR operator. &&
evaluates to true
if both its operands are true
, or else to false
. Similarly, ||
evaluates to true
if either of its operands is true
, or else to false
. Hence, the correct choice is (C). Refer to PHP Operations for more information.What does the following code output?
<?php
$x = 50;
if ($x % 2 === 0) {
echo '2', "\n";
}
elseif ($x % 10 === 0) {
echo '10', "\n";
}
The condition in the first
if
statement, i.e. $x % 2 === 0
, is met, and likewise, the first if
statement executes. Since the next statement is an elseif
, and not a standalone if
statement, it's skipped because the preceding if
has already been executed. Hence, 2
is printed and this goes with choice (B). Refer to PHP Control Flow for more information.In the code below, what is
(int)
called?<?php
$x = (int) '10';
echo $x;
(int)
is referred to as a typecast and serves to convert a given value into an integer. Refer to PHP Data Types for more information.Which of the following is a function to get the type of a given value?
gettype()
is used to return the type of a given value in PHP. Hence, the correct choice is (C). Refer to PHP Data Types for more information.What does the following code output?
<?php
$nums = [1, 3, 4, 10];
for ($i = 0; $i < count($nums) - 1; $i++) {
echo $nums[$i];
}
The condition
$i < count($nums) - 1
causes the loop to iterate from the first element of $nums
to the second last element. Moreover, echo $nums[i]
prints each of the numbers one after another, without leaving a blank line or space between them. This results in the output 134
. Hence, the correct choice is (C). Refer to PHP Control Flow for more information.What does
10++
return?The increment operator, or in general any assignment-related operator, can only be used with variables (or other identifiers).
10
is a literal, not a variable, and thus it's illegal to use the increment operator with it. Hence, the correct choice is (C). Refer to PHP Operations for more information.What's the problem in the code below?
<?php
do
echo 'Please enter 0 > ';
$input = rtrim(fgets(STDIN));
while ($input !== '0');
echo "You entered '0'";
The problem is that the body of
do...while
from line 4 to 5 is not encapsulated in a pair of curly braces ({}
). The curly braces denotes a block statement in PHP and since only a single statement is expected after do
, we need to use a block statement after do
and then put all the respective statements inside the block. Refer to PHP Control Flow for more information.In the code below, what is
$a
called?<?php
function add($a, $b) {
return $a + $b;
}
$a
is a parameter of the function add()
. Hence, the correct choice is (C). Refer to PHP Functions for more information.What does the following code output?
<?php
function add($a, $b) {
$a + $b;
}
var_dump(add(10, 20));
When a function doesn't have an explicit
return
statement, PHP automatically returns NULL
. Likewise, in the code above add(10, 20)
returns NULL
and then var_dump(NULL)
prints NULL
. Hence, the correct choice is (C). Refer to PHP Functions for more information.In the code below, what is the scope of the variable
$a
?<?php
$a = 10;
echo $a;
A variable defined outside all exisiting function definitions is a global variable in PHP i.e. it has a global scope. Hence, the correct choice is (A). Refer to PHP Scoping for more information.