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 Consider the PHP code:
$stmt = $mysqli->prepare("SELECT * FROM users WHERE id = ?");
$stmt->bind_param("i", $id);
What does the "i" in bind_param() signify?

A

Integer data type

B

Incremental value

C

Input parameter

D

Invalid parameter

Q32

Q32 In a PHP script using MySQLi, what does the close() method do when applied to a mysqli object?

A

Closes the database connection

B

Saves changes to the database

C

Closes the PHP script

D

Resets all variables in the script

Q33

Q33 Identify the error in the following PHP code:
$mysqli = new mysqli("localhost", "user", "password", "database"); $result = $mysqli->query("SELECT * FROM users WHERE id = '$id'");

A

No error in the code

B

Missing database connection error handling

C

Improper string concatenation in the query

D

SQL injection vulnerability

Q34

Q34 Spot the error in this PHP database connection code:
$mysqli = new mysqli("localhost", "user", "password"); $mysqli->select_db("database");

A

Missing database in the constructor

B

Improper method to select a database

C

No error in the code

D

Syntax error in mysqli instantiation

Q35

Q35 Which PHP statement is used to execute the same code a specified number of times?

A

foreach

B

while

C

for

D

do-while

Q36

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

A

if

B

while

C

for

D

foreach

Q37

Q37 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

Q38

Q38 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

Q39

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

A

for

B

foreach

C

while

D

do-while

Q40

Q40 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

Q41

Q41 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

Q42

Q42 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

Q43

Q43 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

Q44

Q44 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

Q45

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

A

function

B

def

C

create

D

func

Q46

Q46 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

Q47

Q47 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

Q48

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

A

Global

B

Local

C

Static

D

Universal

Q49

Q49 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

Q50

Q50 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

Q51

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

A

true

B

false

C

5

D

null

Q52

Q52 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

Q53

Q53 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

Q54

Q54 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

Q55

Q55 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()

Q56

Q56 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

Q57

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

A

join()

B

merge()

C

concat()

D

array_join()

Q58

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

Q59

Q59 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

Q60

Q60 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

ad verticalad vertical
ad