Jump to content

Make variable value output of function


lspiehler

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/243527-make-variable-value-output-of-function/
Share on other sites

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);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.