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.
Related
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.
In PHP, when using microtime(true), I am expecting to get in most cases 6 digits to the right of the decimal.
<?php
//version 5.5.31
$time = microtime();
$timeTrue = microtime(true);
var_dump($time, $timeTrue);
// string(21) "0.64728900 1462577720"
// float(1462577720.6473)
?>
Why is the float only showing the first four digits? Sometimes I only get three digits, which makes sense. Should it not be returning float(1462577720.647289). ie 6 digits in most cases, occasionally 5 digits?
You seem to be confusing precision with digits after the decimals point.
Double precision floating points usually have a maximum precision of 14 to 16 digits and your example shows 14. Yes, all digits count. The decimal point is handled by another variable (at the floating point definition level).
The same principle would apply to very large numbers which would also lose precision after 14-15 digits long.
I want to find last 8 digit of number 0000548795846 in php.
Its done fine when I use string but I have some problem with Integer starts with zero.
Try with -
substr(" 0000548795846", -1, 8);
In your example what is happening is that, PHP by default considers the number starting with 0 (zero) as octal number i.e base 8, and you are expecting it as decimal.
When PHP comes across any number starting with zero it converts it into decimal equivalent of the actual number for ex. 0000548795846 gets converted into 44 decimal, so there is no length >= 8 present.
So, Solution is to convert the number in string format. Then use substr() with appropriate arguments, as suggsted by "sgt"
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
I now want to know that means this line:
printf("Answer: %00010.6f", 22);
He prints: 022.000000. But way? i know whar 6f means float.
thansk for answers
After initially thinking this was C (result of long habit), I realize this is for PHP. Mostly the same, but the constant seems to be handled differently.
Anyway, the parameters in your code break down as follows:
f = print the number as a floating point
10 = total field width of ten digits
000 = print up to 3 leading zeros when applicable (i.e. if there aren't 3 significant figures left of the decimal point)
. = use a dot as the decimal separator
6 = six places after the decimal
It seems the printing parameters for PHP's printf are actually on the sprintf page.
The printf() syntax and meanings are very well-documented. Look at the printf(3) man page or the Wikipedia printf entry.
The particular example you gave means: print a floating point number. Give it 6 characters after the decimal point. Then prefix it with zeros until it is at least 10 characters.
this format string means:
use 0 as filling character
fill to a minimum of 3 characters before the . ( 2 -> 002, 23 -> 023, longer numbers stay as they are)
display exactly 10 characters (including the delimiter)
use . as decimal delimiter
display 6 digits after the delimiter
type float
printf("Answer: %f", 22) would enter the number 22 to the string "Answer: %f" in the place of the "%f" and print it as a float ("f" is for "float").
The numbers between the "%" and the "f" are to set the format - the number of digits that the printout will have.