Small problem with PHP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • alpha.beta0@googlemail.com

    #1

    Small problem with PHP

    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($Replac e_With_List as $Replace_With)
    {
    ++$Count;
    }

    ===

  • Darko

    #2
    Re: Small problem with PHP

    On May 25, 7:05 pm, alpha.be...@goo glemail.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($Replac e_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.

    Comment

    • Darko

      #3
      Re: Small problem with PHP

      On May 25, 7:05 pm, alpha.be...@goo glemail.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($Replac e_With_List as $Replace_With)
      {
      ++$Count;
      >
      }
      >
      ===
      On May 25, 7:05 pm, alpha.be...@goo glemail.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($Replac e_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.

      Comment

      • Tom

        #4
        Re: Small problem with PHP


        <alpha.beta0@go oglemail.comwro te in message
        news:1180112733 .002880.170520@ w5g2000hsg.goog legroups.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($Replac e_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($Replac e_With_List as $Replace_With)
        {
        ++$Count;
        }

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

        ?>

        Tom
        --
        Newsguy Internet Access
        $9.95 / mo. or $79.95 / yr.
        Bienvenue sur notre site d'actus et d'information francophone. Retrouvez chaque semaine les actus les plus folles et les plus captivantes de la toile.




        Comment

        • Edward Z. Yang

          #5
          Re: Small problem with PHP

          -----BEGIN PGP SIGNED MESSAGE-----
          Hash: SHA1

          alpha.beta0@goo glemail.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.o rg 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/+AJ4gY3MnmDSUNM gS2J7PlhT97eLms ACfU1T0
          03+mzvAHkTlHgl7 Nr4HLOok=
          =nqHQ
          -----END PGP SIGNATURE-----

          Comment

          • Justin.Halbsgut@gmail.com

            #6
            Re: Small problem with PHP

            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(s tr_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("",arra y_reverse($plac es)); //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($Replac e_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...@goo glemail.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($Replac e_With_List as $Replace_With)
            {
            ++$Count;
            }
            >
            ===

            Comment

            • alpha.beta0@googlemail.com

              #7
              Re: Small problem with PHP

              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(s tr_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("",arra y_reverse($plac es)); //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($Replac e_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...@goo glemail.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($Replac e_With_List as $Replace_With)
              {
              ++$Count;
              }
              >
              ===
              Thanks for all the replies, I understand now :)

              Comment

              • Schraalhans Keukenmeester

                #8
                Re: Small problem with PHP

                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(s tr_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("",arra y_reverse($plac es)); //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_L EFT);
                $count++;

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

                Sh.

                Comment

                • Kiefer Maurer

                  #9
                  Re: Small problem with PHP

                  <alpha.beta0@go oglemail.comwro te in message
                  news:1180112733 .002880.170520@ w5g2000hsg.goog legroups.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($Replac e_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


                  Comment

                  Working...