Is it possible to loop with "0" on the left in PHP?

Asked

Viewed 46 times

0

Dear Devs, I’m trying to use a list with zero left using for and str_pad, but I’m not getting the variable with 0 left, the first variable comes complete but in the autoincrement it’s counting without the digits, could you help? The code is currently as follows:

<?php
for($x = str_pad(1, 4, 0, STR_PAD_LEFT); $x < str_pad(1000, 4, 0, STR_PAD_LEFT); ++$x) {
    echo "O X é: ".$x."<br/>";}

  • It’s very confusing. PHP doesn’t have auto increment, so it’s involving databases, but the question doesn’t talk about it at all. Auto increment is number, so there’s no point in having zeros on the left. If you want to do other things you need to specify in detail, it’s probably not even to do what you want. Or it is, but the context is another.

1 answer

0


Try to change the logic, make a loop from 1 to 1000 (as is already being done, without the str_pad) and in your echo the text is formatted.

<?php
for($x = 1; $x < 1000; ++$x) {
    echo "O X é: ".str_pad($x, 4, 0, STR_PAD_LEFT)."<br>";
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.