PHP in Practice
Uploaded :
3 months ago
Updated :
3 months ago
beginner
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
actionto 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 input | Trust $_POST blindly |
| Use prepared statements for SQL | Concatenate user data into SQL |
Use the ?? null-coalescing operator | Assume keys always exist |
| Show generic errors to users | Leak 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.
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.