related to php arithmetic - php

$y = 013;
echo $y + 5; //this result in 16
I can not figure it out how its ans is 16? Can any one help?

because 013 isn't decimal (base 10). it's octal (base 8). the value in decimal is:
(0 * 8^2) + (1 * 8^1) + (3 * 8^0) = 0 + 8 + 3 = 11
which gives the correct (though unexpected, at least by you) result of 16 when added to 5.
moral of the story: don't prepend a number literal with 0 unless you know what it means

number with leading zero is octal number
like
$a = 0123; // octal number (equivalent to 83 decimal
Integers can be specified in decimal
(base 10), hexadecimal (base 16), or
octal (base 8) notation, optionally
preceded by a sign (- or +).
To use octal notation, precede the
number with a 0 (zero). To use
hexadecimal notation precede the
number with 0x.

$y = 013;
echo $y + 5;
013 is octal number all php integer numbers are octal .
show this link. first.
http://www.ascii.cl/conversion.htm

Related

How do i convert an 8 bit binary data to hex

print_r(bin2hex("11111111"));
echo '</br>';
print_r(bindec("11111111"));
Result
131313131313131
255
I want a hexadecimal 16 byte value to do aes encryption.How is the conversion from binary to hex happening in php.I am getting incorrect value using the function.Also when i convert an array of hexadecimal values to string the byte length changes
You get a correct result, it's just not what you want. bin2hex() returns an ASCII string of the hexadecimal representation. A quote from the manual:
Returns an ASCII string containing the hexadecimal representation of str.
So If you want the hexadecimal number you can use this:
print_r(dechex(bindec("11111111")));
The converter to get hexidecimal is dechex(), but it needs a decimal number. To do that we convert you binary string to a decimal number first using bindec() and then pass it into dechex(), e.g:
print_r(dechex(bindec("11111111")));
<?php
$str = "Hello world!";
echo bin2hex($str) . "<br>";
echo pack("H*",bin2hex($str)) . "<br>";
?>
PHP.NET Manual :
http://php.net/manual/en/function.bin2hex.php
Test Your Result : http://www.cs.princeton.edu/courses/archive/fall07/cos109/bc.html
Detailed Explanation:
http://www.computerhope.com/binhex.htm
It's simply 9 * 16 + F where F is 15 (the letters A thru F stand for 10 thru 15). In other words, 0x9F is 159.
It's no different really to the number 314,159 being:
3 * 100,000 (10^5, "to the power of", not "xor")
+ 1 * 10,000 (10^4)
+ 4 * 1,000 (10^3)
+ 1 * 100 (10^2)
+ 5 * 10 (10^1)
+ 9 * 1 (10^0)
for decimal (base 10).
The signedness of such a number is sort of "one level up" from there. The unsigned value of 159 (in 8 bits) is indeed a negative number but only if you interpret it as one.

php variable magic. How php work with decimal and hex

$a1 = 010;
print (int)$a1;
print value = 8
Anybody can explain how php made this result?
Because 010 in hex = 16.
010 is 10 in base-8, i.e. 8 in base-10.
Full reference from the manual:
<?php
$a = 1234; // decimal number
$a = -123; // a negative number
$a = 0123; // octal number (equivalent to 83 decimal)
$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
$a = 0b11111111; // binary number (equivalent to 255 decimal)
?>
It's not hex. It's octal.
For HEX:
$a = 0x10;
For octal
$a = 010;
For HEX number, you have to start with 0x.

What does a leading zero do for a php int?

echo(073032097109032116104101032118101114121032109111100101108032111102032097032109111100101114110032109097106111114032103101110101114097108046);
Essentially, a very large number. Now, why does it output 241872? I know PHP has float handlers. When I remove the leading zero, it functions as expected. What is that leading zero signifying?
If you use a leading zero, the number is interpreted by PHP as an octal number. Thus, a 9 is not a valid part of the number and the parser stops there:
0730320 (base 8)
= 7 * 8^5 + 3 * 8^4 + 3 * 8^2 + 2 * 8^1
= 7 * 32768 + 3 * 4096 + 3 * 64 + 2 * 8 (base 10)
= 241872 (base 10)
A leading zero indicates an octal integer value
Manual:
Warning
If an invalid digit is given in an octal integer (i.e. 8 or 9), the rest of the number is ignored.
Example #2 Octal weirdness
<?php
var_dump(01090); // 010 octal = 8 decimal
?>
So your number gets cut off after 0730320, which is 241872 in decimal.
010 gives you 8 (octal number)
0x10 gives you 16 (hexadecimal number)

Echo 05000 ? PHP

Could some one please Explain why echo 05000 = 2560 ???
i don't get it , i tried to search over the net i didn't find it somewhere
Thank you
05000 is octal notation, 2560 is decimal notation.
The leading 0 means that the number is in octal (base 8) rather than decimal (base 10).
50008 = 5×83 + 0×82 + 0×81 + 0×80 = 256010
It's a string integer literal in octal notation.
You can do base conversions with base_convert. Example:
echo base_convert("5000", 8, 10); //echoes "2560"
See Positional notation.

PHP comparison confusion

// today is 03 Jan 2009
$datemonth = (int) date("md");
if($datemonth == 0103){
echo "Match";
} else {
echo "Not a match";
}
I am receiving Not a match as result. Isn't 0103 equal to 103 when compared as integer? In this situation I can use if($datemonth == 103) for the intended behaviour. But why the logic is failing? A leading zero does not have any value in an integer, right?
When you begin a numeric literal with a leading zero, it means the number is in octal (base 8). You probably meant it to be a decimal (base 10) number. 0103 in octal is equal to 67 in decimal. Drop the leading zero and your code should work. See PHP documentation for more details on numeric literals.

Categories