Problems with older php version

I have one application that runs in PHP 5.6.40-38 environment with the code as seen below:

    $yearsCounter = array('0'=> '2022', '1' => '2021' ,'2' => '2020' ,'3' => '2019', '4'=> '2018' , '5' => '2017', '6' => '2016' );

    for($j=0; $j<sizeof($yearsCounter); $j++)
    {
        $sql = $sql . $this->actionExportToCSVPart($yearsCounter[$j]);     
    }

In that version 5.6.40-38,of the application works and it manipulates the $sql, but in a same application that runs in PHP version, 5.6.40-39 it does not. It does not reproduce $sql’s. I mean all I want is to fill the $sql with all possible options according to the array

The question:

How can I do the same with php 5.6.40-39. I mean how can I create separate " $sql’s ", as I do in PHP 5.6.40-38 according to the values of $yearsCounter, array and reproduce the extra $sql’s in order to build and run the final query.

  $results = Yii::app()->db->createCommand($sql)