wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

PHP Conditional Statements Quiz

Total questions: 40

Worksheet time: 20mins

Name
Class
Date
1.

What is the simplest form of a conditional statement in PHP?

a)

if() else

b)

if()

c)

switch

d)

elseif()

2.

Which operator is used to compare values in a PHP conditional statement?

a)

Arithmetic operator

b)

Comparison operator

c)

Logical operator

d)

Assignment operator

3.

What will be the output of the following code if $grade = 80? if($grade < 75) { $remark = "Failed"; } else { $remark = "Passed"; }

a)

"Failed"

b)

"Passed"

c)

No output

d)

Error

4.

Which conditional statement allows multiple branches but executes only one true condition?

a)

if()

b)

if() else

c)

if() elseif()

d)

while()

5.

What is the purpose of the else statement in PHP?

a)

To execute code when the if condition is true

b)

To execute code when the if condition is false

c)

To repeat a block of code

d)

To define a function

6.

What will be the value of $remark if $grade = 60? if($grade >= 90) { $remark = "Excellent"; } elseif($grade >= 75) { $remark = "Good"; } else { $remark = "Needs Improvement"; }

a)

"Excellent"

b)

"Good"

c)

"Needs Improvement"

d)

No output

7.

Which of the following is the correct syntax for an if() else statement?

a)

if(condition) { code; } else(condition) { code; }

b)

if(condition) { code; } else { code; }

c)

if(condition) code; else code

d)

if(condition) code; else(condition) code;

8.

What is the output of the following code if $num = 5? if($num % 2 == 0) { echo "Even"; } else { echo "Odd"; }

a)

"Even"

b)

"Odd"

c)

No output

d)

Error

9.

Which logical operator is used to combine multiple conditions where both must be true?

a)

|| (OR)

b)

&& (AND)

c)

! (NOT)

d)

== (Equal)

10.

What will the following code output if $age = 18? if($age >= 18) { echo "Adult"; } else { echo "Minor"; }

a)

"Adult"

b)

"Minor"

c)

No output

d)

Error

11.

Which statement is used to handle multiple possible conditions without multiple if() elseif() blocks?

a)

for()

b)

while()

c)

switch

d)

foreach()

12.

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"; }

a)

"Condition 1"

b)

"Condition 2"

c)

"Condition 3"

d)

No output

13.

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"; }

a)

switch($day) { case "Monday": echo "Start of the week"; break; case "Friday": echo "End of the week"; break; default: echo "Midweek"; }

b)

switch($day) { case "Monday": echo "Start of the week"; case "Friday": echo "End of the week"; default: echo "Midweek"; }

c)

switch($day) { "Monday": echo "Start of the week"; "Friday": echo "End of the week"; else: echo "Midweek"; }

d)

switch($day) { if("Monday") echo "Start of the week"; elseif("Friday") echo "End of the week"; else echo "Midweek"; }

14.

What is the most efficient way to check if a number is between 10 and 20 (inclusive)?

a)

if($num > 10 && $num < 20)

b)

if($num >= 10 || $num <= 20)

c)

if($num >= 10 && $num <= 20)

d)

if($num > 10 || $num < 20)

15.

Which of the following is not a type of loop in PHP?

a)

a) while()

b)

b) for()

c)

c) switch

d)

d) do while()

16.

What is the purpose of the break statement in a loop?

a)

a) To skip the current iteration

b)

b) To exit the loop immediately

c)

c) To restart the loop

d)

d) To pause the loop

17.

Which loop executes a block of code at least once, even if the condition is false?

a)

a) for()

b)

b) while()

c)

c) do while()

d)

d) foreach()

18.

What is the correct syntax for a for loop in PHP?

a)

for(initialization; condition; increment) { //code }

b)

for(condition; initialization; increment) { //code }

c)

for(increment; condition; initialization) { //code }

d)

loop for(initialization; condition; increment) { //code }

19.

Which loop is best suited for iterating through arrays?

a)

a) while()

b)

b) for()

c)

c) foreach()

d)

d) do while()

20.

What will be the output of the following code? for($i = 1; $i <= 3; $i++) { echo $i . " "; }

