Page 1 of 1

recommended way to call php scripts

Posted: Tue Mar 23, 2010 7:13 am
by closde
The following is a general 'howto' question.
I want to call one php script from another php script, and pass some parameters.
I want the called script to return some values.
Is there more than one way to do this and if so what's recommended practice?

A related question is, if I call a php script from the url box in my browser, can I add something for the parameters that will replicate the way the script would work had it been called with those parameters from another php script?

Many thanks for any thoughts.

Re: recommended way to call php scripts

Posted: Tue Mar 23, 2010 7:43 am
by M2tM
It is called a "function" and it looks like this:

Code: Select all

 
function SomeCodeHere($value1, $value2){
   $resultValue = $value1 + $value2;
   return $resultValue;
}
 
$result = SomeCodeHere(10, 20);
echo $result; //outputs 30
 

If you want multiple returns you can achieve that with a pass by reference syntax using the &

Code: Select all

 
function SomeCodeHere(&$value1, &$value2){
    $value1 += 10;
    $value2 -= 10;
}
 
$a = 50; $b = 50;
SomeCodeHere($a, $b);
echo $a.' '.$b; //outputs 60 40
 
I'm not sure if this is what you mean, but this is what you are describing so if I am not answering your question, please re-phrase it with exactly what you're trying to do.

*edit: it looks like you added another part to the question about typing in a URL and having the script read those values:

let's say our script is called index.php:

type in http://website.com/index.php?Key=SomeVa ... SomeValue2

then a script could read those values like so:

Code: Select all

 
echo $_GET['Key'].' '.$_GET['Key2']; //Should output SomeValue SomeValue2
 

Re: recommended way to call php scripts

Posted: Wed Mar 24, 2010 5:50 am
by closde
Thanks for your reply - however I was aware of the role of functions in php.
What I wanted to do was from within a php script, call another php script as if it were a function; i.e. pass it arguments, and get a return value.
There is more than one way to do this, but I wondered if there was a recommended way of doing it.
Also calling from the browser, I wanted to simulate this passing of arguments - not by naming them in the conventional way.

Re: recommended way to call php scripts

Posted: Wed Mar 24, 2010 7:59 am
by requinix
The recommended way is to put the functionality of the page inside a function, then include the file with it and call the function whenever you need it.