ngreenwood6 Posted May 12, 2009 Share Posted May 12, 2009 I have a quick question that I am confused about. I am trying to set a variable inside of a function and make it stick within that page. Here is an example: <?php $my_data = "not there"; this_data($my_data); function this_data($data) { $data = "hello"; my_data($data); } function my_data($data) { $my_data = $data; } echo $my_data; ?> However I want $my_data to return "hello" instead of "not there". Any help is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/157883-function-question/ Share on other sites More sharing options...
cunoodle2 Posted May 12, 2009 Share Posted May 12, 2009 It will stay within the function unless you return something. You can always return an array if you want to have more values sent out. <?php $my_data = "not there"; $my_data = this_data($my_data); function this_data($data) { $data = "hello"; return my_data($data); } function my_data($data) { $my_data = $data; return $my_data; } echo $my_data; ?> I changed about 4 lines of code in there. Let me know if that works for you. Quote Link to comment https://forums.phpfreaks.com/topic/157883-function-question/#findComment-832804 Share on other sites More sharing options...
ToonMariner Posted May 12, 2009 Share Posted May 12, 2009 its all about scope... Quote Link to comment https://forums.phpfreaks.com/topic/157883-function-question/#findComment-832806 Share on other sites More sharing options...
ngreenwood6 Posted May 12, 2009 Author Share Posted May 12, 2009 Sorry but I dont think I gave enough information. The this_data() function I am not able to run. However when this runs I want to be able to get the data variable contains before it is finished running that function. I want it to be accessible throughout the whole page. Something like this: <?php $my_data = "hello"; function this_data($data) { $some_data = "world"; // need this before it is run get_data($data); $some_data = "not now"; } function get_data($data) { $my_data = $data; } echo $my_data; // should be world Quote Link to comment https://forums.phpfreaks.com/topic/157883-function-question/#findComment-832810 Share on other sites More sharing options...
ngreenwood6 Posted May 12, 2009 Author Share Posted May 12, 2009 Someone has to know how I can declare a variable inside of a function and make it available outside of the function! Quote Link to comment https://forums.phpfreaks.com/topic/157883-function-question/#findComment-832851 Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 Why would you do that? Quote Link to comment https://forums.phpfreaks.com/topic/157883-function-question/#findComment-832853 Share on other sites More sharing options...
Philip Posted May 12, 2009 Share Posted May 12, 2009 Use of global, see Toon's post. Quote Link to comment https://forums.phpfreaks.com/topic/157883-function-question/#findComment-832854 Share on other sites More sharing options...
gevans Posted May 12, 2009 Share Posted May 12, 2009 <?php function this_data($data) { $some_data = "world"; // need this before it is run return $some_data = "not now"; } $my_data = this_data('a pointless string'); echo $my_data; // should be world As KingPhilip said you want to use a global, or you could pass a variable as reference. Quote Link to comment https://forums.phpfreaks.com/topic/157883-function-question/#findComment-832857 Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 No, it should be "not now" Quote Link to comment https://forums.phpfreaks.com/topic/157883-function-question/#findComment-832859 Share on other sites More sharing options...
ngreenwood6 Posted May 12, 2009 Author Share Posted May 12, 2009 Ok I dont think any of you are actually reading the post. I do not have access to the function. It is being called from another page and I do not want to mess with that function. However there is a variable that is used inside of there that I want to use somewhere else on that page. I do not get the globals thing. I tried it and it doesnt seem to work. $my_string = "hello"; function this_data() { $my_data = "world"; global $my_string; $my_string = $my_data; } echo $my_string; // should be world Still is outputting "hello" Quote Link to comment https://forums.phpfreaks.com/topic/157883-function-question/#findComment-832861 Share on other sites More sharing options...
gevans Posted May 12, 2009 Share Posted May 12, 2009 No, it should be "not now" I didn't change his comments, I'm not sure he understand what he wants anyway. And yes, we are reading your posts, if you read some of ours you would've come up with this solution already, in actual fact you, have, you're just not calling the function..... Quote Link to comment https://forums.phpfreaks.com/topic/157883-function-question/#findComment-832863 Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 You never called the function. :-\ Edit - beat by gevans. Quote Link to comment https://forums.phpfreaks.com/topic/157883-function-question/#findComment-832864 Share on other sites More sharing options...
ngreenwood6 Posted May 13, 2009 Author Share Posted May 13, 2009 thats what your not understanding I am not calling that function. It is getting called on another page by something else. I just want to use the variable on the page that the function resides on. Quote Link to comment https://forums.phpfreaks.com/topic/157883-function-question/#findComment-832868 Share on other sites More sharing options...
Ken2k7 Posted May 13, 2009 Share Posted May 13, 2009 Well then if it's called by another page, what's the problem? In your example, it's never called. If some other page calls, it, I think it should be fine. Quote Link to comment https://forums.phpfreaks.com/topic/157883-function-question/#findComment-832869 Share on other sites More sharing options...
gevans Posted May 13, 2009 Share Posted May 13, 2009 Make sure you're calling it before you echo the variable, otherwise it obviously wont have been changed. That's like driving to a gas station and asking why the tank is empty before you fill it up, think about it! Quote Link to comment https://forums.phpfreaks.com/topic/157883-function-question/#findComment-832872 Share on other sites More sharing options...
Ken2k7 Posted May 13, 2009 Share Posted May 13, 2009 That's like driving to a gas station and asking why the tank is empty before you fill it up, think about it! That was horrible. I think you screwed up what you were trying to say. Quote Link to comment https://forums.phpfreaks.com/topic/157883-function-question/#findComment-832873 Share on other sites More sharing options...
gevans Posted May 13, 2009 Share Posted May 13, 2009 hahaha, should've read.... That's like driving to a gas station and asking why the tank isn't full before you fill it up Quote Link to comment https://forums.phpfreaks.com/topic/157883-function-question/#findComment-832876 Share on other sites More sharing options...
Ken2k7 Posted May 13, 2009 Share Posted May 13, 2009 Still not the same. It has to have the "you created it but you never used it" kind of thing. It's like getting into your car, stepping on the gas pedal with no keys. It just doesn't happen by itself. Quote Link to comment https://forums.phpfreaks.com/topic/157883-function-question/#findComment-832881 Share on other sites More sharing options...
allworknoplay Posted May 13, 2009 Share Posted May 13, 2009 lol you guys are cruel.... Quote Link to comment https://forums.phpfreaks.com/topic/157883-function-question/#findComment-832883 Share on other sites More sharing options...
gevans Posted May 13, 2009 Share Posted May 13, 2009 Actually, if you go by original comment Make sure you're calling it before you echo the variable, otherwise it obviously wont have been changed. What I was saying is you have a starting point and an end point. You can't expect the end point to have happened before you change anything (before you call the function, before you fill up your car). The analogy stands Quote Link to comment https://forums.phpfreaks.com/topic/157883-function-question/#findComment-832885 Share on other sites More sharing options...
Ken2k7 Posted May 13, 2009 Share Posted May 13, 2009 lol you guys are cruel.... Sorry ngreenwood6. Quote Link to comment https://forums.phpfreaks.com/topic/157883-function-question/#findComment-832887 Share on other sites More sharing options...
ngreenwood6 Posted May 13, 2009 Author Share Posted May 13, 2009 I am not echoing the variable and yes I am making sure that I am assigning it before actually using it. Quote Link to comment https://forums.phpfreaks.com/topic/157883-function-question/#findComment-832888 Share on other sites More sharing options...
Ken2k7 Posted May 13, 2009 Share Posted May 13, 2009 Is this related to the other topic about the email thing? If so, I can seriously help you out, but I need to know the original PM message. Quote Link to comment https://forums.phpfreaks.com/topic/157883-function-question/#findComment-832890 Share on other sites More sharing options...
ngreenwood6 Posted May 13, 2009 Author Share Posted May 13, 2009 Yeah this is related to the other topic. Here is the email: Hello nick, the topic "testt" at "mysite" has received a new reply since your last visit. You can view this post by clicking on the following link: http://mysite.com/forums/viewtopic.php?f=85&t=621&p=3078&e=3078 Topic: testt Author: Rukus Message: If you want to view the topic, click the following link: http://mysite.com/forums/viewtopic.php?f=85&t=621 If you no longer wish to watch this topic, either click the "Unsubscribe topic" link found in the forum above, or click the following link: http://mysite.com/forums/viewtopic.php?uid=53&f=85&t=621&unwatch=topic -- Thanks, The Team Quote Link to comment https://forums.phpfreaks.com/topic/157883-function-question/#findComment-832895 Share on other sites More sharing options...
Ken2k7 Posted May 13, 2009 Share Posted May 13, 2009 Just so we're on the same page, are you viewing functions_postings.php? And also, what phpBB version are you working with? Quote Link to comment https://forums.phpfreaks.com/topic/157883-function-question/#findComment-832904 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.