r/PHPhelp • • Aug 18 '25

How to read querystring values in PHP8?

I'm used to PHP7, so this is a construct I'm using a lot:

$id     = intval($_GET["id"]);
$delete = intval($_GET["delete"]);
$csv    = intval($_GET["csv"]);
$absent = intval($_GET["absent"]);

After upgrading to PHP8, this gives me "Undefined array key" errors. So I changed the above to

$id     = 0;
$delete = 0;
$csv    = 0;
$absent = 0;

if( isset($_GET["id"]) )     { $id         = intval($_GET["id"]);     }
if( isset($_GET["delete"]) ) { $delete     = intval($_GET["delete"]); }
if( isset($_GET["csv"]) )    { $csv        = intval($_GET["csv"]);    }
if( isset($_GET["absent"]) ) { $absent     = intval($_GET["absent"]); }

And that is insanely more convoluted IMHO and will require countless hours to redo over my entire application. Can this not be done in a briefer manner?

3 Upvotes

34 comments sorted by

View all comments

Show parent comment

1

u/colshrapnel Aug 18 '25

undefined array accesses lead to errors, not null as in 7.x.

This statement is not 100% correct. The behavior is almost equal actually: in both versions it leads to null and raises an error. Only in 7.4 it's Notice level and above it's Warning level. OP just had Notice level errors silenced on 7.4. That's why it's always a good idea to have error_reporting=E_ALL all the time.