a)

a) 1 2 3

b)

b) 1 2

c)

c) 3 2 1

d)

d) 1 1 1

21.

How many times will the following loop execute? $x = 5; while($x > 0) { $x--; }

a)

0

b)

5

c)

6

d)

Infinite

22.

What will the following code output? $i = 0; do { echo $i; $i++; } while($i < 1);

a)

0

b)

1

c)

01

d)

No output

23.

Which loop structure is used in the following code? $colors = ["Red", "Green", "Blue"]; foreach($colors as $color) { echo $color; }

a)

for()

b)

while()

c)

foreach()

d)

do while()

24.

What will happen if the condition in a while() loop is always true?

a)

The loop will execute once

b)

The loop will not execute

c)

The loop will run infinitely

d)

The loop will throw an error

25.

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 . " "; } }

a)

1-1 1-2 2-1 2-2

b)

1-2 2-1

c)

1-1 2-2

d)

1-1 1-2

26.

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; }

a)

if($i % 2 == 0)

b)

if($i % 2 != 0)

c)

if($i > 5)

d)

if($i < 5)

27.

What will the following code output? $num = 10; while($num > 0) { if($num == 5) { break; } echo $num-- . " "; }

a)

10 9 8 7 6 5

b)

10 9 8 7 6

c)

9 8 7 6 5

d)

10 9 8 7 6 5 4 3 2 1

28.

What is the correct way to create an indexed array in PHP?

a)

a) $array = array("A", "B", "C");

b)

b) $array = ("A", "B", "C");

c)

c) $array = new Array("A", "B", "C");

d)

d) $array = "A", "B", "C";

29.

Which function is used to count the number of elements in an array?

a)

length()

b)

count()

c)

size()

d)

sizeof()

30.

What is the index of the first element in a PHP array?

a)

1

b)

0

c)

-1

d)

It can be any number

31.

Which of the following is an associative array?

a)

$array = array(1, 2, 3);

b)

$array = array("A" => 1, "B" => 2);

c)

$array = [1, 2, 3];

d)

$array = array(0 => "A", 1 => "B");

32.

What does the print_r() function do?

a)

Sorts an array

b)

Displays the structure of an array

c)

Removes duplicate values from an array

d)

Merges two arrays

33.

What will be the output of the following code? $fruits = array("Apple", "Mango", "Grapes"); echo $fruits[1];

a)

Apple

b)

Mango

c)

Grapes

d)

Error

34.

How do you add "Banana" to the end of the following array? $fruits = array("Apple", "Mango");

a)

$fruits[] = "Banana";

b)

$fruits[2] = "Banana";

c)

$fruits->add("Banana");

d)

array_add($fruits, "Banana");

35.

What will the following code output? $numbers = array(5, 3, 8, 1); sort($numbers); print_r($numbers);

a)

Array ( [0] => 1 [1] => 3 [2] => 5 [3] => 8 )

b)

Array ( [0] => 5 [1] => 3 [2] => 8 [3] => 1 )

c)

Array ( [0] => 8 [1] => 5 [2] => 3 [3] => 1 )

d)

1, 3, 5, 8

36.

Which loop is best for iterating through an associative array?

a)

for()

b)

while()

c)

foreach()

d)

do while()

37.

What will be the result of the following code? $data = array("Name" => "John", "Age" => 25); echo $data["Age"];

a)

Name

b)

John

c)

Age

d)

25

38.

How can you access the value "Grapes" in the following multidimensional array? $fruits = array( "Sweet" => array("Apple", "Mango"), "Sour" => array("Grapes", "Lemon") );

a)

$fruits[0][1]

b)

$fruits["Sour"][0]

c)

$fruits["Sweet"][1]

d)

$fruits[1][0]

39.

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"; }

a)

"Condition C"

b)

No output

c)

"Condition B"

d)

"Condition A"

40.

Which of the following is a valid way to declare a function in PHP?

a)

c) function: myFunction() {}

b)

d) myFunction() function {};

c)

b) declare function myFunction() {}

d)

a) function myFunction() {}