2015-05-17 48 views
-1
echo "<input type='hidden' name='pb1' value='$_POST[pb1]'>"; 
echo "<input type='hidden' name='pb2' value='$_POST[pb2]'>"; 

echo "<input type='hidden' name='pc1' value='$_POST[pc1]'>"; 
echo "<input type='hidden' name='pc2' value='$_POST[pc2]'>"; 

我想通过像下面这样的函数调用来执行上述任务。在php输入类型中使用php函数隐藏

function fun1($rm) 
{ 
    for ($i=1;$i<=2;$i++) 
    { 
     echo "<input type='hidden' name='p.$rm.$i' value='$_POST[p.$rm.$i]'>"; 
    } 
} 

fun1('b'); 
fun1('c'); 

请建议如何编辑函数内部的代码以实现目标。

+0

如果将变量连接到字符串中,只需用双引号确定字符串 – Rizier123

回答

0

你在这里混合两种技术。在PHP中,您可以直接在双引号字符串中使用variabled。在一个单引号字符串中,你必须终止并连接。但是,你也试图在一个双引号字符串中动态地访问一个数组索引,而且不幸的是不会工作。这里是双向做你想要什么:

echo "<input type='hidden' name='p$rm$i' value='".$_POST['p'.$rm.$i]."'>"; 

echo '<input type="hidden" name="p$rm$i" value="'.$_POST['p'.$rm.$i].'">'; 

// If you weren't accessing the array index dynamically (with a variable) this would work: 
echo "<input type='hidden' name='p$rm$i' value='$_POST[pb1]'>"; 
// note no quotes for the index when inside a string ^^^ 

功能上它们是完全一样的。哪一个使用实际上是一种偏好。我会说选一个你觉得更可读的。