1. script:
$num = "00445790";
echo $num;
returns:
00445790
2. script
$num = 00445790;
echo $num;
returns:
2351
Can somebody explain why I get 2351 on the second script?
Integers that start with zero are consider octal. Because octal integers only use numbers from 0 to 8 everything from the 9 on are ignored.
So 00445790 becomes 004457 which is 2351 in decimal.
Related
I am very new face to PHP. I read that dechex(255) will give the corresponding hex value ff in PHP.
I need the hex value of -105. I tried dechex(-105) and got the result as ffffff97. But I just only want 97 to do some kind of stuffs.
In Java I know that a bit wise operation with 0xff gave us the result 97 that is (byte)-105 & (byte)0xff = 0x97.
Please to find the solution in PHP just like I've done in Java.
You can do it in php like this:
var_dump(dechex(-105 & 255))
to make it out of the final byte (example output below)
string(2) "97"
dechex() gives you a hexadecimal value for a decimal value between 0 and 2*PHP_INT_MAX+1 (unsigned int).
Anything below 0 or above 2*PHP_INT_MAX+1, will loop.
-105 is NOT 0xffffff97 , and it is not 0x97
0xffffff97 is 4294967191.
and 0x97 is 151.
If you want the hexadecimal representation of the negative number turned into a positive number, use the function abs().
$abs = abs(-105); // $abs becomes +105
$hex = dechex($abs); // $hex becomes 69
Either you want a binary negative value (ffffff97) or a signed value
// for a signed value
$i = -105;
if($i < 0)
echo '-'.dechex(abs($i));
else echo dechex($i);
If you want to remove front "f"
echo preg_replace('#^f+#', '', dechex($i));
I have very little programming experience but I am going over a php book and this block of code is confusing me.
If rand generates a random integer, how does this program use ABCDEFG in the array.
Can you please explain the program thank you. I know what the result is, I am just not sure how it get it.
<?php
$array = '123456789ABCDEFG';
$s = '';
for ($i=1; $i < 50; $i++) {
$s.= $array[rand(0,strlen($array)-1)]; //explain please
}
echo $s;
?>
It's using the array index so $array[11] would equal 'C'. rand() takes a range - in your example that's from 0 to strlen($array)-1 which is the length of the string, minus 1 since it's a 0 based index.
Break it down into parts:
strlen($array) - returns the length of the string in $array, which would be 17
strlen($array) - 1 => 16
rand(0, 16) - generate a random number between 0 and 16
$array[$random_number] - get the $random_number'th element of the array
Its just taking the length of the array with strlen($array). It doesn't matter what is in the string just the length. Then its generating a random number between 0 and the length of the string minus one.
Then it takes whatever character is in that position in the array (so $array[5] would be '6', $array[12] would be 'C', etc) and appending that to string $s. It then has a for loop to repeat it 50 times.
What you end up with is a random string that is 50 characters long and contains the numbers 1-9 and letters A-G.
This question already has answers here:
What's the function of the ~ bitwise operator (Tilde) [duplicate]
(3 answers)
Closed 9 years ago.
Consider:
php > $a = 12; // 1100
php > echo ~$a;
-13
I would expect the inverse of 1100 to be either 0011 (direct) or 11110011 (an entire byte). That would give a result to either 3 or 243. Whence cometh -13?
Again, for good measure, another unexpected result of the same type and explanation:
php > $b = 6; // 0110
php > echo ~$b;
-7
Why -7?
Look at this code:
<?php
$val = 6;
print "$val = ".decbin($val);
print "\n";
$val = ~$val;
print "$val = ".decbin($val);
It prints
6 = 110
-7 = 11111111111111111111111111111001
At first you have 110. As my php uses 32 bits, after inverting all the bits, we get this huge number. As the 1-st bit is 1, php interprets it as a negative value, stored, using two's-complement representation. To find out, the modulus of the negative value, stored in this notation, we
invert the digits:
110
add one to the result:
111
which gives us 7
So, the value is -7
http://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html
Why -7?
6 is 00000000000000000000000000000110, so ~6 is ~00000000000000000000000000000110, and that's equal to 11111111111111111111111111111001. Because a signed datatype is used, the first bit indicates whether the number is positive or negative (positive = 0 and negative = 1). Because it is Two's complement, you should convert the binary number to decimal using this way:
Invert the binary number. You get 00000000000000000000000000000110
Convert 00000000000000000000000000000110 (a positive binary number) to a decimal number. You get 6
Add 6 up with one: you get 7
Make it negative: you get -7
PHP's documentation on this function is a bit sparse and I have read that this function compares ASCII values so...
echo strcmp('hello', 'hello');
//outputs 0 as expected - strings are equal.
echo '<hr />';
echo strcmp('Hello', 'hello');
//outputs -32, a negative number is expected as
//uppercase H has a lower ASCII value than lowercase h.
echo '<hr />';
echo strcmp('60', '100');
//outputs 5.
The last example is confusing me. I don't understand why it is outputting a positive number.
ASCII Value of 0 = 48
ASCII Value of 1 = 49
ASCII Value of 6 = 54
Total ASCII value of '60' = (54 + 48) = 102
Total ASCII value of '100' = (49 + 48 + 48) = 145
The strcmp() functions is saying that '60' is "greater" than '100' even though it seems that the ASCII value and string length of '100' is greater than '60'
Can anyone explain why?
Thanks
strcmp() returns the difference of the first non-matching character between the strings.
6 - 1 is 5.
When you look at it, you are probably not seeing the characters or digits—just the numbers
Because strcmp() stops at the first difference it finds. Hence the difference between the ASCII value of '1' and the ASCII value of '6'
6 is 5 "larger" than 1. This is lexical comparison. The first character is different, that's where the comparison stops.
example: 1.123 =>1 1.999 => 1
thanks.
$y = 1.235251;
$x = (int)$y;
echo $x; //will echo "1"
Edit:
Using the explicit cast to (int) is the most efficient way to to this AFAIK. Also casting to (int) will cut off the digits after the "." if the number is negative instead of rounding to the next lower negative number:
echo (int)(-3.75); //echoes "-3";
echo floor(-3.75); //echoes "-4";
floor()
will round a number down to the nearest integer.
EDIT: As pointed out by Mark below, this will only work for positive values, which is an important assumption. For negative values, you'd want to use ceil() -- but checking the sign of the input value would be cumbersome and you'd probably want to employ Mark's or TechnoP's (int) cast idea instead. Hope that helps.
You could use a bitwise operator.
Without:
echo 49 / 3;
>> 16.333333333333
With "| 0" bitwise:
echo 49 / 3 | 0;
>> 16
$y = 1.234;
list($y) = explode(".", "$y");
If your input can only be positive floats then as already mentioned floor works.
floor(1.2)
However if your integer could also be negative then floor may not give you what you want: it always rounds down even for negative numbers. Instead you can cast to int as another post mentioned. This will give you the correct result for both negative and positive numbers.
(int)-1.2
To remove all number after point use some php function
echo round(51.5); // Round the number, return 51.
echo floor(51.5); // Round down number, return 51.
echo ceil(51.3); // Round up number, return 52.