iamtom Posted November 16, 2008 Share Posted November 16, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/132942-leading-zeros/ Share on other sites More sharing options...
ddrudik Posted November 16, 2008 Share Posted November 16, 2008 That doesn't seem to follow with how strings work, consider this code: <?php $Ccrd="1234123412340001"; $last4 = substr($Ccrd, -4, 4); echo $last4; ?> output: 0001 Possibly show the code where you assign $Ccrd. Quote Link to comment https://forums.phpfreaks.com/topic/132942-leading-zeros/#findComment-691333 Share on other sites More sharing options...
corbin Posted November 16, 2008 Share Posted November 16, 2008 He means it's 145 in the database.... @OP google zerofill. Quote Link to comment https://forums.phpfreaks.com/topic/132942-leading-zeros/#findComment-691335 Share on other sites More sharing options...
gevans Posted November 16, 2008 Share Posted November 16, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/132942-leading-zeros/#findComment-691339 Share on other sites More sharing options...
.josh Posted November 16, 2008 Share Posted November 16, 2008 sprintf Quote Link to comment https://forums.phpfreaks.com/topic/132942-leading-zeros/#findComment-691340 Share on other sites More sharing options...
ddrudik Posted November 16, 2008 Share Posted November 16, 2008 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] Quote Link to comment https://forums.phpfreaks.com/topic/132942-leading-zeros/#findComment-691507 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.