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

前端 未结 5 1543
面向向阳花
面向向阳花 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:26

    01 is the octal number 1 (which is equivalent to the decimal 1 in this case). Since you want to format the output to have two digits for the number, consider using printf:

    printf("
    • % marks the start of a conversion
    • 0 means "pad the string with zero"
    • 2 means "the replacement should have a minimum length of 2"
    • d means "the argument is an integer"

    References:

    • PHP Manual: printf
    • PHP Manual: sprintf

提交回复
热议问题