NEW
Font size
WorksheetsPHP Conditional Statements Quiz
Total questions: 40
Worksheet time: 20mins
What is the simplest form of a conditional statement in PHP?
if() else
if()
switch
elseif()
Which operator is used to compare values in a PHP conditional statement?
Arithmetic operator
Comparison operator
Logical operator
Assignment operator
What will be the output of the following code if $grade = 80? if($grade < 75) { $remark = "Failed"; } else { $remark = "Passed"; }
"Failed"
"Passed"
No output
Error
Which conditional statement allows multiple branches but executes only one true condition?
if()
if() else
if() elseif()
while()
What is the purpose of the else statement in PHP?
To execute code when the if condition is true
To execute code when the if condition is false
To repeat a block of code
To define a function
What will be the value of $remark if $grade = 60? if($grade >= 90) { $remark = "Excellent"; } elseif($grade >= 75) { $remark = "Good"; } else { $remark = "Needs Improvement"; }
"Excellent"
"Good"
"Needs Improvement"
No output
Which of the following is the correct syntax for an if() else statement?
if(condition) { code; } else(condition) { code; }
if(condition) { code; } else { code; }
if(condition) code; else code
if(condition) code; else(condition) code;
What is the output of the following code if $num = 5? if($num % 2 == 0) { echo "Even"; } else { echo "Odd"; }
"Even"
"Odd"
No output
Error
Which logical operator is used to combine multiple conditions where both must be true?
|| (OR)
&& (AND)
! (NOT)
== (Equal)
What will the following code output if $age = 18? if($age >= 18) { echo "Adult"; } else { echo "Minor"; }
"Adult"
"Minor"
No output
Error
Which statement is used to handle multiple possible conditions without multiple if() elseif() blocks?
for()
while()
switch
foreach()
What will be the output of the following code if $x = 10 and $y = 5? if($x > 5 && $y < 10) { echo "Condition 1"; } elseif($x == 10 || $y == 3) { echo "Condition 2"; } else { echo "Condition 3"; }
"Condition 1"
"Condition 2"
"Condition 3"
No output
How can you rewrite the following code using a switch statement? $day = "Monday"; if($day == "Monday") { echo "Start of the week"; } elseif($day == "Friday") { echo "End of the week"; } else { echo "Midweek"; }
switch($day) { case "Monday": echo "Start of the week"; break; case "Friday": echo "End of the week"; break; default: echo "Midweek"; }
switch($day) { case "Monday": echo "Start of the week"; case "Friday": echo "End of the week"; default: echo "Midweek"; }
switch($day) { "Monday": echo "Start of the week"; "Friday": echo "End of the week"; else: echo "Midweek"; }
switch($day) { if("Monday") echo "Start of the week"; elseif("Friday") echo "End of the week"; else echo "Midweek"; }
What is the most efficient way to check if a number is between 10 and 20 (inclusive)?
if($num > 10 && $num < 20)
if($num >= 10 || $num <= 20)
if($num >= 10 && $num <= 20)
if($num > 10 || $num < 20)
Which of the following is not a type of loop in PHP?
a) while()
b) for()
c) switch
d) do while()
What is the purpose of the break statement in a loop?
a) To skip the current iteration
b) To exit the loop immediately
c) To restart the loop
d) To pause the loop
Which loop executes a block of code at least once, even if the condition is false?
a) for()
b) while()
c) do while()
d) foreach()
What is the correct syntax for a for loop in PHP?
for(initialization; condition; increment) { //code }
for(condition; initialization; increment) { //code }
for(increment; condition; initialization) { //code }
loop for(initialization; condition; increment) { //code }
Which loop is best suited for iterating through arrays?
a) while()
b) for()
c) foreach()
d) do while()
What will be the output of the following code? for($i = 1; $i <= 3; $i++) { echo $i . " "; }
a) 1 2 3
b) 1 2
c) 3 2 1
d) 1 1 1
How many times will the following loop execute? $x = 5; while($x > 0) { $x--; }
0
5
6
Infinite
What will the following code output? $i = 0; do { echo $i; $i++; } while($i < 1);
0
1
01
No output
Which loop structure is used in the following code? $colors = ["Red", "Green", "Blue"]; foreach($colors as $color) { echo $color; }
for()
while()
foreach()
do while()
What will happen if the condition in a while() loop is always true?
The loop will execute once
The loop will not execute
The loop will run infinitely
The loop will throw an error
What will be the output of the following nested loop? for($i = 1; $i <= 2; $i++) { for($j = 1; $j <= 2; $j++) { echo $i . "-" . $j . " "; } }
1-1 1-2 2-1 2-2
1-2 2-1
1-1 2-2
1-1 1-2
How can you modify the following loop to print only even numbers between 1 and 10? for($i = 1; $i <= 10; $i++) { // Missing condition echo $i; }
if($i % 2 == 0)
if($i % 2 != 0)
if($i > 5)
if($i < 5)
What will the following code output? $num = 10; while($num > 0) { if($num == 5) { break; } echo $num-- . " "; }
10 9 8 7 6 5
10 9 8 7 6
9 8 7 6 5
10 9 8 7 6 5 4 3 2 1
What is the correct way to create an indexed array in PHP?
a) $array = array("A", "B", "C");
b) $array = ("A", "B", "C");
c) $array = new Array("A", "B", "C");
d) $array = "A", "B", "C";
Which function is used to count the number of elements in an array?
length()
count()
size()
sizeof()
What is the index of the first element in a PHP array?
1
0
-1
It can be any number
Which of the following is an associative array?
$array = array(1, 2, 3);
$array = array("A" => 1, "B" => 2);
$array = [1, 2, 3];
$array = array(0 => "A", 1 => "B");
What does the print_r() function do?
Sorts an array
Displays the structure of an array
Removes duplicate values from an array
Merges two arrays
What will be the output of the following code? $fruits = array("Apple", "Mango", "Grapes"); echo $fruits[1];
Apple
Mango
Grapes
Error
How do you add "Banana" to the end of the following array? $fruits = array("Apple", "Mango");
$fruits[] = "Banana";
$fruits[2] = "Banana";
$fruits->add("Banana");
array_add($fruits, "Banana");
What will the following code output? $numbers = array(5, 3, 8, 1); sort($numbers); print_r($numbers);
Array ( [0] => 1 [1] => 3 [2] => 5 [3] => 8 )
Array ( [0] => 5 [1] => 3 [2] => 8 [3] => 1 )
Array ( [0] => 8 [1] => 5 [2] => 3 [3] => 1 )
1, 3, 5, 8
Which loop is best for iterating through an associative array?
for()
while()
foreach()
do while()
What will be the result of the following code? $data = array("Name" => "John", "Age" => 25); echo $data["Age"];
Name
John
Age
25
How can you access the value "Grapes" in the following multidimensional array? $fruits = array( "Sweet" => array("Apple", "Mango"), "Sour" => array("Grapes", "Lemon") );
$fruits[0][1]
$fruits["Sour"][0]
$fruits["Sweet"][1]
$fruits[1][0]
What will be the output of the following code if $a = 3 and $b = 7? if($a < 5 || $b > 10) { echo "Condition A"; } elseif($a == 3 && $b == 7) { echo "Condition B"; } else { echo "Condition C"; }
"Condition C"
No output
"Condition B"
"Condition A"
Which of the following is a valid way to declare a function in PHP?
c) function: myFunction() {}
d) myFunction() function {};
b) declare function myFunction() {}
a) function myFunction() {}
