Page 1 of 1

How to call another PHP script?

Posted: Sun Dec 10, 2006 6:33 am
by andrew_here
Hi!

I'm new to PHP.

I'm trying to call another PHP script from a PHP script. And I also need to pass some variables. I've tried using file (http://localhost/script.php?variable1=h ... ble2=world ). However, it does not work.

Can someone help?

Thanks!

Posted: Sun Dec 10, 2006 8:08 am
by neophyte
Please post the code you are using. And be sure to use the PHP tags for color coding and formatting.

Posted: Sun Dec 10, 2006 8:40 am
by feyd
include() most often works just fine.

Posted: Sun Dec 10, 2006 9:09 am
by andrew_here
Hi Neophyte and Feyd,

I've tried include, but it doesnt seem to work too.

I've used the following statement:

include 'http://localhost/script.php?variable1=h ... ble2=world'

Is that the correct syntax?

My receiving php script has the following statements to accept:

$var1 = $_GET['variable1'];
$var2 = $_GET['variable2'];

Am I missing something??

Thanks!

Posted: Sun Dec 10, 2006 9:13 am
by John Cartwright
Index.php

Code: Select all

$foo = 'hello';
$bar = 'world';

include 'script.php';
Script.php

Code: Select all

echo $foo .' '. $bar;
:wink:

Posted: Sun Dec 10, 2006 9:26 am
by andrew_here
Hi Jcart and everyone!

Does this mean that when you include 'other script', it's as if the entire content of the the other script is added on? Does this add significant overhead (assuming that my other script is in a loop and if not needed, the statements of my other script are not excuted and can be bypassed by conditional statements)? I'm asking as my function will be called very frequently and my other script is quite big.

Thanks!

Posted: Sun Dec 10, 2006 9:35 am
by aaronhall
When you include a file, it's interpreted in exactly the same way as if you were to put the contents of the file in place of the include() call. If you are calling include() from inside a loop, I don't know that PHP is smart enough to include it once and call it from memory on subsequent calls. It would be better to put the code in the included file into a function, include the file before the loop, and call that function from within the loop.

edit: Of course, the code inside the function won't see any variables that are outside of the function, except those that are passed to it as arguments.

Posted: Sun Dec 10, 2006 10:26 am
by John Cartwright
aaronhall wrote:If you are calling include() from inside a loop, I don't know that PHP is smart enough to include it once and call it from memory on subsequent calls
include_once() :wink:

Posted: Sun Dec 10, 2006 10:37 am
by aaronhall
That crossed my mind and I quickly tossed it aside after I thought it would throw a warning on the second loop... time to go to bed :lol:

Posted: Sun Dec 10, 2006 10:55 pm
by andrew_here
Thanks guys,

Your recommendations work.

But is there a way to call an address using PHP?

Maybe something like

Code: Select all

file (http://localhost/script.php?variable1=h ... ble2=world )
Because by default, my other script is programmed to get variables

Code: Select all

$var1 = $_GET['variable1'];
$var2 = $_GET['variable2'];
So even if I were to define variables in my calling script, they would be overwritten to be empty.

Thanks!

Posted: Sun Dec 10, 2006 11:28 pm
by Sloth
I should think that file statement would work fine. It'll call your script but then it won't really display the output =/

Depends on what you're trying to accomplish tho

Posted: Mon Dec 11, 2006 2:58 am
by aaronhall
I'm not sure if this is what you're looking for, but you can redirect to that page using header:

Code: Select all

<?php
header("Location: http://localhost/script.php?variable1=$ ... ble2=$var2");
?>
This must be called before any output is sent to the browser.