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

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

Related

Any php function to to drop decimal part if its zero [duplicate]

This question already has answers here:
Remove useless zero digits from decimals in PHP
(29 answers)
Closed 6 years ago.
javascript Number function is handy when dropping undesired zero if decimal part is zero e.g
Number(2.00)
2
Is there such function in php or any alternative
I think this works that way by default in PHP. If you use proper number type like float or double.
If you're using string then you need to map
$a = '2.00';
echo (float)$a; // 2
Example of using float
$a = 2.00;
echo $a; //2
or
2.00 + 0; //2
If you want to format the number to show decimal part 2.00 you need to use number_format function (http://php.net/manual/en/function.number-format.php)
$a = 2.00
echo number_format($a, 2); // 2.00
You can achieve this by adding zero ... for example
echo "Result " . (2.00 + 0) . "\n";
Result 2
you can use floatval()
echo floatval('2.00'); //2
echo floatval('2.23'); //2.23
See the documentation for more info
you can use intval() or any roundoff function
$_float = 1.9020441;
$_float = explode(".", $_float);
echo strlen($_float[1]);`

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.

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

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)

What does the percent sign mean in PHP?

What exactly does this mean?
$number = ( 3 - 2 + 7 ) % 7;
It's the modulus operator, as mentioned, which returns the remainder of a division operation.
Examples: 3%5 returns 3, as 3 divided by 5 is 0 with a remainder of 3.
5 % 10 returns 5, for the same reason, 10 goes into 5 zero times with a remainder of 5.
10 % 5 returns 0, as 10 divided by 5 goes exactly 2 times with no remainder.
In the example you posted, (3 - 2 + 7) works out to 8, giving you 8 % 7, so $number will be 1, which is the remainder of 8/7.
It is the modulus operator:
$a % $b = Remainder of $a
divided by $b.
It is often used to get "one element every N elements". For instance, to only get one element each three elements:
for ($i=0 ; $i<10 ; $i++) {
if ($i % 3 === 0) {
echo $i . '<br />';
}
}
Which gets this output:
0
3
6
9
(Yeah, OK, $i+=3 would have done the trick; but this was just a demo.)
It is the modulus operator. In the statement $a % $b the result is the remainder when $a is divided by $b
Using this operator one can easily calculate odd or even days in month for example, if needed for schedule or something:
<?php echo (date(j) % 2 == 0) ? 'Today is even date' : 'Today is odd date'; ?>
% means modulus.
Modulus is the fancy name for "remainder after divide" in mathematics.
(numerator) mod (denominator) = (remainder)
In PHP
<?php
$n = 13;
$d = 7
$r = "$n % $d";
echo "$r is ($n mod $d).";
?>
In this case, this script will echo
6 is (13 mod 7).
Where $r is for the remainder (answer), $n for the numerator and $d for the denominator. The modulus operator is commonly used in public-key cryptography due to its special characteristic as a one-way function.
Since so many people say "modulus finds the remainder of the divisor", let's start by defining exactly what a remainder is.
In mathematics, the remainder is the amount "left over" after
performing some computation. In arithmetic, the remainder is the
integer "left over" after dividing one integer by another to produce
an integer quotient (integer division).
See: http://en.wikipedia.org/wiki/Remainder
So % (integer modulus) is a simple way of asking, "How much of the divisor is left over after dividing?"
To use the OP's computation of (3 - 2 + 7) = 8 % 7 = 1:
It can be broken down into:
(3 - 2 + 7) = 8
8 / 7 = 1.143 #Rounded up
.143 * 7 = 1.001 #Which results in an integer of 1
7 can go into 8 1 time with .14 of 7 leftover
That's all there is to it. I hope this helps to simplify how exactly modulus works.
Additional examples using different divisors with 21.
Breakdown of 21 % 3 = 0:
21 / 3 = 7.0
3 * 0 = 0
(3 can go into 21 7 times with 0 of 3 leftover)
Breakdown of 21 % 6 = 3:
21 / 6 = 3.5
.5 * 6 = 3
(6 can go into 21 3 times with .5 of 6 leftover)
Breakdown of 21 % 8 = 5:
21 / 8 = 2.625
.625 * 8 = 5
(8 can go into 21 2 times with .625 of 8 leftover)

Categories