How can I start a for-loop with 01 instead of 1?

前端 未结 5 1539
面向向阳花
面向向阳花 2021-01-14 05:17

How do I start a for-loop with 01 as opposed to 1? I\'ve tried the below, but it doesn\'t seem to work.

for ($i = 01; $i <= 12;          


        
5条回答
  •  一生所求
    2021-01-14 05:41

    You can't really start an integer at 01, you will need to pad the value, probably using str_pad to prefix leading elements to a string:

    $value = $i;
    if ($i < 10) {
        $value = str_pad($i, 2, "0", STR_PAD_LEFT);
    }
    

    Note that for different unit types you will obviously need to alter the desired pad_length.

提交回复
热议问题