PHP: What does the '&' character mean? [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Reference - What does this symbol mean in PHP?
What does adding a '&' mean in this snippet I found?
$i = 10;
if($i&1){
echo "$i is odd";
}
else {
echo "$i is even";
}

It's the bitwise AND operator. in your case, it takes the binary representations of 10 and 1 and performs the logical AND operation on the individual bits.

That's a binray and. So for example 148 (binary 10010100) & 136 (binary 10001000) will be 128 (binary 10000000). So $i & 1 is either 1 (true) or 0 (false)

Related

Question about calculating Average in PHP [duplicate]

This question already has answers here:
How to calculate correctly in php?
(6 answers)
Closed 1 year ago.
I am a newbie learning PHP and i am trying to find average of 3 numbers but not getting the correct answer. I don't know where i am going wrong.
function percentage($math,$eng,$sc){
$s = $math+$eng+$sc / 3 ;
return $s;
}
$p = percentage(10,20,30);
echo $p;
I am getting the ansewer as 40 whereas i am supposed to get 20. Kindly check if there is any error.
Return value is right. Check operators precedence.
If you want 20 as return value code is:
$s = ($math+$eng+$sc) / 3 ;
You forgot to use parentheses:
$s = ($math+$eng+$sc) / 3 ;
All things together:
function percentage($math,$eng,$sc){
$s = ($math+$eng+$sc) / 3 ;
return $s;
}
echo percentage(10,20,30);

A confision of increment operator [duplicate]

This question already has answers here:
Increment behavior on strings - PHP easter egg?
(3 answers)
Closed 7 years ago.
$a = 'z99';
var_dump(++$a);
Above code will output aa00.I'm very confused about this output. Can somebody help me?
That's because PHP follows Perl's convention.
You can find more information on http://php.net/manual/en/language.operators.increment.php
So because this is a string the 99 goes to 00 -> next increment and the Z goes to AA. If you would have done it with Y99 it would have been Z00.
$s = 'W';
for ($n=0; $n<6; $n++) {
echo ++$s . PHP_EOL;
}
Returns:
X
Y
Z
AA
AB
AC
Note that decrementing strings won't work in PHP.
$s = 'W';
for ($n=0; $n<6; $n++) {
echo --$s . PHP_EOL;
}
Returns:
W W W W W W

Unexpected bitwise operation result [duplicate]

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

what does this mean return($var & 1); [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
callback function return return($var & 1)?
(8 answers)
Closed 9 years ago.
in PHP in the line below return($var & 1); What does this mean, what does the & mean in this context?
<?php
function test_odd($var)
{
return($var & 1);
}
$a1=array("a","b",2,3,4);
print_r(array_filter($a1,"test_odd"));
?>
The & (ampersand) operator does bitwise AND.
If $var is odd it returns 1, otherwise 0.
Let's say $var = 13 , that in binary is 1101 (because 13 = 2^3 + 2^2 + 2^0).
When you do 1101 & 0001 you get 0001. So you can either get 1 if $var has its last bit 1 (meaning it's odd) or 0 if $var has 0 as the last bit, meaning that $var is written as a sum of powers of two, without 2^0` meaning its even.

Unfamiliar usage of '%' in PHP script [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 9 years ago.
I found the following line of code in a PHP script and have never seen anything like it before:
$a = ($ba%10)
What does this do?
Its is PHP's Arithmetic Operators
The result of the modulus operator % has the same sign as the dividend — that is, the result of $a % $b will have the same sign as $a. For example:
<?php
echo (5 % 3)."\n"; // prints 2
echo (5 % -3)."\n"; // prints 2
echo (-5 % 3)."\n"; // prints -2
echo (-5 % -3)."\n"; // prints -2
?>
Click PHP.NET for more information!
It tells you the remainder of a division calculation. So 25%8 would be 1. If $ba = 101 then $ba%10 would equal 1.
% is the modulus operator, it gives you the remainder of integer division.
e.g. 87 % 10 = 7

Categories