如何在函数内调用期望$ _POST的脚本

时间:2010-05-18 15:18:11

标签: php post

我的script.php接受$_POST输入并回显字符串。

$theinput = $_POST['theinput'];
$output = //some processing;
echo $output;

我需要在不同文件second.php中的另一个函数中使用相同的代码,并且不想重复代码。但是第一个脚本script.php期望输入为$_POST。我在函数中的输入不仅仅是$_POST常规参数。

function processinput($someinput){
   //run the same script above, 
   //just the input is a parameter, not a `$_POST`
}

任何想法如何做到这一点?是否可以模拟$_POST或类似的东西?

3 个答案:

答案 0 :(得分:3)

您总是可以将值分配给$ _POST,就像它是一个数组一样。这是一个黑客工作,你可能最好更改函数以将值作为参数,但它会起作用。

$_POST['theinput'] = "value";

答案 1 :(得分:1)

您是否在寻找includeinclude_once方法?

second.php:

function processinput($someinput){
  $output = //some processing;
  echo $output;
}

的script.php:

include_once('second.php'); // second.php contains processinput()
$theinput = $_POST['theinput'];
processinput($theinput);

答案 2 :(得分:0)

function processinput($someinput){ 
   //I need to call the above script and give it  
   //the same input `$someinput` which is not a `$_POST` anymore 
   $results = strrev($someinput);
   return $results;
} 

$theinput = $_POST['theinput'];
$output = processinput($theinput);
echo $output;