What is the best practice for accessing variables in a URL and using them in conditional statements in PHP?

When accessing variables in a URL and using them in conditional statements in PHP, it is important to sanitize and validate the input to prevent security vulnerabilities such as SQL injection or cross-site scripting attacks. One way to do this is by using the $_GET superglobal array to retrieve the variables from the URL and then using conditional statements to check and process the data accordingly.

// Retrieve the variable from the URL and sanitize it
$variable = isset($_GET['variable']) ? htmlspecialchars($_GET['variable']) : '';

// Check if the variable meets certain conditions
if ($variable == 'condition1') {
    // Perform actions if the variable meets condition1
    echo 'Variable meets condition1';
} elseif ($variable == 'condition2') {
    // Perform actions if the variable meets condition2
    echo 'Variable meets condition2';
} else {
    // Handle other cases
    echo 'Variable does not meet any conditions';
}