an HCL GUVI product

php programming banner

PHP Multiple Choice Questions (MCQs) and Answers

Master PHP with Practice MCQs. Explore our curated collection of Multiple Choice Questions. Ideal for placement and interview preparation, our questions range from basic to advanced, ensuring comprehensive coverage of PHP. Begin your placement preparation journey now!

Q31

Q31 In PHP, the switch statement is used as an alternative to which other control structure?

A

if

B

while

C

for

D

foreach

Q32

Q32 What is the purpose of the break statement in PHP loops?

A

To pause the loop execution

B

To exit the loop

C

To skip the current iteration

D

To continue loop execution

Q33

Q33 In PHP, what will happen if you forget to increment the counter in a while loop?

A

The loop will run indefinitely

B

The loop will exit immediately

C

The loop will throw an error

D

The loop will skip iterations

Q34

Q34 Which looping structure is best for iterating over an associative array in PHP?

A

for

B

foreach

C

while

D

do-while

Q35

Q35 What will be the output of the following PHP code?
$i = 0;
while($i < 3) {
echo $i;
$i++;
}
?>

A

012

B

123

C

210

D

An error

Q36

Q36 Consider the following PHP code:
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) {
continue;
}
echo $i;
}
?>
What will be the output?

A

1245

B

12345

C

1234

D

Error

Q37

Q37 Identify the error in this PHP foreach loop:
foreach($array as $value) {
echo $value
}
?>

A

Missing semicolon after echo

B

Syntax error in foreach

C

Missing $ before array

D

No error in the code

Q38

Q38 Spot the mistake in this PHP while loop:
$i = 0;
while ($i <= 5) {
echo $i
}
$i++;
?>

A

Infinite loop

B

Missing increment inside loop

C

Missing semicolon after echo

D

Syntax error in while

Q39

Q39 Find the error in this PHP code involving a for loop:
echo $i;
}
?>

A

Syntax error in for loop

B

Missing semicolon after echo

C

No error in the code

D

Infinite loop

Q40

Q40 What is the keyword used to define a function in PHP?

A

function

B

def

C

create

D

func

Q41

Q41 What is a primary purpose of a function in PHP?

A

To repeat a block of code multiple times

B

To stop the execution of a script

C

To organize and reuse code

D

To declare variables

Q42

Q42 In PHP, what is a function parameter that is given a default value called?

A

Mandatory parameter

B

Optional parameter

C

Dynamic parameter

D

Static parameter

Q43

Q43 What is the scope of a variable declared inside a PHP function?

A

Global

B

Local

C

Static

D

Universal

Q44

Q44 Which of the following statements about PHP anonymous functions is true?

A

They must have a name

B

They cannot use external variables

C

They are a type of PHP class

D

They can be assigned to a variable or passed as an argument

Q45

Q45 What will be the output of this PHP function call?
function add($x, $y) {
return $x + $y;
}
echo add(2, 3);

A

5

B

23

C

Error

D

Nothing

Q46

Q46 Given the PHP function:
function isEven($num) {
return $num % 2 == 0;
}
What will isEven(5) return?

A

true

B

false

C

5

D

null

Q47

Q47 Consider the following PHP function:
function multiply(&$value) {
$value *= 2;
}
$num = 10;
multiply($num);
What will be the value of $num after the function call?

A

10

B

20

C

Error

D

Null

Q48

Q48 Identify the error in this PHP function definition:
function multiply($x, $y) {
return $x * $y return $x + $y;
}

A

Multiple return statements

B

Missing semicolon

C

Syntax error in return statements

D

No error in the code

Q49

Q49 Spot the mistake in this PHP function:
function getSum($arr) {
$sum = 0;
foreach($arr as $num) {
$sum += $num;
}
return $sum; } What will getSum([1, 2, 3]) return?

A

Nothing, there is a syntax error

B

6

C

Error due to incorrect parameter type

D

3

Q50

Q50 In PHP, which function is used to count the number of elements in an array?

A

count()

B

get_length()

C

size()

D

array_size()

Q51

Q51 What is the purpose of the explode() function in PHP?

A

To combine array elements into a string

B

To split a string into an array

C

To search for a string in an array

D

To sort an array

Q52

Q52 In PHP, which function is used to join array elements with a string?

A

join()

B

merge()

C

concat()

D

array_join()

Q53

Q53 Which of the following is a correct way to declare a multidimensional array in PHP?

A

$array = array(array(1, 2, 3), array(4, 5, 6));

B

$array = [array(1, 2, 3), array(4, 5, 6)];

C

$array = {(1, 2, 3), (4, 5, 6)};

D

$array = {array(1, 2, 3), array(4, 5, 6)};

Q54

Q54 What will be the output of the following PHP code?
$array = ['a', 'b', 'c']; echo $array[1];

A

a

B

b

C

c

D

Error

Q55

Q55 Consider this PHP code:
$str = "Hello World";
$array = str_split($str, 3);
print_r($array);
What does print_r($array); output?

A

Array ( [0] => Hel [1] => lo [2] => Wo [3] => rld )

B

Array ( [0] => Hello [1] => World )

C

Array ( [0] => H [1] => e [2] => l [3] => l [4] => o [5] => [6] => W [7] => o [8] => r [9] => l [10] => d )

D

Error

Q56

Q56 Identify the issue in this PHP code:
$str = 'Hello'; echo $str[10];

A

Trying to access an undefined index

B

Syntax error

C

No error, it will print 'Hello'

D

It will print a space

Q57

Q57 Find the error in the following PHP code:
$arr = array("one", "two", "three");
foreach ($ar as $val) {
echo $val;
}

A

Typo in the variable name inside foreach

B

Syntax error in array declaration

C

No error in the code

D

Error in echo statement

Q58

Q58 What keyword is used to declare a class in PHP?

A

class

B

object

C

new

D

struct

Q59

Q59 What is the purpose of the __construct() method in a PHP class?

A

To configure a server

B

To create an object from the class

C

To initialize an object's properties

D

To delete an object

Q60

Q60 In PHP, which keyword is used to inherit a class from another class?

A

extends

B

inherits

C

uses

D

implements