Jump to content

Leading zero's


iamtom

Recommended Posts

Thnaks in advance for any pointers...

I have a data filed that is 4 characters long,

It is set to hold 4 numbers  XXXX

I and using it to hold the last 4 of credit card number , $last4 = substr($Ccrd, -4, 4);

My problem is when any on the first characters are 0's it does not show them, so I get " 145" when I should get "0145". 

How can I prevent this and show the leading 0 ?

 

Thanks

Tom

Link to comment
https://forums.phpfreaks.com/topic/132942-leading-zeros/
Share on other sites

Store the number as a string instead of a number

 

$number = 0055;
echo $number;//prints 55

$number = "0055";
echo $number;//prints 0055

<?php
$Ccrd="1234123412340001";
$last4 = settype(substr($Ccrd, -4, 4), "string");
echo $last4;
?>

 

too the last bit of code from ddrudik, just need to set it as a string

Link to comment
https://forums.phpfreaks.com/topic/132942-leading-zeros/#findComment-691339
Share on other sites

He means it's 145 in the database....

 

I see where everyone's going with the question, it's just that the code and output shown in the question do not match with a string or number designation, consider:

<pre>
<?php
$Ccrd="1234123412340145";
$last4 = substr($Ccrd, -4, 4);
echo "[$last4]<br>";
$Ccrd=1234123412340145;
$last4 = substr($Ccrd, -4, 4);
echo "[$last4]";
?>

output:

[0145]
[0100]

Link to comment
https://forums.phpfreaks.com/topic/132942-leading-zeros/#findComment-691507
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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