PHP Forms MCQ Questions and Answers

1. How does PHP receive form data sent via the GET method?

a) $_GET array
b) $_POST array
c) $_REQUEST array
d) get_data()

Answer:

a) $_GET array

Explanation:

PHP receives data sent via the GET method through the global $_GET array.

2. What method should be used to send sensitive data, like passwords, in a PHP form?

a) GET
b) POST
c) Either GET or POST
d) SEND

Answer:

b) POST

Explanation:

The POST method should be used for sending sensitive data, as it does not expose data in the URL.

3. How do you retrieve a form input value in PHP if the method is POST?

a) $_POST['input_name']
b) $_GET['input_name']
c) $_REQUEST['input_name']
d) post_data('input_name')

Answer:

a) $_POST['input_name']

Explanation:

Form data sent via POST is accessed in PHP using the $_POST array with the name of the input.

4. What is the purpose of the htmlspecialchars function in form handling in PHP?

a) To enhance the appearance of the text
b) To prevent SQL injection
c) To prevent XSS attacks
d) To convert HTML into PHP code

Answer:

c) To prevent XSS attacks

Explanation:

htmlspecialchars() converts special characters to HTML entities, helping prevent XSS attacks.

5. Which superglobal array in PHP contains information about both GET and POST data?

a) $_SERVER
b) $_ENV
c) $_REQUEST
d) $_SESSION

Answer:

c) $_REQUEST

Explanation:

The $_REQUEST array contains data from both $_GET and $_POST as well as cookies.

6. How do you check if a form has been submitted in PHP?

a) Checking if $_SERVER['REQUEST_METHOD'] is set
b) Checking if $_GET or $_POST array is not empty
c) Using the isset() function on submit button
d) All of the above

Answer:

d) All of the above

Explanation:

Form submission can be checked using several methods, including checking the request method, if data arrays are not empty, or if the submit button is set.

7. What attribute must be included in an HTML form to upload files to a PHP script?

a) method="upload"
b) enctype="multipart/form-data"
c) type="file"
d) upload="true"

Answer:

b) enctype="multipart/form-data"

Explanation:

The enctype attribute must be set to "multipart/form-data" for uploading files through a form.

8. How are file uploads accessed in PHP?

a) $_FILE
b) $_FILES
c) $_UPLOAD
d) $_FILE_UPLOAD

Answer:

b) $_FILES

Explanation:

Uploaded files in PHP are accessed using the $_FILES superglobal array.

9. How do you access a checkbox value in PHP if it is checked?

a) Directly using $_POST['checkbox_name']
b) Using isset($_POST['checkbox_name'])
c) Checkboxes do not have values in PHP
d) Using get_checkbox('checkbox_name')

Answer:

b) Using isset($_POST['checkbox_name'])

Explanation:

To access a checkbox value, you need to check if it is set using isset() since unchecked boxes are not sent in the request.

10. Which method, GET or POST, appends form data to the URL?

a) GET
b) POST
c) Both
d) Neither

Answer:

a) GET

Explanation:

The GET method appends form data to the URL, while POST sends data in the request body.

11. How do you ensure a form field is not empty in PHP?

a) Using empty($_POST['field_name'])
b) Using isset($_POST['field_name']) and $_POST['field_name'] != ''
c) Checking the length of the $_POST['field_name']
d) Both a) and b)

Answer:

b) Using isset($_POST['field_name']) and $_POST['field_name'] != ''

Explanation:

To ensure a form field is not empty, check that it is set and its value is not an empty string.

12. Which PHP function is used to validate email addresses?

a) email_validate()
b) validate_email()
c) filter_var() with FILTER_VALIDATE_EMAIL
d) is_email()

Answer:

c) filter_var() with FILTER_VALIDATE_EMAIL

Explanation:

The filter_var() function with the FILTER_VALIDATE_EMAIL filter is used to validate email addresses.

13. How do you retain form field values after submission in PHP?

a) Using cookies
b) Using session variables
c) Echoing the form values in the value attribute
d) All of the above

Answer:

c) Echoing the form values in the value attribute

Explanation:

To retain form field values, echo the submitted values in the value attribute of the form fields.

14. What is the default form submission method in HTML?

a) GET
b) POST
c) REQUEST
d) PUT

Answer:

a) GET

Explanation:

If the method attribute is not specified in an HTML form, the default submission method is GET.

15. How do you access form data sent via both GET and POST methods in PHP without knowing the method used?

a) Using $_GLOBAL
b) Using $_REQUEST
c) Using $_FORM
d) It's not possible

Answer:

b) Using $_REQUEST

Explanation:

The $_REQUEST superglobal array can be used to access form data regardless of whether the GET or POST method was used.


Comments