cdikland Posted October 18, 2007 Share Posted October 18, 2007 I have a very perculiar problem with the str_pad function. In particular the pad_len. If this number is greater than 4 it wont work. EXAMPLE: $headerID=3; $paddedHeaderID=str_pad($headerID, 4, "0", STR_PAD_LEFT); // results in 0003 as expected HOWEVER... $paddedHeaderID=str_pad($headerID, 6, "0", STR_PAD_LEFT); // results in 000000 AND the value of $headerID is set to zero ??? What am I doing wrong. Quote Link to comment https://forums.phpfreaks.com/topic/73821-php-str_pad-problem/ Share on other sites More sharing options...
kenrbnsn Posted October 18, 2007 Share Posted October 18, 2007 I just tried your code, it seems to work for me in PHP 4.4.4 How are you determining the contents of $paddedHeaderID? You can also use the sprintf() function to produce padded strings: <?php $paddedHeaderID = sprintf("%06d",$headerID); echo $paddedHeaderID; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/73821-php-str_pad-problem/#findComment-372458 Share on other sites More sharing options...
cdikland Posted October 19, 2007 Author Share Posted October 19, 2007 I just tried your code, it seems to work for me in PHP 4.4.4 How are you determining the contents of $paddedHeaderID? You can also use the sprintf() function to produce padded strings: <?php $paddedHeaderID = sprintf("%06d",$headerID); echo $paddedHeaderID; ?> Ken I was running it under version 5.2 AND it did not work yesterday ??? (I was using echo $paddedHeaderID?). Since then I rebooted my server AND wouldnt you know it... It works... I spent a lot of time on it yesterday even though I had an alternative already working. I was curious why the str_pad did not work even though there appears nothing in the documentation about this "limitation". Why it works now, after the reboot is beyond me. ??? Oh well.... Quote Link to comment https://forums.phpfreaks.com/topic/73821-php-str_pad-problem/#findComment-373106 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.