Getting stuff out of a function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Pezmc
Forum Commoner
Posts: 53
Joined: Mon Nov 06, 2006 2:15 pm

Getting stuff out of a function

Post by Pezmc »

I am not a newbie I have just never figured out how to do this (taught my self php).
I have a function say

Code: Select all

function socks(test,sock) {
echo $test
echo "I am a fary:"
echo $sock

$total = $test * $sock;

}
How can I get the total out of the function to use in other places as a variable, is there a returning function or something?
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

Code: Select all

function multiply($x, $y)
{
   return $x * $y ;
}

$result = multiply(10, 9) ;
You are not a newbie and didn't know that? Good lord, if you have been writing a ton of code without using function returns then you are one hard core dude.

Perhaps a book like Learing PHP from O'Reilly press would be a good idea. Just spend 15 minutes a day with it on the toilet and you will learn a lot of stuff that will make your coding life a lot easier.
Last edited by Begby on Fri Mar 30, 2007 8:23 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The keyword "return"...
Pezmc
Forum Commoner
Posts: 53
Joined: Mon Nov 06, 2006 2:15 pm

Post by Pezmc »

Begby wrote:

Code: Select all

function multiply($x, $y)
{
   return $x * $y ;
}

$result = multiply(10, 9) ;
You are not a newbie and didn't know that? Good lord, if you have been writing a ton of code without using function returns then you are one hard core dude.

Perhaps a book like Learing PHP from O'Reilly press would be a good idea. Just spend 15 minutes a day with it on the toilet and you will learn a lot of stuff that will make your coding life a lot easier.
Lol thanks yeh I have mainly been using c&p for all of the functions. I have written quite a few website with out it. Is it possible to use

Code: Select all

return $socks, $dogs, $etc;
or do I need to do

Code: Select all

return $socks; 
return $dogs;
return $etc;
Thanks for your help this will make my life alot easier in the future
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

You can only return one thing at a time. As soon as you hit the first return the function is immediately exited with no other code in that function executed.

To return multiple items you will either need to return an array, return an object, or pass parameters to the function by reference and change their values.

Edit: Haha, I type quicker than Feyd
|
|
V
Last edited by Begby on Fri Mar 30, 2007 8:45 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Neither are usable. To return multiple values you use an array. Functions can only return one thing. That one thing may contain other things, but the function itself can only return one thing.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

As has been said, functions can only return one thing (eg an array), but you can work around that with list()..

Code: Select all

function myFunction() {

   $item = "Lawnmower";
   $make = "Honda";
   $color = "Red";

   return array($item,$make,$color);

}

list($item,$make,$color) = myFunction();

echo "I want a ".$color." ".$make." ".$item;
Silly example because you'd be better off with an object in this case, but it demonstrates using list().
Post Reply