The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End php variable problems

  • This topic is empty.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
  • #31196

    Hey guys, I was doing some basic coding on my local host when I ran into this problem..

    I was coding a zebra-striped table and wanted to dynamically generate text for the lines.. I made a form input and a jQuery script to do this…

    My problem is that the variable isn’t recognized throughout the whole page script. My page is split into 3 files, header.php, index.php and footer.php. The header grabs the post string and sets up the variable, but any content after the header file won’t display the variable when i echo it. When I put the whole script into one file, it works fine. I also tested this out on my live server to see if my localhost was screwed up, but I got the same results…

    What is the problem?


    The variable is only available for the scope of the include i.e. header.php.
    If you call it in the index.php first then alter the value as global $var = "Woohoo" in header.php it will be available for any include below the index.php.


    Clokey2k, that is not true. At least, neither when I think back at my past experiences on this point, nor what I read from the man-page.

    There’s only two scopes in PHP. The global scope and the local scope. Include, include_once, require or require_once does not make new scopes, nor change scopes, for the included files.

    The local scope is within functions and classes, the global scope is the rest. Take a look at the following example:

    $a = '$a is global!';

    function test() {
    $a = '$a is local!';


    echo $a; // Output: $a is global!


    One problem I’ve seen quite often is people using functions to get different parts of the website, and then causing them self scope-trouble. Like the following example.

    function getPart($part) {
    require ('path/to/'.$part.'.php';
    $contentForHeader = 'Hi, im header!';

    Files included via the “getPart()” function, will be part of that functions local scope (a seperate scope for each call to that function) and will thus not have access to the $contentForHeader Variable.

    If one wish to use such functions, you could make a “super global array” for information you wish to use all over your site. Calling that “$data” and putting the line “global $data;” inside your getPart function would allow this.


    @marcdefiant: Take a look at this example:

    require 'header.php';

    $variableForHeader = 'HeaderInfo';

    echo $variableSetInHeader;

    require 'footer.php';

    The $variableForHeader will not be available in header.php for obvious reasons; it is declared after the header.php file is included.

    The echo $variableSetInHeader will work as expected.

    It is hard to say anything about what the error could be, when we have no clue about what your code looks like. ;-)


    Darn, I thought I had close. Variable scopes have always forced me crazy. I have just read the man pages include section which agrees with you @gno.
    Variables that are in an included file inherit the scope in the original document calling it. So if the file is included as part of a function, then variables would not be available outside of the function. Hopefully that has set me straight!
    I also hope that I have not added confusion to the original issue. Maybe representing some code here could help diagnose the issue a little further?


    From the examples you write here, there should be no difference what so ever.

    But well, the most important thing is that it works :-)


    @gno you are a life saver thank you for saving me from these templating torture i put myself through

Viewing 7 posts - 1 through 7 (of 7 total)
  • The forum ‘Back End’ is closed to new topics and replies.