PHP in Practice

Uploaded : 3 months ago Updated : 3 months ago beginner

Take The Quiz

Take The Quiz

Let’s build for real

Apply the fundamentals: you’ll handle a simple form submission — the bread and butter of PHP back ends.

Project: a contact form handler

  • Create an HTML form with method="post".
  • Point its action to a .php file.
  • In PHP, read the submitted fields from $_POST.
  • Validate them and show a confirmation.

The form (HTML)

<form method="post" action="submit.php">
  <input name="email" type="email">
  <button type="submit">Send</button>
</form>

The handler (submit.php)

<?php
$email = trim($_POST["email"] ?? "");

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
  echo "Thanks! We will reach you at $email";
} else {
  echo "Please enter a valid email.";
}

Always validate input

Never trust $_POST/$_GET data. Validate and sanitise everything — it is your first line of defence against attacks.

Good habits vs. common mistakes

Do ✓Don’t ✗
Validate every inputTrust $_POST blindly
Use prepared statements for SQLConcatenate user data into SQL
Use the ?? null-coalescing operatorAssume keys always exist
Show generic errors to usersLeak stack traces in production

SQL injection is the #1 risk

Never build SQL by concatenating user input. Use prepared statements (PDO/MySQLi) — frameworks like Laravel do this for you.

Talk is cheap. Show me the code.Linus Torvalds
What is $_POST?
A built-in array holding data submitted via an HTML form with method="post".
How do I connect to a database?
Use PDO with prepared statements, or an ORM like Laravel’s Eloquent.
What’s next after PHP?
Laravel — the most popular PHP framework, built on these foundations.

Keep going

You can now process forms on the server. Ready for a real framework?

Explore the Laravel track

Comments