0

有没有办法重复一个字符串任意次数?我有这段代码,我觉得应该有一种更简单的方法。

    $vitals = parent::userVitals($uid);
    $hearts = round($vitals['health']/25);
    /* with our powers combined! */
    if($hearts == 4)
    {
        $health = "♥♥♥♥";
    }
    elseif($hearts == 3)
    {
        $health = "♥♥♥";

    }
    elseif($hearts == 2)
    {
        $health = "♥♥";
    }
    elseif($hearts == 1)
    {
        $health = "♥";
    }
    return $health;
4

2 回答 2

4
$health = str_repeat('♥', $hearts);

http://php.net/manual/en/function.str-repeat.php

PHP 充满了像这样的随机函数。无需重新发明轮子。

于 2013-08-03T05:07:11.033 回答
3

试试for-loop喜欢

$vitals = parent::userVitals($uid);
$hearts = round($vitals['health']/25);

$health = "";
for( $i=0 ; $i < $hearts ; $i++) {
   $health .= "&hearts;";
}
于 2013-08-03T05:05:54.400 回答