How to call another PHP script?
Moderator: General Moderators
-
andrew_here
- Forum Newbie
- Posts: 4
- Joined: Sun Dec 10, 2006 6:28 am
How to call another PHP script?
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!
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!
-
andrew_here
- Forum Newbie
- Posts: 4
- Joined: Sun Dec 10, 2006 6:28 am
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!
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!
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Index.php
Script.php

Code: Select all
$foo = 'hello';
$bar = 'world';
include 'script.php';Code: Select all
echo $foo .' '. $bar;-
andrew_here
- Forum Newbie
- Posts: 4
- Joined: Sun Dec 10, 2006 6:28 am
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!
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!
- aaronhall
- DevNet Resident
- Posts: 1040
- Joined: Tue Aug 13, 2002 5:10 pm
- Location: Back in Phoenix, missing the microbrews
- Contact:
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.
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.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
include_once()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
-
andrew_here
- Forum Newbie
- Posts: 4
- Joined: Sun Dec 10, 2006 6:28 am
Thanks guys,
Your recommendations work.
But is there a way to call an address using PHP?
Maybe something like
Because by default, my other script is programmed to get variables
So even if I were to define variables in my calling script, they would be overwritten to be empty.
Thanks!
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 )Code: Select all
$var1 = $_GET['variable1'];
$var2 = $_GET['variable2'];Thanks!
- aaronhall
- DevNet Resident
- Posts: 1040
- Joined: Tue Aug 13, 2002 5:10 pm
- Location: Back in Phoenix, missing the microbrews
- Contact:
I'm not sure if this is what you're looking for, but you can redirect to that page using header:
This must be called before any output is sent to the browser.
Code: Select all
<?php
header("Location: http://localhost/script.php?variable1=$ ... ble2=$var2");
?>