Jump to content

PHP str_pad Problem


cdikland

Recommended Posts

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.

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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....

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.