I made my first function today. But it's not working properly. Why can't I access the variables outside the function from the function after calling the function? Here's an example: function subsinfos(){ $var = "Hello"; return $var; } PHP: Now if I call up the function like this: subsinfos(); PHP: How can I access the $var that was returned from the function? It doesn't work if I do this: subsinfos(); echo $var; PHP: Why? What's the use of return then? What am I missing?
return is the variable that the function returns, you could kinda call it like being a variable. Let me explain that further =) if you call your function subsinfos(); Code (markup): That would result in the 'variable' subsinfos() having the value you return within the function. so if you'd use either echo subsinfos(); Code (markup): or $newvar = subsinfos(); //do something with the var, or don't echo $newvar; Code (markup): it would work like a charm =) Be sure to check php.net or read this tutorial: http://tizag.com/phpT/ Code (markup): that tutorial is really helpful for beginners ^^! edit: sorry for the link not being clickable and you having to copy-paste it, the restrictions eh...
Oh. Thanks, but what if there are more variables returned? How would I pull them? return $var2, $var2, var3; Either of your examples only pulls $var2... The only way I managed to pull them all was to put them all in an array and return that array, but there must be a easier way.
you can only return one value from any function but u can return more then one value in the fucntion if u use return value as array or use the call by reference parameters like //normal function returnning only one value function subsinfos(){ $var = "Hello"; return $var; } $value=subsinfos(); // function with call by refrence function subsinfos_r(&$var1,&$var2,&$var3) { $var1 ="value 1"; $var2 ="value 2"; $var3 ="value 3"; } $somevar1=""; $somevar2=""; $somevar3=""; subsinfos_r($somevar1,$somevar2,$somevar3); // now if u will echo these values u can see there values are now changed.now they contains value which we assigned in function echo($somevar1); echo($somevar2); echo($somevar3); // by this way u can return more then 1 values without using arrays
unfortunately using an array or the ByRef parameters are the easiest ways... If you have a function that really needs to return multiple values think about how closely related the values are and splitting it up into multiple functions and arrays. For example the info of one day such as: temperature, rainfall, sun hours, amount of traffic jams, traffic jam lengt, etc... I'd split it in a traffic array and a wheather array and have two functions, one for traffic and one for the wheather which both return an array of values. Hope I helped and gave you some ideas =)
You can also make a variable global and it can be accessed from outside the function if remember right. global $varname; //define a global variable PHP:
Isn't the global command used to make variables outside the function accessible within the function not vice versa?
eh, you shouldn't really use globals that way. You can try this: list($a, $b, $c) = func(); Where func is: function func() { return array(1, 2, 3); } Then you can simply use $a, $b, $c. Or just do: $returned = func(); and then $returned[0] etc.