What are best practices for handling undefined variables and offsets in PHP, especially when dealing with user input like $_GET parameters?

When dealing with user input like $_GET parameters in PHP, it is important to handle undefined variables and offsets to prevent errors and vulnerabilities. One best practice is to use isset() or empty() functions to check if a variable is set before accessing it. Additionally, using the null coalescing operator (??) can provide a default value if the variable is undefined.

// Using isset() to check if a variable is set before accessing it
$variable = isset($_GET['param']) ? $_GET['param'] : '';

// Using the null coalescing operator to provide a default value
$variable = $_GET['param'] ?? '';