Skip to main content
PHP for Beginners
CHAPTER 06 Beginner

PHP Input and Output

Updated: May 12, 2026
15 min read

# Chapter 6: PHP Input and Output

1. Introduction

Welcome to Chapter 6! A web application isn't very useful if it only displays static data. To make an application interactive, you need a way to receive information from the user (Input) and display responses back to them (Output). In this chapter, we will briefly revisit echo and print for output, and then dive into the most exciting part: capturing user input using HTML forms and processing it with PHP!

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Deepen your understanding of outputting data with echo and print.
  • Create basic HTML forms to capture user data.
  • Understand how data flows from the browser to the PHP server.
  • Capture form submissions using PHP.

3. Output Refresher: Echo and Print

As we saw earlier, echo and print are the backbone of PHP output.
  • echo is slightly faster and can take multiple parameters.
  • print returns a value of 1, meaning it can be used inside expressions.
php
12345
<?php
// Combining text and variables for output
$user = "Admin";
echo "<h2>Welcome back, " . $user . "!</h2>";
?>

4. User Input: The HTML Form Basics

Unlike desktop applications where you can constantly monitor the keyboard, web applications are stateless. To get input from a user, we use an HTML form. The user fills out the form, clicks submit, and the browser sends that data in a "request" to the PHP server.

A basic HTML form requires two crucial attributes on the <form> tag:

  1. 1. action: The PHP file that will process the data.
  1. 2. method: How the data is sent (usually GET or POST, which we will master in Chapter 13).

html
123456
<!-- Form Example -->
<form action="process.php" method="POST">
    <label for="username">Enter your name:</label>
    <input type="text" id="username" name="username">
    <button type="submit">Submit</button>
</form>

5. The name Attribute is King

In the HTML above, notice the name="username" attribute on the input field. This is the most important part of the form. When the form is submitted, PHP uses this exact name to identify and capture the data the user typed in.

6. Capturing Input in PHP

When the form is submitted to process.php using method="POST", PHP automatically stores the incoming data in a special built-in array called $_POST.
php
1234567
<?php
// process.php
// Capturing the 'username' from the form
$captured_name = $_POST[&#039;username'];

echo "Hello, " . $captured_name;
?>

7. Real-World Examples

Imagine a newsletter signup box at the bottom of a website.
html
12345
<!-- HTML Form -->
<form action="subscribe.php" method="POST">
    <input type="email" name="user_email" placeholder="Enter Email">
    <button type="submit">Subscribe</button>
</form>
php
12345
<?php
// subscribe.php
$email = $_POST[&#039;user_email'];
echo "Thank you for subscribing with: " . $email;
?>

8. Output Explanations

When the user types test@example.com and clicks subscribe, the browser sends a POST request to subscribe.php. PHP looks inside the $_POST array for the key user_email. It finds the email, stores it in the $email variable, and finally echoes out the thank you message.

9. Common Mistakes

  • Forgetting the name attribute: If your <input> has no name attribute, PHP will never receive the data, even if the user typed it in!
  • Mismatched Methods: If your HTML form uses method="GET", but your PHP tries to read from $_POST, it will fail. They must match.
  • Trying to access input before submission: If you try to echo $_POST['username'] when the user first loads the page (before they hit submit), PHP will throw a warning because the data doesn't exist yet.

10. Best Practices

  • Always add an id attribute for CSS/Labels, and a name attribute for PHP on every input field.
  • For sensitive data like passwords, always use method="POST".
  • We will learn how to validate and secure this input in upcoming chapters—never trust user input directly in production!

11. Exercises

  1. 1. Create an HTML file with a form that asks for a favorite color.
  1. 2. Ensure the input field has name="color".
  1. 3. Create a PHP file that captures that color and outputs: "Your favorite color is [color]".

12. Mini Project: User Greeting App

Task: Build a single-page app where the form submits to itself. It asks for a First Name and Last Name, and outputs a formatted greeting.
php
12345678910111213141516171819202122232425262728
<!DOCTYPE html>
<html>
<head><title>Greeting App</title></head>
<body>
    <h2>User Greeting App</h2>
    
    <!-- Submitting to the same page -->
    <form action="" method="POST">
        <input type="text" name="first_name" placeholder="First Name" required>
        <input type="text" name="last_name" placeholder="Last Name" required>
        <button type="submit">Greet Me</button>
    </form>

    <br>

    <?php
    // We check if the form was actually submitted to prevent errors on first load
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $first = $_POST[&#039;first_name'];
        $last = $_POST[&#039;last_name'];
        
        echo "<div style=&#039;color: green;'>";
        echo "<h3>Welcome, $first $last!</h3>";
        echo "</div>";
    }
    ?>
</body>
</html>

13. Coding Challenges

Challenge 1: Modify the User Greeting App above. Add a third input for "Age" (as a number). In the PHP output, echo out a message that says "Welcome [Name]. You are [Age] years old."
php
1234567891011
<!-- Solution Snippet -->
<input type="number" name="age" placeholder="Age" required>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $first = $_POST[&#039;first_name'];
    $last = $_POST[&#039;last_name'];
    $age = $_POST[&#039;age'];
    echo "<h3>Welcome, $first $last! You are $age years old.</h3>";
}
?>

14. MCQs with Answers

1. Which HTML attribute does PHP use to identify a specific input field? A) id B) class C) name D) type *Answer: C*

2. If a form uses method="POST", which PHP array will hold the submitted data? A) $_GET B) $_POST C) $_REQUEST D) $_FORM *Answer: B*

3. Why do we need HTML forms in PHP development? A) Because PHP cannot create its own buttons. B) Because web apps are stateless, and forms are the standard way browsers send user input to servers. C) Forms make the website load faster. D) Forms are required for database connections. *Answer: B*

15. Interview Questions

Q: What happens if a user submits a form without filling in a text input? *A:* Unless the HTML input has the required attribute, the browser will submit the form. PHP will receive the data, but the value associated with that input's name will be an empty string "".

Q: Why do we check $_SERVER["REQUEST_METHOD"] == "POST" in the mini-project? *A:* Because the PHP code is on the same page as the HTML form. When the user first visits the page via a standard URL link, it is a GET request, so the form hasn't been submitted yet. Checking the request method prevents PHP from trying to read $_POST data that doesn't exist yet, avoiding error messages.

16. FAQs

Q: Can I use GET instead of POST? *A:* Yes! GET appends the form data directly to the URL (e.g., process.php?name=John). It is great for search bars, but terrible for passwords or large data. We will cover this deeply in Chapter 13.

17. Summary

You now have the power of interactivity! You learned how to structure an HTML form to talk to your PHP backend. You understand that the HTML name attribute is the critical link between the frontend input and the backend $_POST array, allowing you to capture, process, and output user-submitted data.

18. Next Chapter Recommendation

Currently, our code just runs top to bottom blindly. But what if we only want to greet the user IF they are an admin? In Chapter 7: PHP Conditional Statements, we will learn how to make our scripts "think" and make decisions!

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·