开发者

Return an array from PHP function to Smarty

开发者 https://www.devze.com 2023-02-09 21:45 出处:网络
At the risk of self-embarrassment, could anyone tell me how to use return here: function dayCount() { for ($dayBegin = 1; $dayBegin < 32; $dayBegin++)

At the risk of self-embarrassment, could anyone tell me how to use return here:

function dayCount() {

    for ($dayBegin = 1; $dayBegin < 32; $dayBegin++)
    {
      "<option value=\"".$dayBegin."\">".$dayBegin."</option>";
    }
}

My issue is that I am passing this function to Smarty via

$dayCount = dayCount();
$smarty->assign('dayCount', $dayCount);

and

{$dayCount}

but instead the HTM开发者_如何学JAVAL is going straight to the buffer, right before <html> (thanks Hamish), not inside the HTML element I want.

Any help on this?


You need to build up the return statement

function dayCount() {
    $return = array();
    for ($dayBegin = 1; $dayBegin < 32; $dayBegin++)
    {
      $return[] = "<option value=\"".$dayBegin."\">".$dayBegin."</option>";
    }
    return $return;
}

Although this is building up an array like you asked. When outputting it, you would need to implode it.

implode('', $dayCount);

Or otherwise, build up a string instead of an array.

0

精彩评论

暂无评论...
验证码 换一张
取 消