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
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?

Link to comment
Share on other sites

Here is a slimmed down example, please try

 

<?

function first() {
$beans = 50;
second();
}

function second() {
global $beans;
echo $beans;
echo "The function works, but did it echo 50 above?";
}


first();
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.