This question already has answers here:
Interview question: In php, is 123==0123?
(5 answers)
Closed 6 years ago.
When I was practicing in php, I noticed, that the following expressions yield strange results:
011 == 11 // false
'011' == 11 // true
Shouldn't they both evaluate to same result?
This is because 011 is treated as an octal value because of the leading 0.
Here's the more in-depth explanation:
The 011 literal is recognized as an octal value
It is then converted to decimal value, which equals to 9
The actual comparison happens which looks like the following: 9 == 11 // false
As of the '011' == 11, it evaluates to true, because when string is compared to integer, it's coerced to the integer value as well. Interestingly, the leading zero in the string is ignored in the proccess and the php interpreter treats the value as a decimal rather than an octal one!
Related
This question already has answers here:
PHP is_nan() not check for a number
(2 answers)
PHP returning NaN
(5 answers)
In Java, what does NaN mean?
(11 answers)
Why is NaN not equal to NaN? [duplicate]
(6 answers)
Closed 4 years ago.
What is the concept of NaN in PHP? When and where is it used, and why this is useful?
What is this below function used for? And please, I need an explanation of the code below.
<?php echo is_nan(200) . "<br>"; echo is_nan(acos(1.01)); ?>
NaN means "Not a Number". We basically call imaginary numbers (eg: Complex Numbers) as NaN, for eg: square root of -1 (i), acos(1.01). These numbers cannot be computed / calculated.
Reference:
nan/"not a number" is not meant to see if the data type is
numeric/textual/etc..
NaN is actually a set of values which can be stored in floating-point
variables, but dont actually evaluate to a proper floating point
number.
The floating point system has three sections: 1 bit for the sign
(+/-), an 8 bit exponent, and a 23 bit fractional part. There are
rules governing which combinations of values can be placed into each
section, and some values are reserved for numbers such as infinity.
This leads to certain combinations being invalid, or in other words,
not a number.
From the documentation of is_nan() function:
bool is_nan ( float $val )
Details: Checks whether val is 'not a number', like the result of acos(1.01).
Returns TRUE if val is 'not a number', else FALSE.
So, basically this function is used for checking the validity of return values of mathematical functions and operations, and expects a float as a parameter.
Now, 200 is a valid number. So, is_nan(200) will return False.
While, acos(1.01) is trying to find arc cosine of 1.01. From Mathematics, we very well know that cosine function returns value in the range of -1 to +1. So value of 1.01 (more than 1) cannot exist. Hence, acos(1.01) cannot be determined. Thus it is not a valid number. Hence, is_nan(acos(1.01)) will return True.
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:
int variable with leading zero?
(5 answers)
Closed 7 years ago.
I want to know the difference between echo integer 204 and 0204 without quote.
echo 204; // output : 204
echo 0204; // output : 132
Numbers beginning with 0 (in many programming languages) are octal numbers. 204 outputs how you would expect because it begins with a number 1 through 9, signifying a decimal number. See php's documentation on integers.
Refer #Example 1
echo 204; // output : 204 because its a decimal number
echo 0204; // output : 132 because its a octal number
The answer is simple the first is decimal the second is Octal
Putting a leading zero on an integer literal in PHP code instructs PHP that the number is in Octal (base 8) format.
Echo, on the other hand, when invoked by itself with just a number, just takes the number fed to it and prints it in ordinary decimal form.
Octal 204 = Decimal 132.
http://php.net/manual/en/language.types.integer.php
This question already has answers here:
Why does PHP consider 0 to be equal to a string?
(9 answers)
Closed 8 years ago.
When I run the follow code in PHP
if('yes' == 0)
echo 'a';
else
echo 'b';
The output is a.
I don't understand what happen?
And can I convert the php code to C source code to have a look what real happening?
PHP is a dynamically typed language, and == is a loose comparison operator, meaning it will first cast values it compares to one type, int for that matter, and then compare them; strings are being cast to integers by taking numericals from the left part, so 1abc casts to 1. By that logic yes cast to 0, and 0 == 0 yields true.
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,