Is it possible to pass a variable to a PHP script from inside another PHP piece of code?
am 27.10.2007 23:44:56 von leonardodiserpierodavinci
Hi. Sorry for what is perhaps a neophyte question: is it possible to
pass a variable to a PHP script from inside another PHP piece of code?
For instance, the file test.php (which of course resides on a
webserver that supports PHP) is as such:
[some HTML code...]
echo("the variable abc contains " . $_GET['abc'] . "");
?>
[some more HTML code...]
and if I open this file directly i.e. by typing test.php?abc=888 in
the URL line of a web browser, I correctly get the message "the
variable abc contains 888" in italic, surrounded by the HTML content.
Now, let's say that I want to include all this content in another
file, index.php.
I tried to call it as such in index.php
[other HTML...]
include "test.php?abc=888";
?>
[yet other HTML...]
which is not the correct way as it gives the following error:
Warning: main(l/test.php?abc=888) [function.main]: failed to open
stream: No such file or directory in /home/www/index.php on line 52
Warning: main() [function.include]: Failed opening 'l/test.php?
abc=888' for inclusion (include_path='.:/usr/share/php:/usr/share/
pear') in /home/www/index.php on line 52
How should I call the file?
Thanks in advance for any hint!
Re: Is it possible to pass a variable to a PHP script from inside another PHP piece of code?
am 27.10.2007 23:50:47 von luiheidsgoeroe
On Sat, 27 Oct 2007 23:44:56 +0200, leonardodiserpierodavinci@gmail.com =
=
wrote:
> Hi. Sorry for what is perhaps a neophyte question: is it possible to
> pass a variable to a PHP script from inside another PHP piece of code?=
>
> For instance, the file test.php (which of course resides on a
> webserver that supports PHP) is as such:
>
> [some HTML code...]
>
> echo("the variable abc contains " . $_GET['abc'] . "");
> ?>
> [some more HTML code...]
>
> and if I open this file directly i.e. by typing test.php?abc=3D888 in
> the URL line of a web browser, I correctly get the message "the
> variable abc contains 888" in italic, surrounded by the HTML content.
>
> Now, let's say that I want to include all this content in another
> file, index.php.
> I tried to call it as such in index.php
>
> [other HTML...]
>
> include "test.php?abc=3D888";
> ?>
> [yet other HTML...]
>
> which is not the correct way as it gives the following error:
>
> Warning: main(l/test.php?abc=3D888) [function.main]: failed to open
> stream: No such file or directory in /home/www/index.php on line 52
> Warning: main() [function.include]: Failed opening 'l/test.php?
> abc=3D888' for inclusion (include_path=3D'.:/usr/share/php:/usr/share/=
> pear') in /home/www/index.php on line 52
>
> How should I call the file?
> Thanks in advance for any hint!
The $_GET array is available to all scipts, _provided_ you use the file =
=
system to include the file, and not by http some newcomers seem to do. S=
o:
URL:
test.php?foo=3Dbar
test.php:
$check =3D 'hello';
include './include.php';
?>
include.php
var_dump($_GET);
echo $check;
?>
... will correctly show the $_GET array and the $check variable.
-- =
Rik Wasmus
Re: Is it possible to pass a variable to a PHP script from insideanother PHP piece of code?
am 27.10.2007 23:58:41 von Jerry Stuckle
leonardodiserpierodavinci@gmail.com wrote:
> Hi. Sorry for what is perhaps a neophyte question: is it possible to
> pass a variable to a PHP script from inside another PHP piece of code?
>
> For instance, the file test.php (which of course resides on a
> webserver that supports PHP) is as such:
>
> [some HTML code...]
>
> echo("the variable abc contains " . $_GET['abc'] . "");
> ?>
> [some more HTML code...]
>
> and if I open this file directly i.e. by typing test.php?abc=888 in
> the URL line of a web browser, I correctly get the message "the
> variable abc contains 888" in italic, surrounded by the HTML content.
>
> Now, let's say that I want to include all this content in another
> file, index.php.
> I tried to call it as such in index.php
>
> [other HTML...]
>
> include "test.php?abc=888";
> ?>
> [yet other HTML...]
>
> which is not the correct way as it gives the following error:
>
> Warning: main(l/test.php?abc=888) [function.main]: failed to open
> stream: No such file or directory in /home/www/index.php on line 52
> Warning: main() [function.include]: Failed opening 'l/test.php?
> abc=888' for inclusion (include_path='.:/usr/share/php:/usr/share/
> pear') in /home/www/index.php on line 52
>
> How should I call the file?
> Thanks in advance for any hint!
>
>
The include statement goes directly to the file system and loads the
file, just like if you copied and pasted it in an editor.
When you load it from your browser, you are loading it through the web
server, which parses out the values and places the "abc=888" in the
$_GET superglobal.
So the file name is not "test.php?abc=888" (could you open it this way
in your text editor?).
So two things. First of all, include the file by the name - absolute
path is best, i.e.
include($_SERVER['DOCUMENT_ROOT'] . '/test.php');
($_SERVER['DOCUMENT_ROOT'] is a predefined variable which points to the
root directory of your website.)
This effectively adds the file to your current one, very similar to a
copy/paste operation.
So, for the second part, any variables defined before you include the
file (and are not within function scope) will be available to the
included file.
So if you were to say
$abc=888;
before your include, in the included file $abc will contain 888.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================