Bird
Raised Fist0
PHPprogramming~20 mins

Form handling execution flow in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Form Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this PHP form handling code?

Consider this PHP script that processes a form submission. What will it output when the form is submitted with name=John?

PHP
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = $_POST['name'] ?? 'Guest';
    echo "Hello, $name!";
} else {
    echo "Please submit the form.";
}
?>
AUndefined index: name
BHello, John!
CHello, Guest!
DPlease submit the form.
Attempts:
2 left
💡 Hint

Check the request method and how the $_POST array is used.

❓ Predict Output
intermediate
2:00remaining
What happens if the form is submitted with GET method?

Given this PHP code snippet, what will be the output if the form is submitted using the GET method with name=Alice?

PHP
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    echo "Hello, " . htmlspecialchars($_POST['name']) . "!";
} else {
    echo "Form not submitted via POST.";
}
?>
AForm not submitted via POST.
BHello, Alice!
CUndefined index: name
DHello, !
Attempts:
2 left
💡 Hint

Look at the condition checking the request method.

🔧 Debug
advanced
2:30remaining
Why does this PHP form handler show an error when no input is given?

Examine this PHP code snippet. When the form is submitted without the 'email' field, it shows an error. What causes this error?

PHP
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = $_POST['email'];
    echo "Email: $email";
}
?>
AThe variable $email is not declared before use.
BThe form method is incorrect, it should be GET.
CThe echo statement syntax is invalid.
DAccessing <code>$_POST['email']</code> without checking if it exists causes an undefined index error.
Attempts:
2 left
💡 Hint

Think about what happens if a key is missing in the $_POST array.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this PHP form processing code

What is the syntax error in this PHP snippet that handles form data?

PHP
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = $_POST['username'];
    echo "User: $username";
} else {
    echo "No data received."
}
?>
AUsing single quotes instead of double quotes in echo.
BIncorrect comparison operator in the if condition.
CMissing semicolon after the echo statement in the else block.
DVariable $username is not initialized.
Attempts:
2 left
💡 Hint

Check the end of each statement carefully.

🚀 Application
expert
3:00remaining
How many items are in the $_POST array after submitting this form?

Given this HTML form and PHP script, how many key-value pairs will $_POST contain after submission?

HTML form:

<form method="POST" action="submit.php">
  <input type="text" name="first_name" value="Anna" />
  <input type="text" name="last_name" value="Smith" />
  <input type="checkbox" name="subscribe" value="yes" checked />
  <input type="checkbox" name="subscribe" value="no" />
  <input type="submit" value="Send" />
</form>

PHP snippet (submit.php):

<?php
print_r($_POST);
?>
A3
B2
C1
D4
Attempts:
2 left
💡 Hint

Remember how checkboxes with the same name behave in form submission.

Practice

(1/5)
1. What is the main purpose of checking $_SERVER['REQUEST_METHOD'] == 'POST' in PHP form handling?
easy
A. To display the form to the user
B. To ensure the form data is processed only when the form is submitted
C. To reset the form fields
D. To connect to the database

Solution

  1. Step 1: Understand the role of $_SERVER['REQUEST_METHOD']

    This variable tells us how the page was requested, for example, via GET or POST.
  2. Step 2: Check why we compare it to 'POST'

    When the form is submitted, it usually sends data via POST. Checking this ensures we only process data after submission.
  3. Final Answer:

    To ensure the form data is processed only when the form is submitted -> Option B
  4. Quick Check:

    Request method check = process form only after submit [OK]
Hint: Process form only if request method is POST [OK]
Common Mistakes:
  • Processing form data without checking request method
  • Confusing GET and POST methods
  • Trying to process form before submission
2. Which of the following is the correct way to start processing form data only after submission in PHP?
easy
A. if ($_SERVER['REQUEST_METHOD'] == 'POST') { /* process form */ }
B. if ($_SERVER['REQUEST_METHOD'] = 'POST') { /* process form */ }
C. if ($_POST) { /* process form */ }
D. if ($_GET['submit']) { /* process form */ }

