473,473 Members | 1,826 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

4 digit number

pradeepjain
563 Contributor
Hii guys,
I am generating a ID for students and i want the Id to be 4 digit always .....I keep incrementing the ID's .so it must automatically put 0's behind the number to make it 4 digit....How to do it...
Oct 3 '08 #1
3 2782
bnashenas1984
258 Contributor
Hi
You can not use a numerical variable because PHP will automaticaly delete extra zeros on the left side of your variable.

But you can use a string like this:

Expand|Select|Wrap|Line Numbers
  1. <?PHP
  2.     $var=123;
  3.     $fourDigit = trim($var);
  4.     for ($i=0; $i<=3; $i++) {
  5.         if (strlen($fourDigit) < 4) {$fourDigit = "0".$fourDigit;}
  6.     }
  7.     echo $fourDigit;
  8. ?>
  9.  
The length of your string will allways be 4 as long as you use numbers less than 10000

Hope it helps
Oct 3 '08 #2
Atli
5,058 Recognized Expert Expert
Why don't you just start counting at 1000?

Or, if you are using a database, like MySQL, you can have the database automatically add zeros to have the number fill a given length.

From the MySQL manual:
When used in conjunction with the optional extension attribute ZEROFILL, the default padding of spaces is replaced with zeros. For example, for a column declared as INT(5) ZEROFILL, a value of 4 is retrieved as 00004.
Oct 4 '08 #3
pradeepjain
563 Contributor
thank you bnashenas1984.that worked for me..i was using a similar kind of script but for 0 i was not using "0" i was just using 0 that caused me problem.


thanks,
Pradeep
Oct 4 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Rune Strand | last post by:
Hi, If I have a lot of integers and want do something with each digit as integer, what is the fastest way to get there? Eg. Make 12345 into an iterable object, like or "12345" (Btw: What is...
17
by: Kermit Piper | last post by:
Hello, I have been searching, Googling, searching. Cannot find a javascript to calc 10, 11 or 12 digit UPC codes. I just need an algorithm that calcs to verify the correct check digit. I have...
3
by: Kermit Piper | last post by:
Hello everyone, OK, let me try and explain again please. Here is what I'm trying to do. I have a 12-digit (UPC-A) javascript validation script which works great. All I need now is a similar...
11
by: balakrishnan.dinesh | last post by:
hi frnds, Im having two 20digit numbers, But while comparing those it is giiving wrong ouput in javascript. for example here is my code, my secanrio is , ~ If first 20 digit number is...
8
by: Candace | last post by:
I am using the following code to pick off each digit of a number, from right to left. The number I am working with is 84357. So for the first iteration it should return the number 7 and for the...
2
by: hikmaz | last post by:
I am trying to get the rightmost digits (%10) of a number (taken from the user) and store it into successive array locations and get rid of the rightmost digit (\10) to store the next and so on and...
8
by: Marc | last post by:
Hi all, I have to generate and send to a printer many 6 digit alphanumeric strings. they have to be unique but I cannot check in a database or something like that if it have already been printed....
7
by: southernsunsations | last post by:
/******************************************************************\ * * * * Date: 09.06.2007 ...
3
by: hl2ob | last post by:
Alright I'm still new to javascript. I was getting it pretty well, and getting everything alright untill this point. We have to make a program that test a 5 digit number as a palindrome. I have no...
7
by: harijay | last post by:
Hi I am a few months new into python. I have used regexps before in perl and java but am a little confused with this problem. I want to parse a number of strings and extract only those that...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.