recommended way to call php scripts

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
closde
Forum Newbie
Posts: 2
Joined: Tue Mar 23, 2010 6:58 am

recommended way to call php scripts

Post 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.
M2tM
Forum Commoner
Posts: 41
Joined: Sat Feb 27, 2010 12:35 pm

Re: recommended way to call php scripts

Post 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
 
closde
Forum Newbie
Posts: 2
Joined: Tue Mar 23, 2010 6:58 am

Re: recommended way to call php scripts

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: recommended way to call php scripts

Post 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.
Post Reply