Global Variable not working

Team… New to Php sorry but I have a basic handle on it…
I do a call to a table and get the output and put into an array, I display the values using the echo and they are displayed correctly, yet when I go into another section / paragraph the value is lost. Here is the global definition and how Ive populated it, along with the next display.

The global variable is $GIDNUMBER

Appreciate your help.

global $CFG, $PAGE, $DB; $GIDNUMBER;

echo 3. id is $studentid;
echo , 4. id is $idnumber;
echo , 5. Gidnumber is $GIDNUMBER;

Values here are correct. Yet when I do an update of a email address in the section and display the value, it is lost.

	if ($action)
		if ($action=="update_name")
			// update the name, email address and username on the user table
			if ($firstname && $lastname && $username && $email)
				$record = new stdClass();
				$record->id = "$studentid";
				$record->firstname = "$firstname";
				$record->lastname = "$lastname";
				$record->email = "$email";
				$record->username = "$username";
				echo "1. id is $studentid";
				echo ", 2. id is $idnumber";
				echo ", 6. Gidnumber b4 update_record is $GIDNUMBER";     
				$DB->update_record('user', $record, false);
				$DB->update_record('user', $record, false);

I would use the following $GLOBALS['GIDNUMBER']

or if you have a function like this, you first have to get the variable with the global function into your local function.

function myFunction() {
  global $GIDNUMBER; // need to call this when you want to access it directly in function

  echo $GIDNUMBER;

All the best