468,306 Members | 1,219 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,306 developers. It's quick & easy.

Is it possible to pass a variable to a PHP script from inside another PHP piece of code?

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...]
<?php
echo("<i>the variable abc contains " . $_GET['abc'] . "</i>");
?>
[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...]
<?php
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!

Oct 27 '07 #1
3 1539
The file is l/test.php of course.

Oct 27 '07 #2
On Sat, 27 Oct 2007 23:44:56 +0200, le***********************@gmail.com
<le***********************@gmail.comwrote:
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...]
<?php
echo("<i>the variable abc contains " . $_GET['abc'] . "</i>");
?>
[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...]
<?php
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 $_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. So:

URL:
test.php?foo=bar

test.php:
<?php
$check = 'hello';
include './include.php';
?>

include.php
<?php
var_dump($_GET);
echo $check;
?>

... will correctly show the $_GET array and the $check variable.
--
Rik Wasmus
Oct 27 '07 #3
Thanks a lot for these very useful and quick answers!

Oct 28 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Jan Pieter Kunst | last post: by
6 posts views Thread by Chris Lasher | last post: by
8 posts views Thread by Daniel | last post: by
pbmods
1 post views Thread by pbmods | last post: by
12 posts views Thread by Bryan Parkoff | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.