lspiehler Posted August 1, 2011 Share Posted August 1, 2011 Why won't $assemblelist take the output of function this()? As you can see below, in my actual function, I need to be able to remove the last comma. <?php $list = array('apples', 'pears', 'bananas', 'oranges'); function this($array) { foreach($array as $fruits) { print "I like to eat ".$fruits.","; } } $assemblelist = this($list); $formattedlist = substr('$assemblelist', 0, -1); ?> Thank you for any help! -Lyas Quote Link to comment https://forums.phpfreaks.com/topic/243527-make-variable-value-output-of-function/ Share on other sites More sharing options...
Alex Posted August 1, 2011 Share Posted August 1, 2011 You have a few problems. First, your function is outputting the text, not returning it. Second, '$assemblelist' is a literal string, not a variable. To simplify things, you can just have the function return it in the format you want. Something like this: function this($array) { $str = ''; foreach($array as $fruits) { $str .= "I like to eat ".$fruits.","; } return substr($str, 0, -1); } $assemblelist = this($list); print_r($assemblelist); Quote Link to comment https://forums.phpfreaks.com/topic/243527-make-variable-value-output-of-function/#findComment-1250463 Share on other sites More sharing options...
lspiehler Posted August 2, 2011 Author Share Posted August 2, 2011 Thanks for the education on functions. I've got what I need, and thanks for always being so fast. You guys are great! -Lyas Quote Link to comment https://forums.phpfreaks.com/topic/243527-make-variable-value-output-of-function/#findComment-1250548 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.