Title: calling global variables into a function in header.php
Last modified: August 18, 2016

---

# calling global variables into a function in header.php

 *  [budoink](https://wordpress.org/support/users/budoink/)
 * (@budoink)
 * [20 years, 5 months ago](https://wordpress.org/support/topic/calling-global-variables-into-a-function-in-headerphp/)
 * 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](https://wordpress.org/support/users/schestowitz/)
 * (@schestowitz)
 * [20 years, 5 months ago](https://wordpress.org/support/topic/calling-global-variables-into-a-function-in-headerphp/#post-301130)
 * 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.
 *  Thread Starter [budoink](https://wordpress.org/support/users/budoink/)
 * (@budoink)
 * [20 years, 5 months ago](https://wordpress.org/support/topic/calling-global-variables-into-a-function-in-headerphp/#post-301436)
 * 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
 *  Thread Starter [budoink](https://wordpress.org/support/users/budoink/)
 * (@budoink)
 * [20 years, 4 months ago](https://wordpress.org/support/topic/calling-global-variables-into-a-function-in-headerphp/#post-301667)
 * I know bumps are annoying, but does anyone have any ideas whatsoever? Im still
   stumped 🙁
 * Thankyou in advance
 *  Thread Starter [budoink](https://wordpress.org/support/users/budoink/)
 * (@budoink)
 * [20 years, 4 months ago](https://wordpress.org/support/topic/calling-global-variables-into-a-function-in-headerphp/#post-301669)
 * 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.

## Tags

 * [call](https://wordpress.org/support/topic-tag/call/)
 * [calling](https://wordpress.org/support/topic-tag/calling/)
 * [function](https://wordpress.org/support/topic-tag/function/)
 * [global](https://wordpress.org/support/topic-tag/global/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [variables](https://wordpress.org/support/topic-tag/variables/)

 * In: [Everything else WordPress](https://wordpress.org/support/forum/miscellaneous/)
 * 4 replies
 * 2 participants
 * Last reply from: [budoink](https://wordpress.org/support/users/budoink/)
 * Last activity: [20 years, 4 months ago](https://wordpress.org/support/topic/calling-global-variables-into-a-function-in-headerphp/#post-301669)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
