<?php
$n1=$_POST['mont'];
echo $n1;
if($n1==07)
echo "numeric";
if($n1=="07")
echo "string";
if($n1==08)
echo "numeric";
if($n1=="08")
echo "string";
?>
in this if $_POST["mont"] is 07
the output is both string and integer
but if $_POST["mont"] is 08
the output is only string
what may be the cause
Two causes:
PHP is weakly-typed, which means that there's no difference between a string containing a number, and just the number. "5" == 5
Numbers that start with a 0 are considered to be octal. "08" is not a valid octal number, so it can only be considered a string.
Leading zero's on numbers indicate octal format. Octal only goes up to 7, so 08 wouldn't be valid.
Isn't php's consistency fantastic? The same issue happens in JavaScript too. The string with a leading zero is casted as an octal (base-8) number, so "010" would have equaled 8, but not "08".
It's because any number preceding with a '0' in PHP is read as an octal number, so '07' really means 7 in base 8, not base 10 (although they are the same amount). Therefore '08' is considered invalid as an octal and not read as a number.
Related
Let's suppose I have a code which outputs $i as
$i = 016;
echo $i / 2;
//ans will be 7
I know that the leading zero indicates an octal number in PHP, but how is it interpreted, how can it be executed? Can somebody share its execution step by step? What is the role of parser here? I have researched a lot and read all the previous answers but none are having any deep explanation.
When you preceed integers with zero in PHP, in that instance, 029.
It becomes octal.
So when you echo that, it will convert to its decimal form.
Which results to:
echo 016; //14 (decimal) valid octal
echo 029; // 2 (decimal) - Invalid octal
Actually, its here stated in the manual
Valid octal:
octal : 0[0-7]+
Note: Prior to PHP 7, if an invalid digit was given in an octal integer (i.e. 8 or 9), the rest of the number was ignored. Since PHP 7, a parse error is emitted.
The octal numeral system, or oct for short, is the base-8 number system, and uses the digits 0 to 7.
Octal numerals can be made from binary numerals by grouping consecutive binary digits into groups of three (starting from the right).
For example, the binary representation for decimal 74 is 1001010. Two zeroes can be added at the left: (00)1 001 010, corresponding the octal digits 1 1 2, yielding the octal representation 112.
In your question $i = 016; is calculated by the interpreter and produces $i = 14;(which is the equilevant decimal number)
Then you simply divide it by 2, which outputs 7.
I have the following error while running this code below:
Code:
<?php
$a = array(00001, 00008, 00009, 00012);
print_r($a);
?>
Error:
Parse error: Invalid numeric literal.
Why this issue occurred and how do i solve this?
This comes from the changes made to how integers, specifically octals, are handled in PHP7 (as oppsoed to PHP5).
From the documentation (from PHP7 migration)
Invalid octal literals
Previously, octal literals that contained invalid numbers were silently truncated (0128 was taken as 012). Now, an invalid octal literal will cause a parse error.
From the documentation of integers
Prior to PHP 7, if an invalid digit was given in an octal integer (i.e. 8 or 9), the rest of the number was ignored. Since PHP 7, a parse error is emitted.
Either use them as strings, or actual integers
$a = array(1, 8, 9, 12); // Integers
$a = array("00001", "00008", "00009", "00012"); // Strings
Example of PHP7 vs PHP5: https://3v4l.org/GRZF2
http://php.net/manual/en/language.types.integer.php
Manual: http://php.net/manual/en/migration70.incompatible.php
This is because all numbers starting with 0 is considered octal values, which has an upper limit of 8 digits per position (0-7). As stated in the PHP manual, instead of silently dropping the invalid digits they now (7.x) produce the above warning.
Why are you writing your numbers like that though? If the leading zeroes are significant, then it's not a number you have but a string. Should you need to do calculations on those as if they were numbers, then you need to add the leading zeroes when outputting the values to the client.
This can be done with printf() or sprintf() like this:
$number = 5;
printf ("%05$1d", $number);
Please see the manual for more examples.
Sometime an apparently valid numeric literal is being detected as an invalid numeric literal.
This is a regression since php5.4
You can be fix this by changing the array to:
$a =array(1,8,9,12);
$a = array('0001','0008','0009','0012'); //alternative method for fix
Reference: https://bugs.php.net/bug.php?id=70193
decimal : [1-9][0-9]*(_[0-9]+)*|0
octal : 0[0-7]+(_[0-7]+)*
from document regex for consider value as decimal and octal are given above
In this scenario with value is 00008, 00009 not pass validation for octal or decimal as well. since it is given error, parse parameter to string is solving problem partially.
Note: In PHP any number starts from zero considered as octal but 8 and
9 will not used in octal number resulting throw this error.
I'm fairly new to PHP, so forgive me if this is a stupid question.
I tried to run mktime() on my site and, because I'm still getting familiar with the syntax, ran something like this:
echo date('h:i:s M-d-Y', mktime(12, 00, 00, 12, 08, 2013) );
Which I was surprised to find returned this:
12:00:00 Nov-30-2013
When what I was expecting was this:
12:00:00 Dec-08-2013
I eventually figured out that the "08" was the problem and it should just be "8".
Given that mktime() is capable of making some on-the-fly corrections and assumptions (such as example #2), why didn't it simply correct "08" to "8"? This is especially confusing to me since it handles double 0's just fine. Is this a feature, a bug, or simply an idiosyncrasy of the language?
Also, why did it "correct" to Nov 30 as opposed to some other date? Given the above examples, I would have expected Jan 01.
This isn't an issue with mktime, but with how PHP interprets literal numbers.
In PHP, when a literal number is prefixed with a zero, e.g. 01 or 08 it is interpreted as octal, see http://www.php.net/manual/en/language.types.integer.php similar to how the 0x prefix denotes hexadecimal literal numbers.
Note that 08 is actually invalid, as octal numbers have the digits 0-7, PHP's documentation states that "If an invalid digit is given in an octal integer (i.e. 8 or 9), the rest of the number is ignored.", so 08 becomes 0, hence why it snaps to 30th November.
Numbers starting with a leading 0 like 08 are octal notation; except 08 is invalid octal so it is treated as a value of 0.
As there is no 0th day in month 12, it takes 1 from the 1st december to give the last day of the previous month, 30th november
Numbers with a leading zero evaluate to octal numbers (base 8):
http://www.php.net/manual/en/language.types.integer.php
the php code is pretty simple :
<?php
$my_bn_num=number_bangla(0123);
function number_bangla($num1){
echo ("<br>num =".$num1);
}
?>
And the ouput is: num=83
But if I call the function with a singly quoted string like this:
$my_bn_num=number_bangla('0123');
the output is: num=0123
What is the detailed difference between 0123 and '0123' here ?
0123 is an octal integer because it starts with 0.
Integers will be printed by echo/print as decimal numbers.
'0123' is a string, so nothing will be converted when it is printed.
0123 is a numeric value whereas '0123' is a string value. Internally PHP stores those types differently.
In order to cast string to integer you can do following:
$num = (int) $num;
In this particular example printing number 0123 results to 83 because 0123 is an octal value. For casting octal strings to octal numbers PHP offers octdec() function:
$num = octdec($num);
0123 is an octal number. In most scripting languages any number represented by a 0 in front of it is treated as if it were an octal number (since you normally don't put 0's in front of numbers.
When you specify it in quotes its treated as a string.
When you are handling integers and want them in a particular format, what you should do is use the intval function.
intval($num,10) would translate the variable $num to a base-10 integer, instead of relying on the PHP casting code to work its black magic.
Why is it that following results in 34?
It doesn't seem to have anything to do with octal numbers.
intval(042);
but a leading 0 does indicate octal in many languages, as is the case here.
It does have to do with octal numbers, 042 is interpreted as the octal number 42 which is 4 * 8 + 2 = 34.
Please be aware that the octal interpretation happens when the number literal is parsed while loading the PHP script. It has nothing to do with intval(), which doesn't do anything here because the value is already integer.
Octal interpretation happens only with number literals, not when casting a string to integer:
intval(042) // == 34
intval('042') // == 42
(int)'042' // == 42
In PHP a number with leading 0 is read as an octal number.
So 042 is read as an octal number.
The intval() function converts it into decimal number, which is 34.
So the browser output is 34.
It's simply how the function is defined. The leading zero is an instruction parse it as an octal number, similarly as to how 0x as a prefix means hex. See the documentation for more information.
Be careful when passing this function a string value with a leading "0". If you give it "042" then, it will treat it as BASE 8 - 9 and convert it to decimal value, which is by default base.
Please go through this