简体   繁体   English

如何消除此PHP代码中的重复?

[英]How do I eliminate repetition in this PHP code?

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? 有没有一种方法可以检查所有这些值而不必编写所有这些if语句,然后在此代码示例sub10获取例如最后的值,该如何使用PHP?

Here is the PHP code. 这是PHP代码。

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'];
}

You could use a for loop that checks the parameters: 您可以使用for循环来检查参数:

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 . 语法${'sub'.$i}是一种变量变量语法,用于引用由表达式'sub'.$i的值标识的变量。

And if you just want the last subX parameter, test for parameters in reverse order: 如果只需要最后一个subX参数,请按相反顺序测试参数:

$sub = null;
for ($i=10; $i>=1; --$i) {
    if (isset($_GET['sub'.$i])) {
        $sub = $_GET['sub'.$i];
        break;
    }
}

The easiest way would be to make the form with sub[] elements, like this: 最简单的方法是使用sub[]元素制作表单,如下所示:

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. PHP将所有这些组合成一个数组。

Then, to get the last one, it'd be $_GET['sub'][-1] . 然后,要获取最后一个,它将是$_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. 编辑:这基本上是NullUserException所做的相同的事情,但他直接在url中而不是在表单中进行。

一个更优雅的解决方案是使用数组和foreach语句。

/foo.php?sub=1,2,3,4,5 /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. 然后,您可以从$subs[count($subs)-1]array_pop($subs)获取最后一个值,也可以根据需要获取其他值。

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. 从好的方面来说:不需要循环到PHP_MAX_INT,因为它仅适用于实际存在的内容。 Down side... variable variables. 缺点...可变变量。

Here's a more sensible solution: 这是一个更明智的解决方案:

Demo: http://bit.ly/diY6Pk 演示: http//bit.ly/diY6Pk

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

Code: 码:

print_r($_GET['sub']);

$sub = end($_GET['sub']);
echo $sub;

I tried to come up with a solution with extract , but it got ridiculously complicated: 我试图提出一个带有extract的解决方案,但是它变得异常复杂:

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. 它的作用:创建一个数字,范围为1到10,在其前面加上sub ,然后旋转该数组(这样变量名现在位于该数组的键中),然后与$_GET数组相交。 Thus only sub1..10 are extracted. 因此,仅提取sub..10。

As dmags said, I prefer to use array (and I use this regularly). 正如dmags所说,我更喜欢使用数组(并且我经常使用它)。 It's more elegant (IMHO) and works also if you have different variable names (ie name, address, city, country, zip, etc...) 如果您使用不同的变量名(例如,名称,地址,城市,国家/地区,邮政编码等),它会更优雅(IMHO),并且也可以使用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM