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 PHP tag?

A

<?php ?>

B

<php>

C

<? ?>

D

<% %>

Q2

Q2 PHP can be embedded in which of the following types of documents?

A

HTML only

B

CSS only

C

JavaScript only

D

HTML, CSS, and JavaScript

Q3

Q3 In PHP, which character is used at the end of each statement?

A

:

B

;

C

,

D

.

Q4

Q4 How do you write comments in PHP?

A

Using // for single line and /* */ for multiple lines

B

Using #

C

Using

D

Using **

Q5

Q5 What is the correct way to declare a variable in PHP?

A

int $varName

B

$varName

C

var $varName

D

declare $varName

Q6

Q6 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

Q7

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

A

6

B

123

C

Error

D

1

Q8

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

A

String

B

Array

C

Object

D

Character

Q9

Q9 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

Q10

Q10 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)

Q11

Q11 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

Q12

Q12 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

Q13

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

A

GET

B

POST

C

PUT

D

DELETE

Q14

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

A

$_GET

B

$_POST

C

$_SERVER

D

$_FILES

Q15

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

A

<group>

B

<fieldset>

C

<section>

D

<div>

Q16

Q16 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

Q17

Q17 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

Q18

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

A

value

B

type

C

name

D

placeholder

Q19

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

A

$_GET

B

$_POST

C

$_REQUEST

D

$_SERVER

Q20

Q20 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

Q21

Q21 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'])

Q22

Q22 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'

Q23

Q23 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

Q24

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

A

mysql_connect()

B

mysqli_connect()

C

connect_db()

D

db_connect()

Q25

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

Q26

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

Q27

Q27 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

Q28

Q28 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

Q29

Q29 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

Q30

Q30 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

...
ad verticalad vertical
ad