Jump to content

Pass value from one function to another


jarvis

Recommended Posts

Hi,

 

Apologies if this is a basic question but either I've misunderstood or I'm being daft 

 

In one function I have:

function create_coupon() {
	$unique_coupon_code = $coupon_name.'-'.$date; #concatenate the two
	return $unique_coupon_code;
}

Then in my second function I try to get that variable ($unique_coupon_code) to use again:

function use_coupon() {
	echo $unique_coupon_code;
}

But it returns blank?

 

However, if I echo $unique_coupon_code; in the first function, it does show a value

 

What am I doing wrong?

Thanks

Link to comment
Share on other sites

$unique_coupon_code only exists in the create_coupon() function (and only after it gets defined). Variables in one function do not magically appear in other functions - it would create havoc.

 

To get the value, call the function and use its return value.

$coupon = create_coupon();
echo $coupon; // or simply  echo create_coupon();
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.