Re: sending variable to include file



"vetchling" <vetchling@xxxxxxxxx> writes:

Hi, I'm pretty new to php but I've been working on a site and have a
problem I can't fix, though it's probably pretty simple.

Basically I have an index.php file that includes a header.php file. I
attempt to pass a variable to it as follows:
<?include("header.php?page=Home");?>

Then the header.php file contains this code:
<?php
$page = $_GET['page'];
?>
<html><head><title><?$page?></title>

I want $page to contain the string "Home" of course, but it ends up
being nothing but an empty string.

Thanks in advance for your help.

You can't add ?args to an include; PHP doesn't evalutate that when
including files. (In fact, it usually never evaluates URL arguments:
that's the HTTP daemon's job.) It merely copies the contents of
header.php directly into your document (or parses equivilantly).

To do this, you need to do:

#In mypage.php you need to access $mypage_page directly.
mypage_page = "Home";
include ("mypage.php");


I've prefixed the variable $page to avoid naming conflicts.

--
Andrew Poelstra <http://www.wpsoftware.net/projects>
To reach me by email, use `apoelstra' at the above domain.
"Do BOTH ends of the cable need to be plugged in?" -Anon.
.



Relevant Pages

  • Re: sending variable to include file
    ... vetchling wrote: ... problem I can't fix, ... being nothing but an empty string. ...
    (comp.lang.php)
  • Re: 5.2.2 and POST data change?
    ... scripts immediately broke, although the 5.2.2 release notes don't seem to indicate I should have a problem. ... Starting yesterday it's always an empty string. ... How hould I interpret the 'php' protocol, and what does //input point to? ... zip, php, file, data, http, ftp, compress.bzip2, compress.zlib, https, ftps ...
    (comp.lang.php)
  • RESOLVED, sort of -- Re: [PHP] where is my session data on my new server?
    ... I still don't know what the bug was or how it was fixed or if I can ... return to the old behavior (which certainly would put me back in business ... and 2) probably not something I can "fix" quickly. ...
    (php.general)
  • Re: Get URL
    ... > This is best done by the PHP files themselves. ... * Retrieves the value of an element of an associative array. ... The default is the empty string. ... * Array identifier. ...
    (comp.lang.php)
  • Re: 2-stage contact form - mysterious error
    ... > I voluntarily update the copy on the site for the owners but the forms ... > php. ... then we can help you fix ...
    (php.general)