Returning an Array from 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
webcan
Forum Commoner
Posts: 66
Joined: Tue Oct 28, 2003 2:25 pm
Location: Toronto, Canada

Returning an Array from a Function

Post by webcan »

Would someone be kind enough to post a simple/minimal example of how a function, my_function, would return an array of two strings, say "abc" and "def" (versus returning a single value).

I've looked in O'reilly's PHP book on page 339 (for those that have it) but I can't get it to work, I suspect because the example isn't a real example.

Thanks,
Peter.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

return array('abc','def');
webcan
Forum Commoner
Posts: 66
Joined: Tue Oct 28, 2003 2:25 pm
Location: Toronto, Canada

Post by webcan »

Wow, that is totally easy. :) So unlike the example in the book.

Ok, one last question, let's say I already have an array $data that I populate elsewhere in the function, how do I return it? return($data)?

Thanks,
Peter.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

FYI - you only need the parentheses if the data contains an expression, which an array does not.


return $data;
webcan
Forum Commoner
Posts: 66
Joined: Tue Oct 28, 2003 2:25 pm
Location: Toronto, Canada

Post by webcan »

OK, thanks.
Post Reply