Solution

  1. Step 1: Identify correct comparison operator

    if ($_SERVER['REQUEST_METHOD'] = 'POST') { /* process form */ } uses a single equals sign (=) which assigns, not compares. This is incorrect.
  2. Step 2: Check proper request method check

    if ($_SERVER['REQUEST_METHOD'] == 'POST') { /* process form */ } correctly uses double equals (==) to compare $_SERVER['REQUEST_METHOD'] to 'POST'.
  3. Final Answer:

    if ($_SERVER['REQUEST_METHOD'] == 'POST') { /* process form */ } -> Option A
  4. Quick Check:

    Use '==' to compare request method [OK]
Hint: Use '==' to compare request method, not '=' [OK]
Common Mistakes:
  • Using '=' instead of '==' for comparison
  • Checking $_POST without verifying request method
  • Using $_GET when form uses POST
3. What will be the output of this PHP code when the form is submitted with name=John?
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = $_POST['name'] ?? 'Guest';
    echo "Hello, $name!";
} else {
    echo "Please submit the form.";
}
?>
medium
A. Undefined index error
B. Please submit the form.
C. Hello, Guest!
D. Hello, John!

Solution

  1. Step 1: Check request method condition

    If the form is submitted, the request method is POST, so the if block runs.
  2. Step 2: Retrieve and display the name

    The code uses $_POST['name'] ?? 'Guest' which means if 'name' exists, use it; else use 'Guest'. Since 'name=John' is submitted, it uses 'John'.
  3. Final Answer:

    Hello, John! -> Option D
  4. Quick Check:

    POST with name=John outputs Hello, John! [OK]
Hint: Check if POST data exists, then use it or default [OK]
Common Mistakes:
  • Not checking request method before accessing $_POST
  • Expecting output without form submission
  • Forgetting null coalescing operator ??
4. Identify the error in this PHP form handling snippet:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $email = $_POST['email'];
}
echo "Email: $email";
?>
medium
A. Echoing variable outside the if block without initialization
B. Using $_POST without checking if 'email' exists
C. Missing semicolon after echo statement
D. Incorrect request method check

Solution

  1. Step 1: Analyze variable scope and initialization

    The variable $email is assigned only inside the if block, so if the request method is not POST, it remains undefined.
  2. Step 2: Check usage of variable outside if block

    Echoing $email outside the if block without initialization causes a notice or error if the form is not submitted.
  3. Final Answer:

    Echoing variable outside the if block without initialization -> Option A
  4. Quick Check:

    Use variable only after initialization [OK]
Hint: Initialize variables before use outside condition [OK]
Common Mistakes:
  • Assuming variables inside if exist outside
  • Not initializing variables before echo
  • Ignoring request method check
5. You want to keep the user's input visible in the form field after submission to improve user experience. Which PHP code snippet correctly achieves this for a text input named username?
hard
A. <input type='text' name='username' value='<?php echo $_POST['username']; ?>'>
B. <input type='text' name='username' value='<?php echo $_GET['username']; ?>'>
C. <input type='text' name='username' value='<?php echo htmlspecialchars($_POST['username'] ?? ''); ?>'>
D. <input type='text' name='username' value='<?php echo $_POST['username'] ?? 'Guest'; ?>'>

Solution

  1. Step 1: Ensure safe output in HTML attribute

    Using htmlspecialchars() prevents HTML injection and ensures special characters display correctly.
  2. Step 2: Use null coalescing operator to avoid undefined index

    $_POST['username'] ?? '' ensures the value is empty string if not set, avoiding errors.
  3. Final Answer:

    <input type='text' name='username' value='<?php echo htmlspecialchars($_POST['username'] ?? ''); ?>'> -> Option C
  4. Quick Check:

    Use htmlspecialchars and ?? for safe, persistent input [OK]
Hint: Use htmlspecialchars and ?? to keep input safe and visible [OK]
Common Mistakes:
  • Not using htmlspecialchars causing XSS risk
  • Not handling unset $_POST causing errors
  • Using $_GET when form uses POST
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard