检查动态变量(is_numeric($$变量))

时间:2016-03-29 01:29:40

标签: php

我试图检查动态变量是否仅包含数字,但我收到以下错误:

Warning: Illegal string offset 'name' in /home/aet/website.com/pages/upload.php on line 34

Notice: Undefined variable: a in /home/aet/website.com/pages/upload.php on line 34

Warning: Illegal string offset 'name' in /home/aet/website.com/pages/upload.php on line 38

Notice: Undefined variable: a in /home/aet/website.com/pages/upload.php on line 38

我真正想要的是根据白名单动态声明大型表单中的变量(但不是数组键,而是我preg_replace它们。)

$list = array('name' => 'mandatory', 'surname' => 'optional');

foreach ( $list as $name => $nouse ) {
    if (isset($_POST[$name])) {
        if ( is_array($_POST[$name]) ) {
            $$name = filter_input( INPUT_POST , $name , FILTER_SANITIZE_STRING , FILTER_REQUIRE_ARRAY );
            foreach ($$name as $key => $value) {
                $key = preg_replace('[a-z]', '', $key);
                if ( is_numeric($$name[$key]) ) { // this is line 34
                    $$key = (int) $$name[$key];
                    echo $report[$key] = $key . ' variable (from ' . $name . ' ) created1... <span style="color:green;">OK!</span><br>';
                } else {
                    $$key = $$name[$key];  // this is line 38
                    echo $report[$key] = $key . ' variable (from ' . $name . ' ) created2... <span style="color:green;">OK!</span><br>';
                }
            }
        } else {
            if ( is_numeric($_POST[$name]) ) {
                $$name = filter_input( INPUT_POST , $name , FILTER_SANITIZE_NUMBER_INT );
                echo $report[$name] = $name . ' variable created3... <span style="color:green;">OK!</span><br>';
            } else {
                $$name = filter_input( INPUT_POST , $name , FILTER_SANITIZE_STRING );
                echo $report[$name] = $name . ' variable created4... <span style="color:green;">OK!</span><br>';
            }
        }
    } else {
        if ($list[$name] == 'optional') {
            $$name = 0;
            echo $report[$name] = $name . ' (optional) variable not filled5... <span style="color:green;">OK!</span><br>';
        } else die('Error: You must fill all mandatory fields! (' . $name . ')');
    }
}

当名称指向帖子数组名称时,会触发错误。 html很简单:

<input type="text" name="address_book[name]" />
<input type="text" name="address_book[surname]" />
编辑:我发现了一个错误,但遗憾的是并没有解决问题。 $name数组中的address_book[name]变量覆盖了第一个foreach中的$name变量,现在消息正常工作:

Warning: Illegal string offset 'name' in /home/aet/website.com/pages/upload.php on line 34

Notice: Undefined variable: a in /home/aet/website.com/pages/upload.php on line 34

Warning: Illegal string offset 'name' in /home/aet/website.com/pages/upload.php on line 38

Notice: Undefined variable: a in /home/aet/website.com/pages/upload.php on line 38
name variable (from address_book ) created2... OK!

Warning: Illegal string offset 'surname' in /home/aet/website.com/pages/upload.php on line 34

Notice: Undefined variable: a in /home/aet/website.com/pages/upload.php on line 34

Warning: Illegal string offset 'surname' in /home/aet/website.com/pages/upload.php on line 38

Notice: Undefined variable: a in /home/aet/website.com/pages/upload.php on line 38
surname variable (from address_book ) created2... OK!

1 个答案:

答案 0 :(得分:1)

您正在混淆变量并将其重用于多种用途。这里可能存在多个问题。

在一个特殊情况下我可以看到,你覆盖id然后用它来索引回数组:

$key

删除foreach ($$name as $key => $value) { $key = preg_replace('[a-z]', '', $key); if ( is_numeric($$name[$key]) ) { 表示法并给出(略微)更好的变量名称,这变为:

$$

当你想说的是:

foreach ($filtered_value as $sub_key => $sub_value) {
    $sanitised_sub_key = preg_replace('[a-z]', '', $sub_key);
    if ( is_numeric($filtered_value[$sanitised_sub_key]) ) {

或更简单:

    if ( is_numeric($filtered_value[$sub_key]) ) {

这是变量名称重要的一个很好的例子。通过扩展我认为动态变量总是一个坏主意 - 应该选择调用变量的内容,如果你有一组相关变量,它们应该在数组,因此您可以命名该集。 (例如 if ( is_numeric($sub_value) ) { ,而非$filtered_input[$field_name])。