WordPress.org

Support

Support » Miscellaneous » calling global variables into a function in header.php

calling global variables into a function in header.php

  • I was having trouble trying to call global variables into a function I’ve put in my modified header, so I tried using a simple function in the default header.php and I’m still having the same problem that the global variables just don’t seem to work in the funtion:


    <?php
    $a = 1;
    $b = 2;
    function Sum() {
    global $a, $b;
    $b = $a + $b;
    }

    Sum();
    echo 'b value is:'.$b;
    ?>

    When in an empty php page this function outputs $b=3, but in header.php $b=2.

    Does anyone know why? Any help would be greatly appreciated.

Viewing 4 replies - 1 through 4 (of 4 total)
  • schestowitz
    Member

    @schestowitz

    I am not sure what you are trying to achieve (a test/exercise?), but make sure $a and $b are not *already* defined somewhere in header.php . There might be a variable name collision and overriding it can cause all sorts of oddities. That’d be my guess.

    Thanks for the reply schestowitz. No the actual function i was trying to use was


    <head>
    <?php
    $current_tab = 'home';

    function get_tab($tab,$tab_href,$tab_title) {
    global $current_tab;
    if ($current_tab == $tab) {
    $tab_class = 'tabselected';
    } else {
    $tab_class = 'tab';
    }
    echo "
    < a href='$tab_href' class='$tab_class'>$tab_title</a >";
    }
    ?>
    </head>
    <body>
    <?php
    get_tab('home','/','Welcome');
    get_tab('news','/articles/','News');
    get_tab('tools','/tools/','Tools');
    get_tab('support','/support/','Support');
    get_tab('contact','/contact/','Contact');
    ?>
    </body>

    $current_tab works fine outside of the function – if i try and echo $current_tab within the function as the link text for instance, nothing is printed so it’s like it just hasn’t passed into the function, if i echo it in the body tags however it prints fine – so i don’t think its a variable collision, unless i’m misunderstanding what you mean by that.

    My understanding of php isn’t fantastic, so i tired to use a much simpler function for testing purposes just so I could be sure it wasn’t get_tab() .

    Thanks for your suggestion though, any more ideas? …pretty please

    I know bumps are annoying, but does anyone have any ideas whatsoever? Im still stumped 🙁

    Thankyou in advance

    In case anyone else has this problem, the only work around i could think of was to pass the global variable into the function when calling the function ie:

    <head>
    <?php
    $current_tab = 'home';

    function get_tab($tab,$tab_href,$tab_title,$current_tab) {
    if ($current_tab == $tab) {
    $tab_class = 'tabselected';
    } else {
    $tab_class = 'tab';
    }
    echo "
    < a href='$tab_href' class='$tab_class'>$tab_title</a >";
    }
    ?>
    </head>
    <body>
    <?php
    get_tab('home','/','Welcome',$current_tab);
    ?>
    </body>

    This probably isn’t very good coding, but it does work.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘calling global variables into a function in header.php’ is closed to new replies.