来自变量的 PHP 函数

标签 php forms function

我希望能够从表单提交一个值,然后调用提交的函数,我的代码:

<?php
include 'specs.php';
if (empty($_POST) === false) {
$func = $_POST['function'];
$a1 = $_POST['a1'];
$a2 = $_POST['a2'];
$a3 = $_POST['a3'];
$a4 = $_POST['a4'];
$a5 = $_POST['a5'];
echo $func.'('$a1', '$a2', '$a3', '$a4', '$a5');';
}
?>
<form action="" method="post">
Function:<br><input type="text" name="function"><hr>
Value 1: <input type="text" name="a1">
Value 2: <input type="text" name="a2">
Value 3: <input type="text" name="a3">
Value 4: <input type="text" name="a4">
Value 5: <input type="text" name="a5">
<input type="submit" value="Execute">

最佳答案

您可以使用call_user_func_array

call_user_func_array ( 'thefuncname' , your_parameters )

在你的情况下,它会是这样的:

$func = $_POST['function'];
$parameters = array($a1, $a2, $a3, $a4, $a5);
call_user_func_array ( $func, $pameters);

关于来自变量的 PHP 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16227684/

相关文章:

将变量作为参数的默认值的 PHP 函数

php - 如果变量是 int 值,array_combine php 函数不会保留字符串变量的数据类型

php - htaccess 404 在添加尾部斜杠时无法正常工作

ruby-on-rails - Rails - 在表单中显示外键引用

php - 使用 PHP 序列化 jQuery AJAX 表单数据

c - 在C中解密文件的功能?

php - 找不到类 'SimpleXMLElement'

php - Zend GData Contacts PHP,添加联系人 SSL 错误

javascript - 获取span的内容

c - 用C语言编写没有指针的合并排序