1

Is there a way I can check all these values without having to code all these if statements and then grab the last value for instance in this code example sub10 How can I do this using PHP?

Here is the PHP code.

if(isset($_GET['sub1'])) {
    $sub1 = $_GET['sub1'];
}

if(isset($_GET['sub2'])) {
    $sub2 = $_GET['sub2'];
}

if(isset($_GET['sub3'])) {
    $sub3 = $_GET['sub3'];
}

if(isset($_GET['sub4'])) {
    $sub4 = $_GET['sub4'];
}

if(isset($_GET['sub5'])) {
    $sub5 = $_GET['sub5'];
}

if(isset($_GET['sub6'])) {
    $sub6 = $_GET['sub6'];
}

if(isset($_GET['sub7'])) {
    $sub7 = $_GET['sub7'];
}

if(isset($_GET['sub8'])) {
    $sub8 = $_GET['sub8'];
}

if(isset($_GET['sub9'])) {
    $sub9 = $_GET['sub9'];
}

if(isset($_GET['sub10'])) {
    $sub10 = $_GET['sub10'];
}
Jeff
  • 21,744
  • 6
  • 51
  • 55
g4tv
  • 31
  • 3
  • Duplicate of [Variable functions and variable names in PHP](http://stackoverflow.com/questions/1617976/variable-functions-and-variable-names-in-php), [How can I simplify this redundant code?](http://stackoverflow.com/questions/2882183/how-can-i-simplify-this-redundant-code). – outis Oct 02 '10 at 19:03

8 Answers8

4

You could use a for loop that checks the parameters:

for ($i=1; $i<10; ++$i) {
    if (isset($_GET['sub'.$i])) {
        ${'sub'.$i} = $_GET['sub'.$i];
    }
}

The syntax ${'sub'.$i} is a variable variable syntax to refer to the variable identified by the value of the expression 'sub'.$i.

And if you just want the last subX parameter, test for parameters in reverse order:

$sub = null;
for ($i=10; $i>=1; --$i) {
    if (isset($_GET['sub'.$i])) {
        $sub = $_GET['sub'.$i];
        break;
    }
}
Gumbo
  • 643,351
  • 109
  • 780
  • 844
3

The easiest way would be to make the form with sub[] elements, like this:

Sub 1 <input type="text" name="sub[]" />
Sub 2 <input type="text" name="sub[]" />
Sub 3 <input type="text" name="sub[]" />
Sub 4 <input type="text" name="sub[]" />
Sub 5 <input type="text" name="sub[]" />

PHP will combine all of those into an array.

Then, to get the last one, it'd be $_GET['sub'][-1].

Edit: This is essentially the same thing NullUserException is doing, but he's doing it directly in the url instead of a form.

Powerlord
  • 87,612
  • 17
  • 125
  • 175
1

A more elegant solution would be to use an array and the foreach statement.

DMags
  • 1,621
  • 1
  • 13
  • 12
1

/foo.php?sub=1,2,3,4,5

$subs = explode(',', $_GET['sub']);

Then you can get the last value from $subs[count($subs)-1] or array_pop($subs) or however else you'd like.

Matthew
  • 47,584
  • 11
  • 86
  • 98
  • Alternately, you could do something like `/foo.php?sub[]=1&sub[]=2&sub[]=3&sub[]=4&sub[]=5`, though it's a bit more verbose and only saves you one php command. – Lèse majesté Aug 23 '10 at 18:27
1

Obligatory variable variables version:

$keys = preg_grep('/^sub\d+$/', array_keys($_GET));

foreach ($keys as $key) {
  $$key = $_GET[$key];
}

On the plus side: Doesn't need to loop up to PHP_MAX_INT as it'll only work on what's really there. Down side... variable variables.

Marc B
  • 356,200
  • 43
  • 426
  • 500
1

Here's a more sensible solution:

Demo: http://bit.ly/diY6Pk

URL: http://ablazex.com/demos/multi.php?sub[]=Hello&sub[]=There&sub[]=Angel&sub[]=From&sub[]=My&sub[]=Nightmare

Code:

print_r($_GET['sub']);

$sub = end($_GET['sub']);
echo $sub;
NullUserException
  • 83,810
  • 28
  • 209
  • 234
0

I tried to come up with a solution with extract, but it got ridiculously complicated:

extract(array_intersect_key($_GET, array_flip(array_map(function ($i) { return 'sub' . $i; }, range(1, 10)))));

Isn't there a way to do this shorter?

Here a formatted version:

extract(
    array_intersect_key(
        $_GET,
        array_flip(
            array_map(
                function ($i) {
                    return 'sub' . $i;
                },
                range(1, 10)
            )
        )
    )
);

What it does: Create an array with the numbers from 1 to 10, prefix them with sub, turn the array around (so the variable names are now in the keys of the array) and then intersect with the $_GET array. Thus only sub1..10 are extracted.

NikiC
  • 100,734
  • 37
  • 191
  • 225
0

As dmags said, I prefer to use array (and I use this regularly). It's more elegant (IMHO) and works also if you have different variable names (i.e. name, address, city, country, zip, etc...)

$vars= array('sub1','sub2','sub3','sub4','sub5','sub6','sub7','sub8','sub9','sub10');
foreach ($vars as $getvar)
    if (isset($_GET[$getvar])) {
        $$getvar= $_GET[$getvar];
    }
}
fabioferrero
  • 69
  • 1
  • 2