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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.