Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Geek Culture / PHP include

Author
Message
AlexI
19
Years of Service
User Offline
Joined: 31st Dec 2004
Location: UK
Posted: 25th Sep 2006 21:35 Edited at: 25th Sep 2006 21:36
Hi,
how can i include the file score.php but like this


As it does not let you have the part after the ? I looked through php's documentation and could'nt find anything

Any ideas?
Thanks,
Alex


the_winch
21
Years of Service
User Offline
Joined: 1st Feb 2003
Location: Oxford, UK
Posted: 25th Sep 2006 22:23
http://php.net/include/

Quote: "When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope."


Should tell you what you need to know.

By way of demonstration, he emitted a batlike squeak that was indeed bothersome.
Richard Davey
Retired Moderator
22
Years of Service
User Offline
Joined: 30th Apr 2002
Location: On the Jupiter Probe
Posted: 25th Sep 2006 22:24
When you include a PHP file, it literally inserts the PHP code (in the included file) at that point in your script.

For example:



If you ran that, you would see 'orange' printed out, because there is a variable called $a declared in the included PHP file, that has over-written the variable set before it.

Following this logic rather than doing:

<?php include "score.php?game=1"; ?>

You can set the 'game' variable before the included script:

$game = 1;
include 'score.php';

Then make 'score.php' check for the existence of $game.

Cheers,

Rich

"Bite my shiny metal ass" (Futurama)
"Don't ping my cheese with your bandwidth" (Dilbert)
AlexI
19
Years of Service
User Offline
Joined: 31st Dec 2004
Location: UK
Posted: 25th Sep 2006 22:31
Thanks, the_wich!


PowerSoft
20
Years of Service
User Offline
Joined: 10th Oct 2004
Location: United Kingdom
Posted: 25th Sep 2006 23:18
or use $_GET[''];

Nicholas Thompson
20
Years of Service
User Offline
Joined: 6th Sep 2004
Location: Bognor Regis, UK
Posted: 26th Sep 2006 00:28
or the PHP manual...

[center]
Elite Gaming
21
Years of Service
User Offline
Joined: 10th May 2003
Location: Isle Of Wight, UK
Posted: 26th Sep 2006 00:48
You can just use <?php require_once('filename'); ?>.

Useful if you want to include files that may contain global functions, or to set configuration values.
Nicholas Thompson
20
Years of Service
User Offline
Joined: 6th Sep 2004
Location: Bognor Regis, UK
Posted: 26th Sep 2006 00:56
thats a different command...

include is more of a "trier". It will try to include but wont fully die if it cant. It'll just produce an error saying it cant. Require, however, is a "do-or-die" type command. If it cant get that file then it will kill the process and the page wont load. That file is REQUIRED.

You've also tagged on the _once suffix. This does an extra check to see if you've already included the file (to avoid duplicate function names, etc). Usefull if you dont know if you've done it before or not, but shouldn't just be used all the time. It has overhead that might not always be necessary.

[center]
Richard Davey
Retired Moderator
22
Years of Service
User Offline
Joined: 30th Apr 2002
Location: On the Jupiter Probe
Posted: 26th Sep 2006 11:49
Quote: "or use $_GET[''];"


.. which won't work in this instance as the super globals will have never been populated via an include.

"Bite my shiny metal ass" (Futurama)
"Don't ping my cheese with your bandwidth" (Dilbert)
PowerSoft
20
Years of Service
User Offline
Joined: 10th Oct 2004
Location: United Kingdom
Posted: 26th Sep 2006 20:43
I meant in conjunction with the URL Rich,

Login to post a reply

Server time is: 2024-11-17 15:33:22
Your offset time is: 2024-11-17 15:33:22