How to "properly" handle $_GET variables in PHP?

get, php, validation, variables

Solution

I would use filter_input with the FILTER_SANITIZE_NUMBER_FLOAT filter for mid. Something like that :

$mid = filter_input(INPUT_GET, 'mid', FILTER_SANITIZE_NUMBER_FLOAT);
$op = filter_input(INPUT_GET, 'op');
if($mid > 0){
  switch($op){
    case 'info':
      // Some code
      break;
    case 'cast':
      // Some more code
      break;
    default:
      break;
  }
}

Problem

Currently, I have the following code: ``` if(isset($_GET['mid']) && !empty($_GET['mid'])) { $mid = $_GET['mid']; if(is_numeric($mid) && $mid > 0) { if(isset($_GET['op']) && !empty($_GET['op'])) { $op = $_GET['op']; if($op == 'info') { } if($op == 'cast') { } } } } ``` But I think it's too "complex" with if statements inside if statements, etc... Would you handle it differently? How would you make it simpler?

Original source