Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts

    Output numbers 01 instead of 1 in php

    I am creating a php script to randomly output numbers from 01 - 49 but I want the output to be 2 digits. Example: 01 02 03 etc, not 1 2 3. I have googled this and checked the manual but I can't find it, I know its a simple solution but I must be typing the wrong search words.

    If someone knows the solution please post it. Thanks.

    Leonard Whistler
    www.stubby.ca
    Leonard Whistler

  • #2
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,266
    Thanks
    6
    Thanked 48 Times in 48 Posts
    You could do something like
    PHP Code:
    <?php
    $str 
    9;
    if(
    $str 10){
        
    $str "0".$str;
    }
    echo 
    $str;
    ?>

  • #3
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    838
    Thanks
    15
    Thanked 9 Times in 9 Posts
    Yea I needed to do the same thing...

    So I just have something like:
    PHP Code:
    $thezero="0"
    Whenever it gets over 9 I change and exclude the zero part.
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #4
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Thanks Nightfire and dniwebdesign........

    ...this forum is very good for solutions like this.


    Leonard Whistler
    www.stubby.ca
    Leonard Whistler

  • #5
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    also worth a looksee ...
    PHP Code:
    <?
    echo str_pad($str2"0"STR_PAD_LEFT) ;
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #6
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,266
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Oooh, a new function for me. *looks it up*

    <edit>That's one useful function. Might come in handy soon</edit>
    Last edited by Nightfire; 04-11-2004 at 03:25 AM.


  •  

    Posting Permissions

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