jarvis Posted January 9, 2017 Share Posted January 9, 2017 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 Quote Link to comment https://forums.phpfreaks.com/topic/302896-pass-value-from-one-function-to-another/ Share on other sites More sharing options...
requinix Posted January 9, 2017 Share Posted January 9, 2017 $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(); Quote Link to comment https://forums.phpfreaks.com/topic/302896-pass-value-from-one-function-to-another/#findComment-1541210 Share on other sites More sharing options...
jarvis Posted January 9, 2017 Author Share Posted January 9, 2017 I sussed it as soon as I clicked submit! My apologies but thank you once again for your time and assistance! Quote Link to comment https://forums.phpfreaks.com/topic/302896-pass-value-from-one-function-to-another/#findComment-1541212 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.