|
Posted by Schraalhans Keukenmeester on 05/26/07 21:08
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.
Navigation:
[Reply to this message]
|