What are the differences between $_POST and $_GET in PHP, and when should each be used?

$_POST and $_GET are both PHP superglobals used to collect form data submitted by the user. The main difference between them is that $_POST sends data to the server in the HTTP request body, while $_GET sends data in the URL. $_POST should be used when dealing with sensitive information like passwords, as the data is not visible in the URL. $_GET can be used for non-sensitive data or when you want to bookmark or share a URL that contains the data.

// Example of using $_POST to collect form data
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $username = $_POST["username"];
    $password = $_POST["password"];
    
    // Process the form data
}
```

```php
// Example of using $_GET to collect form data
if (isset($_GET["search"])) {
    $searchTerm = $_GET["search"];
    
    // Process the search term
}