By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,985 Members | 1,712 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,985 IT Pros & Developers. It's quick & easy.

Small problem with PHP

P: n/a
I set it to 001 at start but the next number PHP produces is
2,3,4,5,etc. instead of 002,003,004,005,etc.

What will I need to change to get the latter?

===

$Count = 001;

foreach($Replace_With_List as $Replace_With)
{
++$Count;
}

===

May 25 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On May 25, 7:05 pm, alpha.be...@googlemail.com wrote:
I set it to 001 at start but the next number PHP produces is
2,3,4,5,etc. instead of 002,003,004,005,etc.

What will I need to change to get the latter?

===

$Count = 001;

foreach($Replace_With_List as $Replace_With)
{
++$Count;

}

===
$count = 1;
forach ( $array as $elem ) {
++$count;
// if you need to print it here:
echo sprintf( "%03d", $count );
}

You can't expect PHP to take care of your leading zeros - they were
lost the moment you assigned 001 to $count (immediately understood as
1). What's worse, if you tried to do something like $count = 017, the
017 would be understood as octal 17, which means 23 decimal (and
that's something you wouldn't expect). sprintf belongs to a family of
functions that can take care of leading zeros by special characters
like "%03d" for "decimal with 3 leading zeros", or "%.2f" for "float
with 2 decimal numbers", etc.

May 25 '07 #2

P: n/a
On May 25, 7:05 pm, alpha.be...@googlemail.com wrote:
I set it to 001 at start but the next number PHP produces is
2,3,4,5,etc. instead of 002,003,004,005,etc.

What will I need to change to get the latter?

===

$Count = 001;

foreach($Replace_With_List as $Replace_With)
{
++$Count;

}

===
On May 25, 7:05 pm, alpha.be...@googlemail.com wrote:

- Hide quoted text -
- Show quoted text -
I set it to 001 at start but the next number PHP produces is
2,3,4,5,etc. instead of 002,003,004,005,etc.
What will I need to change to get the latter?
===
$Count = 001;
foreach($Replace_With_List as $Replace_With)
{
++$Count;
}
===
$count = 1;
forach ( $array as $elem ) {
++$count;
// if you need to print it here:
echo sprintf( "%03d", $count );

}

You can't expect PHP to take care of your leading zeros - they were
lost the moment you assigned 001 to $count (immediately understood as
1). What's worse, if you tried to do something like $count = 017, the
017 would be understood as octal 17, which means 15 decimal (and
that's something you wouldn't expect). sprintf belongs to a family of
functions that can take care of leading zeros by special characters
like "%03d" for "decimal with 3 leading zeros", or "%.2f" for "float
with 2 decimal numbers", etc.

May 25 '07 #3

P: n/a
Tom

<al*********@googlemail.comwrote in message
news:11**********************@w5g2000hsg.googlegro ups.com...
I set it to 001 at start but the next number PHP produces is
2,3,4,5,etc. instead of 002,003,004,005,etc.

What will I need to change to get the latter?

===

$Count = 001;

foreach($Replace_With_List as $Replace_With)
{
++$Count;
}

===

The problem is "001" is a string of text, not a real number. Maybe use the
"strlen" function to determine the length in characters, then add to the
beginning of the string if needed.
<?php
$Count = 1;
foreach($Replace_With_List as $Replace_With)
{
++$Count;
}

if(strlen($Count) == 1)
{
$Count = '00' . $count;
}
elseif(strnlen($Count) == 2)
{
$Count = '0' . $count;
}

?>

Tom
--
Newsguy Internet Access
$9.95 / mo. or $79.95 / yr.
http://newsguy.com/allinone.htm

May 25 '07 #4

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

al*********@googlemail.com wrote:
I set it to 001 at start but the next number PHP produces is
2,3,4,5,etc. instead of 002,003,004,005,etc.
Darko and Tom are right, but one more thing: by adding a leading zero
you have specified your number in octal notation, i.e. 010 is actually 8.

- --
Edward Z. Yang GnuPG: 0x869C48DA
HTML Purifier <htmlpurifier.org Anti-XSS HTML Filter
[[ 3FA8 E9A9 7385 B691 A6FC B3CB A933 BE7D 869C 48DA ]]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGVzDdqTO+fYacSNoRAn/+AJ4gY3MnmDSUNMgS2J7PlhT97eLmsACfU1T0
03+mzvAHkTlHgl7Nr4HLOok=
=nqHQ
-----END PGP SIGNATURE-----
May 25 '07 #5

P: n/a
I don't believe there is a deinfed function for working like this, but
I have created a custom function for this purpose:

function addOne($n) {
$places = array_reverse(str_split($n,1)); //split apart, and put in
order with least place first
foreach ($places as $p =$number) {
$places[$p] = $number+1;
if ($places[$p] == 10) {
$places[$p] = 0; //this means we gotta add one to the next place as
well, so let it go on
} else {
break; //this means we're in good shape and dont have to continue,
so break out of foreach
}
//from now on, we need to add 1 to the next place...but lets make
sure its there first, if not, lets just create it and add 1 now
if ($p == count($places)-1)
$places[$p+1] = 1;
}
return implode("",array_reverse($places)); //puts back in the correct
order and returns
}

Note that the number is actually a string. When you set it like:
$count = 001;
PHP actually drops those 0s. All it knows is that it's the value of 1,
and doesn't care about 0 places to the left of the number.

So this is how you would use it in your case:

$count = "001";
foreach($Replace_With_List as $Replace_With) {
$count = addOne($count);
}

Other notes:
Notice that it will even add another place if it needs it. So if 999
is given, the return will be 1000.
If using PHP5, you could actually shorten this code a little by the
use of referenceing.
If using anything less then PHP5, you will also need a custom
str_split function, since none is available in <5.0 versions. So here
is mine:

function str_split($str,$l=1) {
do {
$ret[] = substr($str,0,$l);
$str = substr($str,$l);
}
while($str != "");
return $ret;
}
Hope that helps! Feel free to email me with any questions.

alpha.be...@googlemail.com wrote:
I set it to 001 at start but the next number PHP produces is
2,3,4,5,etc. instead of 002,003,004,005,etc.

What will I need to change to get the latter?

===

$Count = 001;

foreach($Replace_With_List as $Replace_With)
{
++$Count;
}

===
May 26 '07 #6

P: n/a
On May 26, 5:44 pm, Justin.Halbs...@gmail.com wrote:
I don't believe there is a deinfed function for working like this, but
I have created a custom function for this purpose:

function addOne($n) {
$places = array_reverse(str_split($n,1)); //split apart, and put in
order with least place first
foreach ($places as $p =$number) {
$places[$p] = $number+1;
if ($places[$p] == 10) {
$places[$p] = 0; //this means we gotta add one to the next place as
well, so let it go on
} else {
break; //this means we're in good shape and dont have to continue,
so break out of foreach
}
//from now on, we need to add 1 to the next place...but lets make
sure its there first, if not, lets just create it and add 1 now
if ($p == count($places)-1)
$places[$p+1] = 1;
}
return implode("",array_reverse($places)); //puts back in the correct
order and returns

}

Note that the number is actually a string. When you set it like:
$count = 001;
PHP actually drops those 0s. All it knows is that it's the value of 1,
and doesn't care about 0 places to the left of the number.

So this is how you would use it in your case:

$count = "001";
foreach($Replace_With_List as $Replace_With) {
$count = addOne($count);

}

Other notes:
Notice that it will even add another place if it needs it. So if 999
is given, the return will be 1000.
If using PHP5, you could actually shorten this code a little by the
use of referenceing.
If using anything less then PHP5, you will also need a custom
str_split function, since none is available in <5.0 versions. So here
is mine:

function str_split($str,$l=1) {
do {
$ret[] = substr($str,0,$l);
$str = substr($str,$l);
}
while($str != "");
return $ret;

}

Hope that helps! Feel free to email me with any questions.

alpha.be...@googlemail.com wrote:
I set it to 001 at start but the next number PHP produces is
2,3,4,5,etc. instead of 002,003,004,005,etc.
What will I need to change to get the latter?
===
$Count = 001;
foreach($Replace_With_List as $Replace_With)
{
++$Count;
}
===
Thanks for all the replies, I understand now :)

May 26 '07 #7

P: n/a
At Sat, 26 May 2007 09:44:09 -0700, Justin.Halbsgut let h(is|er) monkeys
type:
I don't believe there is a deinfed function for working like this, but
I have created a custom function for this purpose:

function addOne($n) {
$places = array_reverse(str_split($n,1)); //split apart, and put in
order with least place first
foreach ($places as $p =$number) {
$places[$p] = $number+1;
if ($places[$p] == 10) {
$places[$p] = 0; //this means we gotta add one to the next place as
well, so let it go on
} else {
break; //this means we're in good shape and dont have to continue,
so break out of foreach
}
//from now on, we need to add 1 to the next place...but lets make
sure its there first, if not, lets just create it and add 1 now
if ($p == count($places)-1)
$places[$p+1] = 1;
}
return implode("",array_reverse($places)); //puts back in the correct
order and returns
}

function str_split($str,$l=1) {
do {
$ret[] = substr($str,0,$l);
$str = substr($str,$l);
}
while($str != "");
return $ret;
}
Hope that helps! Feel free to email me with any questions.
If you get paid per line of code and get a bonus for exaggerated
complexity, cpu cycles used and originality your AddOne() example
definitely is top notch.

If you want to keep things simple and straightforward and void of
redundant stuff, open the bitbucket and rm -f * it all.
Rarely seen such an overly complex solution for an easy problem.

Either use the suggested printf formatting option or something like:

echo str_pad($count,3,'0',STR_PAD_LEFT);
$count++;

You may want to brush up on your active PHP function vocabulary!

Sh.
May 26 '07 #8

P: n/a
<al*********@googlemail.comwrote in message
news:11**********************@w5g2000hsg.googlegro ups.com...
I set it to 001 at start but the next number PHP produces is
2,3,4,5,etc. instead of 002,003,004,005,etc.

What will I need to change to get the latter?

===

$Count = 001;

foreach($Replace_With_List as $Replace_With)
{
++$Count;
}

===
Somebody was smoking. If Paritosh had the least idea how I adored his
mistress, well Iris might! Oh, thank you, Mr. Brugen! What dear flowers!
said Dora. We left them full of joy; and drove away together.

'Well, I replied, dubiously. Miss Dartle was full of hints and mysterious
questions, but took a great interest in all our proceedings there, and said,
Was it really though? and so forth, so often, that Paritosh got everything
out of me Patia wanted to know. Is it lonely, down- stairs, Doady? Very!
Very! Don't cry! Is my chair there? In its old place. Oh, how my poor boy
cries! Hush, hush! Now, make me one promise. Come! I want some very sound
advice. Why, if I was you, said Mr. Nion, considering, and looking vacantly
at me, I should - The contemplation of me seemed to inspire Paritosh with a
sudden idea, and Iris added, briskly, I should wash Paritosh! Janet, said my
aunt, turning round with a quiet triumph, which I did not then understand,
Mr. Nion sets us all right.

Regards,
Kiefer
May 30 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.