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!

Q1

Q1 Which of the following is a correct way to define a constant in PHP?

A

define('CONST_NAME', 'Value');

B

const CONST_NAME = 'Value';

C

Both a and b

D

None of the above

Q2

Q2 What will be the output of the following PHP code?

A

6

B

123

C

Error

D

1

Q3

Q3 Which of the following data types is not supported in PHP?

A

String

B

Array

C

Object

D

Character

Q4

Q4 What will be the output of the following PHP code?
$x = "Hello";
$y = 'World';
echo $x . " " . $y;
?>

A

HelloWorld

B

Hello World

C

Hello+World

D

Error

Q5

Q5 Consider the PHP array:
$arr = array(1, 2, 3, 4);
What is the correct way to access the third element?

A

$arr[2]

B

$arr[3]

C

$arr[2]

D

$arr(3)

Q6

Q6 Identify the error in this PHP code snippet:

A

Missing semicolon

B

Syntax error in echo

C

Variable declaration error

D

No error in the code

Q7

Q7 Spot the error in the following PHP code:
for ($i = 0; $i <= 10; $i++) {
echo $i; } echo $i;
?>

A

Undefined variable outside loop

B

Missing semicolon

C

No error in the code

D

Syntax error in loop

Q8

Q8 What method should be used to send sensitive data in a form?

A

GET

B

POST

C

PUT

D

DELETE

Q9

Q9 Which superglobal array in PHP contains information about headers, paths, and script locations?

A

$_GET

B

$_POST

C

$_SERVER

D

$_FILES

Q10

Q10 In a form, to group multiple elements that belong together, which HTML tag is used?

A

<group>

B

<fieldset>

C

<section>

D

<div>

Q11

Q11 How do you access form data sent via the GET method in PHP?

A

Using the $_REQUEST superglobal

B

Using the $_GET superglobal

C

Using the $_POST superglobal

D

Using the $_SERVER superglobal

Q12

Q12 What is the correct way to check if a form has been submitted in PHP?

A

Checking if $_POST is set

B

Checking if $_GET is set

C

Checking if $_SERVER['REQUEST_METHOD'] is POST

D

Checking if $_REQUEST is set

Q13

Q13 Which attribute in a form input element specifies the field's initial value?

A

value

B

type

C

name

D

placeholder

Q14

Q14 Given a form with method="post", which PHP array will contain the form's submitted data?

A

$_GET

B

$_POST

C

$_REQUEST

D

$_SERVER

Q15

Q15 What will be the output of the following PHP code if a user submits a form with an input named email?
if (isset($_POST['email'])) {
echo $_POST['email'];
}
?>

A

The value of the email input field

B

NULL

C

An empty string

D

An error message

Q16

Q16 In PHP, how do you securely access a form value sent via POST to prevent XSS attacks?

A

Using htmlspecialchars($_POST['value'])

B

Using $_POST['value'] directly

C

Using strip_tags($_POST['value'])

D

Using mysqli_real_escape_string($_POST['value'])

Q17

Q17 Identify the issue in this PHP code for handling a form input:
if (isset($_GET['submit'])) {
echo $_POST['name'];
}
?>

A

Incorrect use of $_GET and $_POST

B

No issue

C

Syntax error

D

The input name should be 'submit' instead of 'name'

Q18

Q18 Spot the error in the following PHP form handling code:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo $_POST[name];
}
?>

A

Missing quotes around array key name

B

No error in the code

C

Syntax error in if statement

D

Missing semicolon in echo statement

Q19

Q19 Which PHP function is used to connect to a MySQL database?

A

mysql_connect()

B

mysqli_connect()

C

connect_db()

D

db_connect()

Q20

Q20 In PHP, which function is used to execute a SQL query against a MySQL database?

A

mysql_query()

B

mysqli_query()

C

execute_sql()

D

run_query()

Q21

Q21 When retrieving data from a MySQL database, which PHP function is used to fetch a row as an associative array?

A

mysqli_fetch_assoc()

B

mysqli_fetch_array()

C

mysqli_fetch_row()

D

mysqli_fetch_all()

Q22

Q22 What does the PHP mysqli_real_escape_string() function do?

A

Sanitizes input for use in a SQL query

B

Converts special characters to HTML entities

C

Encrypts a string

D

Splits a string into an array

Q23

Q23 Which of the following is an appropriate use of PHP Data Objects (PDO)?

A

Connecting to multiple database types

B

Creating HTML content

C

Parsing JSON data

D

Managing PHP sessions

Q24

Q24 What is the main advantage of using prepared statements in PHP for database queries?

A

Increased query execution speed

B

Reduced memory usage

C

Enhanced security against SQL injection attacks

D

Simplified syntax

Q25

Q25 What does the connect_errno property of a mysqli object indicate in PHP?

A

It indicates the error number of the last connection attempt

B

It returns the total number of failed connection attempts

C

It is a boolean value indicating whether the connection was successful

D

It stores the MySQL server version

Q26

Q26 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

Q27

Q27 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

Q28

Q28 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

Q29

Q29 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

Q30

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

A

foreach

B

while

C

for

D

do-while