Skip to content
PHP Logo

What is the difference between using $action and $_GET['action'] in PHP when working with the switch function?

When working with the switch function in PHP, using $action directly can lead to potential security vulnerabilities like injection attacks. It is recommended to use $_GET['action'] instead to retrieve the action parameter from the URL. This helps sanitize the input and prevent malicious code execution.

php
$action = isset($_GET['action']) ? $_GET['action'] : 'default';

switch($action) {
    case 'action1':
        // Handle action 1
        break;
    case 'action2':
        // Handle action 2
        break;
    default:
        // Default action
        break;
}