Jump to content

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

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.