My script.php
accepts $_POST
input and echos a string.
$theinput = $_POST['theinput'];
$output = //some processing;
echo $output;
I need the same code in another function in a different file second.php
and don't want to repeat the code. But the first script script.php
expects input as $_POST
. My input in the function is not $_POST
just a regular parameter.
function processinput($someinput){
//run the same script above,
//just the input is a paramet开发者_Go百科er, not a `$_POST`
}
Any ideas how to do that? Is it possible to simulate a $_POST
or something like that?
you can always assign values to $_POST just as if it is an array. A bit of a hack-job, and you are probably better off changing the function to take the value in as a parameter, but it will work.
$_POST['theinput'] = "value";
Are you looking for the include or include_once method?
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);
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;
精彩评论