Starting with Superglobals (Hope it's the right category^^')

Hi, I’m new around here. I’ve started to study how to program a few weeks ago. I’ve done little things before by myself. But now I go to school on two days and work in a company the rest of the week.

My problem is I’ve gotten a tool which uses an old php-version and used register-globals at one point. Around 2014 a work-around was implemented and now my task is to substitute this with superglobals. I’ve read the tutorials at w3schools and the chapter in various books and on various pages. But they all go only as far as how to use them in simple statements. At my company no one has time to answer some questions I have.

I have a configuration file in which the work-around for the register-globals is defined. And all over the script I have variables like: $var etc that are used for sql-queries and if-statements and use input mainly from the post-method.

I thought I could now implement the superglobals by defining anew in the configuration-file as for example $var = (int) $_POST[‘var’];
But then I get a whole lot of undefined-index/variable notices.
So I thought I’d use isset() like so:
[php]
if (isset($_POST[‘var’])) {
$var = (int) $_POST[‘var’];
} else {
// I don’t really know what to do best here either
}
[/php]

But do I have to do that for every single one of the variables? Or is there a way to get this checked for every variable that should consist of something out of $_GET or $_POST in a loop for example?

Honestly, global variables are bad, not to be confused with super globals like $_SERVER, $_POST.

It is a horrid way to handle things.

ehm… yeah…

You can use a ternary (short hand if else) to set $var to the value of POST var, or a default value (in this case null)

[php]$var = isset($_POST[‘var’]) ? $_POST[‘var’] : null;[/php]

Thank you :slight_smile:
But this way I cannot use the typecast, can I?
And my code looks awefully repetetive :frowning:

Sure you can

[php]$var = isset($_POST[‘var’]) ? (int) $_POST[‘var’] : null;[/php]

Add an example of how bad it gets (repetitive wise)

You’re trying to put a band-aid on a band-aid. Creating variables for nothing when the data has not been transformed somehow is pointless and redundant and will just bloat your app.

You would be better off posting your actual code for us to see rather than asking how to patch it.

The solutions to your question are not the answers to your problem.

Sponsor our Newsletter | Privacy Policy | Terms of Service