r/PHPhelp • u/oz1sej • 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
View all comments
Show parent comment
1
u/[deleted] Aug 19 '25
(int) is a language native, intval() is a function. There will be a minimal performance difference, it's very tiny so doesn't matter in most cases. "intval" is more for when you want to convert to something other than base-10.
In this case, $_GET["id"] is most likely a base 10 number, so (int) and intval should be the same result.