Skip to content

Instantly share code, notes, and snippets.

@dylancwood
Last active December 31, 2015 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dylancwood/7922424 to your computer and use it in GitHub Desktop.
Save dylancwood/7922424 to your computer and use it in GitHub Desktop.
This simple script illustrates that PHP interprets digits with leading zeros as an octal. This can cause problems when interpreting user input as an integer without converting it to a string first then parsing it (e.g with intval()).
<?php
echo 'This simple script illustrates that PHP interprets digits with leading zeros as an octal. This can cause problems when interpreting user input as an integer without converting it to a string first then parsing it (e.g with intval()).';
echo '<br>Notice that numbers greater than 7 do not compare nicely with their leading-zero counterparts.';
echo '<br>';
echo '9';
echo '<br>';
echo '9==09 ... ';
echo iif(9==09, 'true', 'false');
echo '<br>';
echo '9==9 ... ';
echo iif(9==9, 'true', 'false');
echo '<br>';
echo '<br>';
echo '8';
echo '<br>';
echo '8==08 ... ';
echo iif(8==08, 'true', 'false');
echo '<br>';
echo '8==8 ... ';
echo iif(8==8, 'true', 'false');
echo '<br>';
echo '8 compared to octal "010"';
echo '<br>';
echo '8==010 ... ';
echo iif(8==010, 'true', 'false');
echo '<br>';
echo '<br>';
echo '7';
echo '<br>';
echo '7==07 ... ';
echo iif(7==07, 'true', 'false');
echo '<br>';
echo '7==7 ... ';
echo iif(7==7, 'true', 'false');
echo '<br>';
echo '<br>';
echo '6';
echo '<br>';
echo '6==06 ... ';
echo iif(6==06, 'true', 'false');
echo '<br>';
echo '6==6 ... ';
echo iif(6==6, 'true', 'false');
echo '<br>';
echo '<br>';
echo '5';
echo '<br>';
echo '5==05 ... ';
echo iif(5==05, 'true', 'false');
echo '<br>';
echo '5==5 ... ';
echo iif(5==5, 'true', 'false');
echo '<br>';
echo '<br>';
echo '4';
echo '<br>';
echo '4==04 ... ';
echo iif(4==04, 'true', 'false');
echo '<br>';
echo '4==4 ... ';
echo iif(4==4, 'true', 'false');
echo '<br>';
echo '<br>';
echo '3';
echo '<br>';
echo '3==03 ... ';
echo iif(3==03, 'true', 'false');
echo '<br>';
echo '3==3 ... ';
echo iif(3==3, 'true', 'false');
echo '<br>';
echo '<br>';
echo '2';
echo '<br>';
echo '2==02 ... ';
echo iif(2==02, 'true', 'false');
echo '<br>';
echo '2==2 ... ';
echo iif(2==2, 'true', 'false');
echo '<br>';
echo '<br>';
echo '1';
echo '<br>';
echo '1==01 ... ';
echo iif(1==01, 'true', 'false');
echo '<br>';
echo '1==1 ... ';
echo iif(1==1, 'true', 'false');
echo '<br>';
echo '<br>';
function iif($test,$test_true_result,$test_false_result='') {
return $test ? $test_true_result : $test_false_result;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment