我试图检查一个动态变量是否只包含数字,但我得到了以下错误:
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]" />
编辑:我发现了一个错误,但不幸的是没有解决问题。来自address_book[name]
数组的$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!
您混淆了变量,并将它们用于多种目的。这里可能有多个问题。
在我可以看到的一个特定情况下,您正在重写$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]
而不是$$field_name
)。