SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2008
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Deleting Leading 0's but I want to KEEP them

    I have a field in my database that has leading zeros. I am using file_put_contents to put the value of this field into a file. The problem is that if the number in the database is 007, the value in the file is 7. How can I fix this? Thanks.

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2007
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's because, for the db, 007 and 7 are the same integer value.
    It depends of your db, but you can try to cast the value to a left padded varchar, or a flot.

    look into the db documentation about casting.

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Really you should not use leading zeros, it offers no benefits or features.
    Save that kind of stuff for presentational stuff!
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    Really you should not use leading zeros, it offers no benefits or features.
    Save that kind of stuff for presentational stuff!
    Not true in every case.

    In SimpleDB you are required to pad your integers if you want to do comparisons since they're technically stored as strings.
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bhanson View Post
    Not true in every case.

    In SimpleDB you are required to pad your integers if you want to do comparisons since they're technically stored as strings.
    Does it give you any features or benefits since you have to do it to overcome a limitation in the DB software?
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    $str= 007 ;

    is not the same as

    $str = "007" ;

    PHP fairly cleverly tries to work out what kind of variable it is being passed.

    Two things you should note:

    1 Work out what PHP is 'thinking', use:

    var_dump() ;

    (try it on those 2 vars above)

    2 You should get into the habit of telling PHP what kind of variable to turn it into, the term is "type juggling" search the PHP manual for it.

    This will save you a mountain of heartache.

  7. #7
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    Does it give you any features or benefits since you have to do it to overcome a limitation in the DB software?
    Adding a positive ability is practically the definition of a feature or benefit.
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  8. #8
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bhanson View Post
    Adding a positive ability is practically the definition of a feature or benefit.
    Right, but I don't see a workaround for a limitation as adding a positive ability.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  9. #9
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    Right, but I don't see a workaround for a limitation as adding a positive ability.
    Faulty logic. Hiring a DBA to manage your relational database must not be a benefit because the complexity is a limitation. It can only grow so large before you have to staff someone for it.

    The definition of a limitation is so arbitrary it's easier to just classify everything as a benefit or feature.
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  10. #10
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I agree with tripy about looking into type casting.

    However if you don't want to fiddle with your table or queries, you can use sprintf to preserve your leading zeros if you're sure of the number of zeros.

    This will make sure that $str is 10 characters.

    Code:
    $str = sprintf('%010s', $str);
    file_put_contents('file', $str);


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •