Reply to Re: Small problem with PHP

Your name:

Reply:


Posted by Darko on 05/25/07 17:51

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.

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация