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
16
u/[deleted] Aug 18 '25
You should do some validation, but you can also do this:
4
u/equilni Aug 18 '25
You should have gotten the error in 7 too:
https://onlinephp.io?s=s7EvyCjg5eLlUslMUbBVyMwrKUvM0VCJd3cNiVbKTFGK1bQGAA%2C%2C&v=7.4.33
Then using
$id = $_GET['id'] ?? 0;works up to 8.3:https://onlinephp.io?s=s7EvyCjg5eLlUol3dw2JVs9MUY9VsFUoKSpNtQYLZ6YAuciS9vYKBmCp1OSMfIXMvJKyxBwNoDJNawA%2C&v=8.3.21%2C7.4.33
4
u/colshrapnel Aug 18 '25 edited Aug 18 '25
Your question is not really "How to read query string values in PHP8". But rather,
How to get rid of that annoying error message?
It's a fair question too, and you've got the answers already, but just in case, if you would be interested in how to read query string values in PHP8, here it goes:
In PHP8, we tend to validate input values, in the meaning of making sure that we are getting expected values and rejecting nonsense requests. For example, most of time
$_GET["id"]should be a positive number, while zero or negative number won't do, let alone strings or arrays. So it has to be checked and entire request outright rejected if we get a malformedid. The same goes for all other values, such as dates, emails, strings (that could be checked for length) etc.There are many libraries that can do such validation in a very concise call, like
Now, you either have the
$requestvariable with all values properly set, or the whole request rejected.Note that we also prefer to have all input values in a single array/object, instead of separate variables.
3
u/Moceannl Aug 18 '25
In most frameworks you would have a helper function or you can create one:
$id = getvar('id');
The function you can make as complicated as you want, for example checking the type:
$id = getvar('id','int');
Or I don't know what else. But you clearly need an abstraction layer.
For example Symfony has: $request->get('id') for you, Laravel has
2
u/cursingcucumber Aug 18 '25
You can use the Symfony HTTP foundation component for example. As Symfony is modular, you don't need the whole framework but you can use components as well.
https://symfony.com/doc/current/components/http_foundation.html
1
u/p1ctus_ Aug 18 '25
Seems you come from <7.4, so undefined array accesses lead to errors, not null as in 7.x. Others showed how to solve the particular problem. For a project-wide upgrade try rector. Maybe there is a way to solve it automatically if not, it's easy to write formatters. Maybe also should consider refactoring to some request class, take a look at some frameworks, how they solve it, there a lot of packages out there, which can make your dev life lots easier.
1
u/National-Collar-5052 Aug 20 '25
i dont like assigning get & post into variables. why not just use them as they are?
if ($_GET['id'] ....)
1
u/dave8271 Aug 18 '25
There's the best solution, which is to use a package such as Symfony's HttpFoundation to use a proper Request object that you can initialize from superglobals, and then not use superglobals at all.
$id = $request->query->getInt($id); // $id is now either an integer or null
Or, using superglobals, you can use null coalescing
$id = intval($_GET['id']) ?? null;
1
u/swampopus Aug 18 '25 edited Aug 18 '25
EDIT:
The closest to a drop-in replacement I can come up with in your use-case would be this:
$val = intval($_GET["id"] ?? NULL);So, you'd have to add ?? NULL (or ?? '', ?? 0, etc) within the intval() call.
---
Original comment:
I'm in the same boat with you; it's a huge pain. I tried writing a convenience function like so:
But honestly its still kind of a pain. Hate to say it but when in a rush, just @ suppressing the warning will do. Not best practice obviously, but sometimes you gotta do what you gotta do.
Ex:
$x = intval(@$_GET['csv']);0
u/[deleted] Aug 18 '25
You should have been using the second option anyway.
filter_input
0
u/LordAmras Aug 18 '25 edited Aug 18 '25
You can use a ternary
$id = isset($_GET['id'])? intav($_GET['id']) : 0
eventually have an helper request with a get method and a default value or use a library to deal with request properly.
The old php way of just ignoring array access errors brought too many issue