What is the meaning of a 'required' field in PHP form handling?
A
A field that can be left empty
B
A field that must be filled before submitting the form
C
A field with a default value
Analysis & Theory
A required field must be filled out; otherwise, the form should not be processed.
Which function is commonly used in PHP to check if a required input is empty?
Analysis & Theory
The `empty()` function checks if the input is empty or not provided.
What is the correct way to check if a form input named 'email' is filled?
A
if($_POST['email'] != '')
B
if(!empty($_POST['email']))
C
if($_POST['email'] !== NULL)
Analysis & Theory
All these conditions can check whether 'email' is filled, but `!empty()` is commonly used.
Which of the following checks whether the 'name' input is not empty?
A
if(empty($_POST['name']))
B
if($_POST['name'] == '')
C
if(strlen($_POST['name']) == 0)
Analysis & Theory
All methods correctly check whether the 'name' input is empty.
If a required field is left empty, what should PHP typically do?
B
Store empty values in the database
C
Show an error message and stop processing
D
Redirect to another page
Analysis & Theory
PHP should show an error message and not process the form until required fields are filled.
What is the output of this code if the 'username' input is left empty?
```
<?php
if(empty($_POST['username'])) {
echo 'Username is required';
} else {
echo $_POST['username'];
}
?>
```
Analysis & Theory
If 'username' is empty, it prints 'Username is required'.
How do you prevent form processing when the 'email' field is empty?
A
Use `if(empty($_POST['email'])) { show error }`
B
Use `if(isset($_POST['email']))`
C
Use `if($_POST['email'] = '')`
D
PHP handles it automatically
Analysis & Theory
`empty()` checks if 'email' is empty and prevents processing if true.
Which is better for handling required fields in PHP?
A
Server-side validation only
B
Client-side validation only
C
Both client-side and server-side validation
Analysis & Theory
Both client-side and server-side validation are recommended for security and user experience.
If a form field is marked as 'required' in HTML, is PHP server-side validation still needed?
A
No, HTML handles it completely
B
Yes, because users can bypass HTML validation
D
Only if the field has a default value
Analysis & Theory
Server-side validation is necessary as HTML 'required' can be bypassed using developer tools.
What happens if `$_POST['email']` is accessed but the form was not submitted?
C
It returns an empty string
D
Undefined index warning unless checked with isset()
Analysis & Theory
PHP throws an 'Undefined index' warning if the key doesn't exist and it's not checked with `isset()`.