satbir Posted November 27, 2017 Share Posted November 27, 2017 I need for loop to display images....but image name must start with 00... like 001.jpg, 002.jpg.I write my code like this..for ($i = 001; $i <= 10; $i++) { echo "<img src='Ony/Ony-Small/'.str_pad($i, 3, '0', STR_PAD_LEFT);.'.jpg' class='img-responsive mx-auto d-block center-block img-thumbnail'>"; }..but this is not working..can anybody suggest me the right way Quote Link to comment https://forums.phpfreaks.com/topic/305774-starting-numbers-in-for-loop-in-php-with-00-characters/ Share on other sites More sharing options...
Solution Barand Posted November 27, 2017 Solution Share Posted November 27, 2017 sprintf() EG for ($i=1; $i<=10; $i++) { echo sprintf("%03d.jpg", $i) . '<br>'; } 1 Quote Link to comment https://forums.phpfreaks.com/topic/305774-starting-numbers-in-for-loop-in-php-with-00-characters/#findComment-1554164 Share on other sites More sharing options...
satbir Posted November 27, 2017 Author Share Posted November 27, 2017 Thanks Working Well Quote Link to comment https://forums.phpfreaks.com/topic/305774-starting-numbers-in-for-loop-in-php-with-00-characters/#findComment-1554166 Share on other sites More sharing options...
benanamen Posted November 27, 2017 Share Posted November 27, 2017 (edited) Alternate solution <?php foreach (range(0,10) as $d) { echo "<img src='Ony/Ony-Small/".sprintf('%02d', $d).".jpg' class='img-responsive mx-auto d-block center-block img-thumbnail'>\n"; } ?> Edited November 27, 2017 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/305774-starting-numbers-in-for-loop-in-php-with-00-characters/#findComment-1554175 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.