Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - list number with url

Tags:

php

numbers

example php like this:

  for ($i=1; $i<=20; $i++){
    $url = "http://example.org/12".$i."<br />";
    echo $url;
  }

i want output :

http://example.org/1201
http://example.org/1202
http://example.org/1203
http://example.org/1204
http://example.org/1205
http://example.org/1206
http://example.org/1207
http://example.org/1208
http://example.org/1209
http://example.org/1210
http://example.org/1211
http://example.org/1212
.......
http://example.org/1220

Thanks for everybody who can help me :D

like image 753
james bond Avatar asked Aug 23 '26 10:08

james bond


1 Answers

You can use str_pad()

for ($i=1; $i<=20; $i++){
    $i = str_pad($i, 2, "0", STR_PAD_LEFT);
    $url = "http://example.org/12".$i."<br />";
    echo $url;
}

You can also use sprintf()

for ($i=1; $i<=20; $i++){
    $i = sprintf("%02d",$i);
    $url = "http://example.org/12".$i."<br />";
    echo $url;
}
like image 118
John Conde Avatar answered Aug 25 '26 00:08

John Conde