PHP: What is the result of $a+++$a++ [duplicate] - php

This question already has answers here:
Pre-incrementation vs. post-incrementation
(3 answers)
Closed 2 years ago.
php > $a = 4;
php > echo $a+++$a++;
php > 9
Why is the result equal to 9 not 8 ?
a++ will increment the value of $a, but return the original value that I held before being incremented. So $a++ return 4 and $a return 4, the result should be 8 = 4 + 4 ?

The first time you get a return value from $a++ the value will be 4. But the second time it will be 5, as you already incremented it just before:
9 = 4 + 5
The final value of $a is 6, as it has been incremented twice.

Related

How do Generate a random number between 1 and 2 in PHP [duplicate]

This question already has answers here:
Random Float between 0 and 1 in PHP
(13 answers)
Closed 4 years ago.
How do Generate a random number between 1 and 2 with a possible result of both integer or float in PHP
I try to use rand(1,2) or mt_rand(1,2) but i keep getting between 1 and 2.
I want my result so be either 1.3 , 1.1, 1.3 ,2
What about this with mt_rand() and mt_getrandmax()?
<?php
function randomFloat($min = 0, $max = 1) {
return number_format($min + mt_rand() / mt_getrandmax() * ($max - $min), 2, '.', ''); // 2 decimal places
}
echo randomFloat(1,2);
DEMO: https://3v4l.org/5a5U1

How can I round down to the nearest multiple of 3 in PHP? [duplicate]

This question already has answers here:
Best way to round down in PHP
(3 answers)
Round up to nearest multiple of five in PHP
(12 answers)
Closed 4 years ago.
How can I round an integer down to the nearest multiple of 3 using PHP? And have <3 be 0.
For example:
4 becomes 3
10 becomes 9
9 becomes 9
8 becomes 6
And so on...
Assuming $x is your input:
print $x-$x%3;
Is this what you looking for.
/**
* Round an integer down to the nearest multiple of the given multiple
* #param integer $number
* #param integer $multiple
* #return integer
*/
function round_down_to_multiple_of( int $number, int $multiple = 3 ): int
{
return (int)($multiple * floor( $number/$multiple ));
}
# TESTS
$numbers = [ 10, 9, 8, 1, 0 ];
foreach( $numbers as $number ){
printf( '%d became %d'.PHP_EOL, $number, round_down_to_multiple_of( $number, 3 ) );
}
After running the above test I get the following results:
10 became 9
9 became 9
8 became 6
1 became 0
0 became 0
I know there are good answers here but this one is for larger numbers for the sake of alternative, using bcmath.
function floor_to_multiple($number, $multiplier) {
return bcsub($number, bcmod($number, $multiplier));
}
If you want for 3, you may use function below:
function round_nearest_3($value){
return $value-$value%3;
}
If you want to return function for any value use function below:
function round_nearest_mod($value,$mod){
return $value-$value%$mod;
}
Examples:
echo round_nearest_3(4); // becomes 3
echo round_nearest_3(10); // becomes 9
echo round_nearest_3(9); // becomes 9
echo round_nearest_3(8); // becomes 6
echo round_nearest_mod(4,3); // becomes 3
echo round_nearest_mod(10,3); // becomes 9
echo round_nearest_mod(9,3); // becomes 9
echo round_nearest_mod(8,3); // becomes 6
<?php
//requested number
$num = 10;
//calc
for($i=1;($i*3)<=$num;$i++)$answer[] = $i;
$answer = max($answer)*3;
//print result
print_r($answer);
I misread that even if it is a perfect fit, it should round down to the last preceeding incremental:
4 becomes 3
10 becomes 9
9 becomes 6
8 becomes 6
Therefore, if for some reason you need this; your answer would be:
print $x-($x-1)%3-1;
I made a mistake in comprehending the question but thought this answer was curious enough to be worth posting.

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.

Need help on conversion statement [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 9 years ago.
I'm new to python, I should convert the following operation in php:
if len (s) == 88:
return s[48] + s[81:67: -1] + s[82] + s[66:62: -1] + s[85] + s[61:48 : -1] + s[67] + s[47:12: -1] + s[3] + s[11:3: -1] + s[2] + s[12]
I do not understand exactly what you mean this formatting s[a:b: -c].
s = "012345678910"
a = 7
b = 4
c = 1
print s[a:b: -c]
returns:
765
So to explain, its a slice between position 7 in the string and position 4, first element is element 0, in steps of minus one. That is to say, you get every element between 7 and 4 not including 4.
The general form of a slice as detailed in the docs is slice(start, stop[, step]).

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