PHP GET & POST Methods
GET and POST are the two main HTTP methods for sending data to the server. Understanding when and how to use each is crucial for web development!
Output
Click Run to execute your code
GET vs POST
| Feature | GET | POST |
|---|---|---|
| Visibility | Visible in URL | Hidden in request body |
| Bookmarkable | Yes | No |
| Data Size | Limited (~2KB) | Large (MB+) |
| Security | Less secure | More secure |
| Use Case | Retrieving data | Submitting data |
GET Method
<?php
// URL: page.php?name=John&age=30
$name = $_GET['name'] ?? 'Guest';
$age = $_GET['age'] ?? 0;
echo "Hello, $name! You are $age years old.";
?>
POST Method
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = $_POST['username'] ?? '';
$password = $_POST['password'] ?? '';
// Process login
}
?>
Summary
- GET: URL parameters, bookmarkable
- POST: Form data, more secure
- Always sanitize: Use htmlspecialchars()
- Check method: $_SERVER['REQUEST_METHOD']
What's Next?
Next, learn about Form Validation - ensuring data is correct and safe!
Enjoying these tutorials?