Q.46 What is the keyword used to define a function in PHP?
A. function
B. def
C. create
D. func
Answer. A
Q.47 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
Answer. A
Q.48 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
Answer. B
Q.49 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
Answer. A
Q.50 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
Answer. A
Q.51 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
Answer. A
Q.52 Which looping structure is best for iterating over an associative array in PHP?
A. for
B. foreach
C. while
D. do-while
Answer. B
Q.53 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
Answer. A
Q.54 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
Answer. B
Q.55 In PHP, the switch statement is used as an alternative to which other control structure?
A. if
B. while
C. for
D. foreach
Answer. A
Q.56 Which PHP statement is used to execute the same code a specified number of times?
A. foreach
B. while
C. for
D. do-while
Answer. C
Q.57 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
Answer. A
Q.58 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
Answer. D
Q.59 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
Answer. A
Q.60 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
Answer. A