Jump to content

[SOLVED] global not working


jaymc

Recommended Posts

For some reason when executing a function which uses global $variable_name the $variable_name is empty

 

 

	echo $clientID; // Outputs the value of $clientID fine

credits_add("1", "6", "FREE", "0.00"); // Does not output $clientID

echo $clientID; // Outputs the value of $clientID fine

 

credits_add() function is shown below

 

	function credits_add($userID, $credits, $paymentType, $rate) {

	global $clientID;

echo $clientID;
}

 

 

Link to comment
https://forums.phpfreaks.com/topic/164016-solved-global-not-working/
Share on other sites

Its not just that function. Here is a brief layout of my code

 


<?
include_once(HOMEDIR."funcs/random_password.php");
include_once(HOMEDIR."funcs/copy_dbStructure.php");
include_once(HOMEDIR."funcs/credits_add.php");
include_once(HOMEDIR."funcs/credits_transfer.php");


function create_client() {

	global $my, $mobile, $email, $companyName, $firstName, $lastName;
	$clientID = 12;

	credits_transfer(); // using this as an example. see below for its contents
}

 

Here is the test function

	function credits_transfer() {
		global $clientID;
			echo $clientID
	}	

 

Its not because I have already called global inside the base function because I have tested without

 

Then only thing I can think is maybe because im executing a function within a function it loses its right to use global?

A) You need to stop using global to pass parameters into functions, and

B) $clientID is not a global variable. It is a local variable that only exists in the create_client() function.

 

A) Sometimes it makes its better to just use global

B) But surely using global $clientID; gives the function access to its contents?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.