| $_GET | $_POST | |
|---|---|---|
| Data location | URL query string | Request body (hidden) |
| URL example | page.php?name=Min | Not visible in URL |
| Size limit | ~2048 chars (URL) | Large (default 8MB) |
| Bookmark-able | ✅ Yes | ❌ No |
| Security | Less secure (visible) | More secure |
| Use for | Search, filters, pagination | Login, registration, sensitive |
<!-- HTML file: contact.php --> <!DOCTYPE html> <html> <body> <form action="contact.php" method="POST"> <input type="text" name="name" placeholder="Your name"> <input type="email" name="email" placeholder="Email"> <button type="submit" name="submit">Send</button> </form> <?php // Same file processes itself (self-referencing) if (isset($_POST['submit'])) { $name = htmlspecialchars($_POST['name'] ?? ''); $email = htmlspecialchars($_POST['email'] ?? ''); echo "Hello, $name! Email: $email"; } ?> </body> </html>
<?php $errors = []; $success = false; // Sanitize functions function clean($data) { return htmlspecialchars(trim($data), ENT_QUOTES, 'UTF-8'); } if (isset($_POST['submit'])) { $name = clean($_POST['name'] ?? ''); $email = clean($_POST['email'] ?? ''); $age = (int)($_POST['age'] ?? 0); // Validate if (empty($name)) $errors[] = "Name required"; elseif (strlen($name) < 2) $errors[] = "Name min 2 chars"; if (empty($email)) $errors[] = "Email required"; elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) $errors[] = "Invalid email format"; if ($age < 1 || $age > 120) $errors[] = "Valid age required"; if (empty($errors)) { $success = true; // Save to database after validation passes } } ?> <!-- HTML template --> <?php if ($success): ?> <p style="color:green">✅ Registration successful!</p> <?php else: ?> <?php foreach ($errors as $error): ?> <p style="color:red">❌ <?= e($error) ?></p> <?php endforeach; ?> <form method="POST"> <input name="name" value="<?= e($name ?? '') ?>"> <!-- Keep form values after error! --> </form> <?php endif; ?>
| Variable | Contents |
|---|---|
$_GET | URL query parameters |
$_POST | POST form data |
$_REQUEST | GET + POST + COOKIE combined |
$_FILES | Uploaded files |
$_SESSION | Session data |
$_COOKIE | Cookie data |
$_SERVER | Server info (IP, method, headers) |
$_ENV | Environment variables |
$GLOBALS | All global variables |
<?php // $_SERVER examples echo $_SERVER['REQUEST_METHOD']; // "GET" or "POST" echo $_SERVER['REMOTE_ADDR']; // User's IP address echo $_SERVER['PHP_SELF']; // Current script path echo $_SERVER['HTTP_USER_AGENT']; // Browser info // Check request method if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Handle POST } ?>
<!-- HTML --> <form method="POST" enctype="multipart/form-data"> <input type="file" name="photo" accept="image/*"> <button type="submit">Upload</button> </form> <?php if (isset($_FILES['photo'])) { $file = $_FILES['photo']; // $_FILES structure: // ['name'] → original filename // ['type'] → MIME type e.g. "image/jpeg" // ['size'] → size in bytes // ['tmp_name'] → temp path on server // ['error'] → 0 = no error if ($file['error'] === 0) { $allowed = ['image/jpeg', 'image/png', 'image/webp']; if (in_array($file['type'], $allowed) && $file['size'] < 2000000) { $dest = "uploads/" . basename($file['name']); move_uploaded_file($file['tmp_name'], $dest); echo "✅ Uploaded!"; } } } ?>
🎉 PHP Core 7 Lessons Complete!
Next: OOP Classes, Inheritance, Interfaces, Traits, Namespaces, Exceptions ဆက်သင်မယ်
← PHP 06 | PHP 08 → OOP Classes