How to call another PHP script?

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
andrew_here
Forum Newbie
Posts: 4
Joined: Sun Dec 10, 2006 6:28 am

How to call another PHP script?

Post 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!
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Please post the code you are using. And be sure to use the PHP tags for color coding and formatting.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

include() most often works just fine.
andrew_here
Forum Newbie
Posts: 4
Joined: Sun Dec 10, 2006 6:28 am

Post 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!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Index.php

Code: Select all

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

include 'script.php';
Script.php

Code: Select all

echo $foo .' '. $bar;
:wink:
andrew_here
Forum Newbie
Posts: 4
Joined: Sun Dec 10, 2006 6:28 am

Post 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!
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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:
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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:
andrew_here
Forum Newbie
Posts: 4
Joined: Sun Dec 10, 2006 6:28 am

Post 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!
Sloth
Forum Newbie
Posts: 18
Joined: Thu Dec 07, 2006 7:29 pm

Post 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
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

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