The leading zero is changing the result in php [duplicate] - php

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

Related

PHP: what will be the output of the following and why ? $x=025 echo $x/5; [duplicate]

This question already has answers here:
How is Leading Zero working in php?
(2 answers)
Closed 1 year ago.
what will be the output of the following and why ?
$x=025 echo $x/5;
The question deals with php and brings the different between 025 and 25 when divided by 5.
The octal numeral system, or oct for short, is the base-8 number system, and uses the digits 0 to 7, that is to say 10 represents 8 in decimal and 100 represents 64 in decimal.
25 = Whole
025 = Octal
025 in octal system = 21
so 21/5 = 4.2

What will be the output 012/2 in PHP? [duplicate]

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.

Generate random number with php must be 5 charachter... Even 1 = 00001 [duplicate]

This question already has answers here:
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 7 years ago.
My question subject can be seen easy, but i need something different.
$string = rand(1,99999) // echo 1
But my need it should
$string = rand(---?----) //echo 00001 - Always 5 characters..
For example when $string echo "23" it should write 00023 , if the $string writes 2 character left side will be 3times zero - 3 charachter, should write left side 2 times zero - 4 character , should write 1 time zero...
Logic is always 5 character...
I found the solution...
str_pad($string, 5, "0", STR_PAD_LEFT);

Computing limits in PHP [duplicate]

This question already has answers here:
Is there something that's larger than any numbers in PHP?
(9 answers)
Closed 8 years ago.
I want to compute n^(2/3) as n approaches infinity in PHP.
What I tried:
$n = "INF";
echo pow($n, (2/3));
Desired result:
INF
Actual result:
0
Any suggestions? How to compute limits in PHP?
UPDATE:
OK, first problem solved - I just needed to remove the quotation mark.
But is this actually the way to calculate a limit? Is it interpreted as a limit?
You're passing in the string "INF" into the exponential expression, which isn't valid. Try passing in just POW:
$n = INF;
echo pow($n, (2/3));
// INF

why intval(042) return result 34? how it works? [duplicate]

This question already has answers here:
int variable with leading zero?
(5 answers)
Closed 9 years ago.
I try understand how intval() works and one of the example is intval(042) = 34. As I know the default base is 10, so why it returns 34 instead 42. What role the zero playing in this example?
echo intval(042); //34
echo intval(42); //42
The leading 0 tells php to interpret it as octal/base 8.
042 in octal is 34 in decimal.
You can find a list of other formats for integers in PHP here.
Numbers prefixed with 0 are considered octal numbers (base 8). 0428 = 3410
In fact, this has nothing to do with intval. If you just write echo 042; you'll get 34.
Similarly, you can also write numbers in hexadecimal. echo 0xff; gives 255.
And, as of PHP 5.4, you can even write numbers in binary: echo 0b1001; gives 9.
because 0 before a number mean that this number is a octal number ,try to convert 42 form octal to decimal it will give you 34
i hope this help you,

Categories