Title: Global variable not available in function
Last modified: August 19, 2016

---

# Global variable not available in function

 *  [vanderhavecn](https://wordpress.org/support/users/vanderhavecn/)
 * (@vanderhavecn)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/global-variable-not-available-in-function/)
 * I’m very new to PHP programming, so excuse me for asking this (most likely) dumb
   question.
    I’ve added a new page to my WordPress by creating a template. This
   template contains code for retrieving the logged-in user’s ID and name. This 
   information needs to be emailed.
 * So in the WP template I have this;
 * `
    // retreive information about this user GLOBAL $fb_currentUserID; GLOBAL $
   fb_currentName;
 * $fb_currentUserID = get_current_user_id();
    echo ($fb_currentUserID);
 * $fb_currentNameQuery = mysql_query("SELECT `user_nicename` FROM `wp_users` WHERE`
   ID` = “.$fb_currentUserID.””);
    $fb_currentNameArray = mysql_fetch_array($fb_currentNameQuery);
   $fb_currentName = $fb_currentNameArray[‘user_nicename’]; echo ($fb_currentName);
 * These echo’s ($fb_currentName and $fb_currentUserID) show up just fine. But when
   I use them in this function (a little further down in the code), nothing is displayed.
 * `
    function mailInvitation($email, $pass) { $subject = "You're invited by".$fb_currentName."";
   echo ($subject); $body = "Hi,\n\nTo register your account, please surf to http://
   localhost/invite?pass=".$pass.""; if (mail($email, $subject, $body)) { echo("
   <p>Message successfully sent!</p>"); } else { echo("<p>Message delivery failed...
   </p>"); } }
 * Here the $subject only shows ‘You’re invited by’. It doesn’t want to print the
   $fb_currentName even though I thought to have it setup as a global variable.
 * Can anyone tell me what I’m doing wrong?

The topic ‘Global variable not available in function’ is closed to new replies.

## Tags

 * [function](https://wordpress.org/support/topic-tag/function/)
 * [variable](https://wordpress.org/support/topic-tag/variable/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 0 replies
 * 1 participant
 * Last reply from: [vanderhavecn](https://wordpress.org/support/users/vanderhavecn/)
 * Last activity: [15 years, 1 month ago](https://wordpress.org/support/topic/global-variable-not-available-in-function/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
