+ Reply to Thread
Results 1 to 4 of 4

Thread: PHP and leading zeroes mindtrick

  1. #1
    cenobite321 is offline x10Hosting Member cenobite321 is an unknown quantity at this point
    Join Date
    Dec 2007
    Posts
    78

    PHP and leading zeroes mindtrick

    Hello everyone,

    I've just got an e-mail from a friend telling me how PHP disliked leading zeroes. He attached the following code:

    PHP Code:
     <?php
     
    for($i=01;$i<=07;$i++){
     print 
    $i;
     echo 
    "<br>";
     }
     
     for(
    $i=01;$i<=08;$i++){//with 8 or more it fails
     
    print $i;
     echo 
    "<br>";
     }
     
    ?>
    As the comment highlights the second for statement fails and numbers are not displayed like in the first one. I made the following analysis of both numbers:

    PHP Code:
     $foo 07;
     
    //print the type of 07
     
    print '07 is a: '.gettype($foo).'<br>';
     
    //print the value of 07
     
    print $foo.'<br>';
     
    //adds 07 plus 1 and prints the result
     
    $foo $foo +1;
     print 
    $foo.'<br>';
     
     
     
    $foo 08;
     
    //print the type of 08
     
    print '08 is a: '.gettype($foo).'<br>';
     
    //print the value of 08
     
    print $foo.'<br>';
     
    //adds 08 plus 1 and prints the result
     
    $foo $foo +1;
     print 
    $foo.'<br>'
    The outcome was this:
    07 is a: integer
    7
    8
    08 is a: integer
    0
    1
    Weird...:nuts:
    Last edited by cenobite321; 01-10-2010 at 04:45 PM.

  2. #2
    marshian's Avatar
    marshian is offline x10 Elder marshian is an unknown quantity at this point
    Join Date
    Jan 2008
    Location
    Belgium
    Posts
    526

    Re: PHP and leading zeroes mindtrick

    PHP doesn't dislike leading zeroes, leading zeroes mean something =P

    I have a test for you, try the following script before you read on:
    PHP Code:
    <?php
    header
    ("Content-type: text/plain");

    $one 001;
    $eight 010;
    echo 
    $one $eight;
    ?>
    Do you know now?

    -----------------------
    A leading zero means the number is octal. The octal numeric system consists of 8 different numbers (0-7), it's related to the hexadecimal system, which uses 16 (0-F).
    If you write 01, this means 1⁽⁸⁾ (read: 1 the numeric system with 8 symbols). This also equals 1⁽¹⁰⁾ in the more common decimal system.
    Writing 010 however means 10⁽⁸⁾ which is the same as 8⁽¹⁰⁾.
    Thus, using 8 or 9 in a number with leading zero indicates an invalid number (since 8 and 9 don't exist in the octal system). Therefore, the variable is assigned the default value of 0.
    That pretty much explains this phenomenon, any questions?
    Real programmers don't document their code - if it was hard to write, it should be hard to understand.

  3. #3
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: PHP and leading zeroes mindtrick


    Weird...:nuts:
    Not really.

    In most programming languages

    x = 011

    gives x the value of 9.

    while

    y = 0x11

    gives y the value of 17

    Leading 0x signifies hexidecimal, or base 16. You use 0-9 and a-f (or A-F) as numerals.
    Leading 0 with no x signifies octal, or base 8. You use 0-7 as numerals.

    Since 8 is not a legal octal numeral, 08 either causes an error, or is evaluated as zero ( 0118 would be evaluated as 011 or decimal 9).
    Nothing is always absolutely so.

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP and leading zeroes mindtrick

    For reference, this is documented on the PHP Integers manual page.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers