[英]Php function from a variable
我希望能够从表单提交值,然后调用提交的函数,我的代码:
<?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);
或者只是echo $func(array($a1, $a2, $a3, $a4, $a5));
(也许您不想要一个数组,所以您可以只放入$func($a1, $a2, $a3, etc);
)
更换
echo $func.'('$a1', '$a2', '$a3', '$a4', '$a5');';
同
call_user_func_array($func, array($a1,$a2,$a3,$a4,$a5));
//suggust add some prefix on func ,then check function_exists
$func = 'auto_'.$_POST['function'];
if (function_exists( $func)) {
$func(array($a1, $a2, $a3, $a4, $a5));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.