This question already has answers here:
int variable with leading zero?
(5 answers)
Closed 5 years ago.
What will be the output of this expression ?
$a = 012;
$b = $a / 2;
var_dump($b);
I got the answer (int) 5 Please anyone suggest me how this is calculate ?
When you use a leading 0 in 012, you're actually using octal notation. That means, that this is 12 number but as an octal number.
So012 is actually 8+2, which is 10 as a decimal number.
Related
This question already has answers here:
What do numbers using 0x notation mean?
(5 answers)
Why the integer data types hexadecimal, octal and binary are getting converted to decimal type and then displayed in a browser?
(1 answer)
Why are hexadecimal numbers prefixed with 0x?
(5 answers)
Closed 2 months ago.
As I mentioned earlier, in the script below, I don't know what is the data type of "0XB"?
$a = 0XB;
$b= 1;
$c= $a + $b;
var_dump($c);
The result is 12, which means 0XB is 11, I am not sure how PHP interpreter 0XB is equal to 11.
Can someone give me an explanation regarding this one and the use cases of this data type?
Thank you in advanced
This question already has answers here:
Why is PHP printing my number in scientific notation, when I specified it as .000021?
(7 answers)
Closed 3 years ago.
I'm trying to do division in my code, here is mine:
$total = $dateRange / $value; // 2 / 10000000
it give me result 2.0E-7 instead of 0.0000002. Isn't it a floating point?
I've tried to use convert to string or json_decode it's still give me same result, and number_format((float) $result, 6, '.', '') gives me
0.000000
You can use -
echo rtrim(rtrim(sprintf('%.8F', $result), '0'), ".");
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
What does the percent sign mean in PHP?
(6 answers)
Closed 3 years ago.
I just started to learn PHP deeper for Zend PHP certification and I found this code, which actually works. Can someone explain me the logic behind this?
<?php
$num = 20% - 8;
echo $num; // 4
What you're seeing is the modulus operator, which in essence asks "What is the remainder of 20 divided by -8".
So you might ask, why isn't it negative 4? From the manual,
The result of the modulo operator % has the same sign as the dividend — that is, the result of $a % $b will have the same sign as $a.
See the PHP: Arithmetic Operators for more documentation.
% is not per cent, but modulus, cf. https://en.m.wikipedia.org/wiki/Modular_arithmetic
20 = 2 * 8 + 4, therefore, 20 % 8 = 4
This question already has answers here:
What does a leading zero do for a php int?
(4 answers)
Closed 5 years ago.
Let us assume In PHP
$i = 016;
echo $i / 2;
The output is returning 7.! why..?
Appending 0 to the front of a number in PHP is making it octal.
16 in octal is equivalent to 14 in decimal.
So , 14/2 = 7 is the answer.
The leading 0 will cause 016 to be interpreted as written in base 8, thus in base 10 it will actually be 14.
So 14/2 = 7
This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 9 years ago.
how can i shorten a long number with php ? So a number like 0.3483748937847832 would become 0.34
Please search stackoverflow first - from PHP: show a number to 2 decimal places
number_format()
return number_format((float)$number, 2, '.', '');
https://stackoverflow.com/a/4483561/689579
or
$padded = sprintf('%0.2f', $unpadded); // 520 -> 520.00
https://stackoverflow.com/a/4483715/689579
Use round
echo round($number,2);