Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
返回php中的表值_Php_For Loop_Return - Fatal编程技术网

返回php中的表值

返回php中的表值,php,for-loop,return,Php,For Loop,Return,我有以下代码: public function getYears() { for($yearNum = 1; $yearNum <= 12; $yearNum++){ $year[]=$this->year; echo $year[$yearNum]=$yearNum; } return $yearNum; } 公共函数getYears() { 对于($eargerum=1;$eargerum-year; echo$ye

我有以下代码:

public function getYears()
{

     for($yearNum = 1; $yearNum <= 12; $yearNum++){
         $year[]=$this->year;
        echo $year[$yearNum]=$yearNum;
    }
    return $yearNum;
}
公共函数getYears()
{
对于($eargerum=1;$eargerum-year;
echo$year[$earAngum]=$earAngum;
}
返回$um;
}
我有一个错误: PHP警告–yii\base\ErrorException

为foreach()提供的参数无效

如何返回这12个数字?

根据您的评论:

我只需要12个没有0的数字(1,2,3,4,5,6,7,8,9,10,11,12)

使用此选项可以获得12个数字:

public function getYears()
{
    for($yearNum = 1; $yearNum <= 12; $yearNum++){
        //$year[] = $yearNum;
        $year[] = $yearNum;
        //echo $year[$yearNum]=$yearNum;
    }
    return $year;
}

// calling function
$result = $this->getYears();
//echo "<pre>";
$numbers = implode(",", $result);
echo $numbers; //1,2,3,4,5,6,7,8,9,10,11,12
公共函数getYears()
{
对于($eargerum=1;$eargerum getYears();
//回声“;
$numbers=内爆(“,”,$result);
echo$numbers;//1,2,3,4,5,6,7,8,9,10,11,12

heh现在是月,但我写years我这样做:
code
public function getMonths(){for($monthNum=1;$monthNum抱歉,我现在的第一个返回值中有“0”了,有人知道如何解决这个问题吗?我只需要12个数字(1,2,3,4,5,6,7,8,9,10,11,12)而没有0,$this->year是什么?