2

I understand that casting with (int) rounds a number towards zero, but then I started poking around with bases other than 10 and now I'm not exactly sure what else (int) does:

$a = 050; // an octal number

echo $a;   // output is ----> 40

however

$a = '050';

echo (int)$a; // output is ----> 50

So I thought (int) besides rounding towards zero also clips leading zeroes. But then if I do the same exercise as in the second code block (above) with binary I don't get what I expect:

$a = '0b010';

echo (int)$a; // output is ----> 0

and in hexadecimal:

$a = '0x050';

echo (int)$a; // output is ----> 0

Even using decimal scientific notation gives a quizzical result:

$a = 5e6;
$b = '5e6';

echo $a; // output is ----> 5000000
echo (int)$b; // output is ----> 5

What does casting to an integer using (int) really mean? Besides stripping the decimal point and everything after in a float, does (int) parse a cast string character-by-character, and return the concatenation of all leading, numeric, non-zero characters? Like this:

function intCast($str) {

    $length = strlen($str);

    $returnVal = '';

    for($i = 0; $i < $length; $i++) {

        // Note: ord() values 48 to 57 represent 0 through 9 

        if ($returnVal === '' && ord($str[$i]) == 48) {

            continue; // leading zeroes are OK but don't concatenate

        } else if (ord($str[$i]) > 48 && ord($str[$i]) < 58) {

            $returnVal .= $str[$i];

        } else {

            break;

        }

    }


    return (strlen($returnVal) > 0) ? $returnVal : 0;

}

***Note that this link does not seem to provide a sufficient answer.

Community
  • 1
  • 1
  • 1
    I really don't see why the linked question doesn't provide a sufficient answer. – Jocelyn Jul 26 '16 at 17:42
  • 1
    The [documentation](http://php.net/manual/en/language.types.string.php#language.types.string.conversion) tells you how strings are converted to numbers. Just disregard the parts about floats and you'll see how it works for ints. – Niet the Dark Absol Jul 26 '16 at 17:42
  • 1
    `$a = 050` echoes `40` because 050 is considered octal. – Aaron Jul 26 '16 at 17:42
  • @Aaron I know about the octal part, but why do different string representations of different bases get treated differently by (int)? – The One and Only ChemistryBlob Jul 26 '16 at 17:43
  • @TheOneandOnlyChemistryBlob You just answered your own question. Different bases need to be handled differently. – Aaron Jul 26 '16 at 17:44

1 Answers1

3

'050' is a string, and PHP's first job in doing a typecast is to normalize that string:

echo '050' + 1 -> echo '50' + 1 -> echo 50 + 1 -> echo 51 -> 51

050 by itself is an octal number, it's already an integer, so the only thing PHP has to do is convert it to a base10 number for output:

echo 050 + 1 -> echo 40 + 1 -> echo 41 -> 41
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Makes sense...wrt scientific notation, (int) will not first convert '1e6' to the actual number and output 100000...it will treat '1e6' as the number 1 followed by a non-number and just output 1 is what it looks like – The One and Only ChemistryBlob Jul 26 '16 at 17:49
  • And a binary "string" '0b111' cast by (int) is not 111 but rather the number 0 followed by a non-number? – The One and Only ChemistryBlob Jul 26 '16 at 17:50
  • 1
    leading zeroes are dropped when doing string->int conversions, which means `0b11` -> `b11` -> `0`, because `b` isn't a valid integer number. it'd be hex, but it's not `0xb` – Marc B Jul 26 '16 at 18:46