php echo 00 or 000

Discussion in 'PHP' started by Adulu, Oct 27, 2008.

  1. #1
    $x=1;
    echo $x;
    result =0

    how to do that result =01 or 001?

    help! appreciate.
     
    Adulu, Oct 27, 2008 IP
  2. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #2
    u want a 0 before the 1 ??
     
    Bohra, Oct 27, 2008 IP
  3. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #3
    http://www.php.net/sprintf

    <?php
    $x = 1;
    
    $str = sprintf('%02s', $x);
    echo "$str<br/>"; // 01
    
    $str = sprintf('%03s', $x);
    echo "$str<br/>"; // 001
    
    $str = sprintf('%04s', $x);
    echo $str; // 0001
    ?>
    Code (markup):
     
    joebert, Oct 27, 2008 IP
    Adulu likes this.
  4. Adulu

    Adulu Peon

    Messages:
    2,791
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    0
    #4

    Thanks so much:)
     
    Adulu, Oct 27, 2008 IP