What's going on with this =+ assignment? - php

I accidentially stumbled over the following code snippet that had me scratch my head for quite a while:
$sum = 0; $realSum = 0;
foreach (range(0,5) as $number) {
$sum =+ $number;
$realSum += $number
}
echo "Sum: $sum, RealSum: $realSum";
// prints 'Sum: 5, RealSum: 15'
?>
What I wanted was obviously the += statement, but somehow PHP wouldn't raise any errors or warnings about the += at all. My IDE also didn't complain about it.
What's this =+ thing in $sum =+ $number? I couldn't find anything on this in the official documentation.

This sign is the sign of the number. So if $number equals 3, if you put -$number, the value will be -3.

$sum =+$number;
This one is like $sum = 0 + $number, it gets last value of the array which is 5
$realSum += $number;
But this one is like $realSum = $realSum + $number

Related

Decoding function to better understand

I am revisiting PHP and want to relearn where i lack and i found one problem, i am unable to understand the following code, where as it should output 6 according to the quiz, i got it from but i broke it down to simple pieces and commented out to better understand, according to me the value of $sum should be 4, but what i am doing wrong, maybe my breakdown is wrong?
$numbers = array(1,2,3,4);
$total = count($numbers);
//$total = 4
$sum = 0;
$output = "";
$i = 0;
foreach($numbers as $number) {
$i = $i + 1;
//0+1 = 1
//0+2 = 2
//0+3 = 3
//0+4 = 4
if ($i < $total) {
$sum = $sum + $number;
//1st time loop = 0 < 4 false
//2nd time loop = 0 < 1 false
//3rd time loop = 0 < 2 false
//5th time loop = 0 < 3 false
//6th time loop = 4 = 4 true
//$sum + $number
//0 + 4
//4
}
}
echo $sum;
This is very basic question and might get down vote but it is also a strong backbone for people who want to become PHP developer.
You don't understand the last part in the loop. It actually goes like this now:
if($i < $total) {
$sum = $sum + $number;
//1st time loop: $sum is 0. $sum + 1 = 1. $sum is now 1.
//2nd time loop: $sum is 1. $sum + 2 = 3. $sum is now 3.
//3rd time loop: $sum is 3. $sum + 3 = 6. $sum is now 6.
//4th time loop: it doesn't get here. $i (4) < $total (4)
//This is false, so it doesn't execute this block.
}
echo $sum; // Output: 6
I altered your script a little so that it will print out what it's doing as it goes. I find it useful to do this kind of thing if I'm having a hard time thinking through a problem.
$numbers = array(1,2,3,4);
$total = count($numbers);
$sum = 0;
$i = 0;
$j = 0;
foreach($numbers as $number) {
$i = $i + 1;
echo "Iteration $j: \$i +1 is $i, \$sum is $sum, \$number is $number";
if ($i < $total) {
$sum = $sum + $number;
echo ", \$i is less than \$total ($total), so \$sum + \$number is: $sum";
} else {
echo ", \$i is not less than \$total ($total), so \$sum will not be increased.";
}
echo '<br>'; // or a new line if it's CLI
$j++;
}
echo $sum;
Lets Explain
Your initial value of $i is 0 but when you start looping you increment it by 1, so the start value of $i is 1.
When checking the condition you did't use equal sign to check for the last value whether you start value is 1. So its clear that your loop must be run for 1 less of total.
$i = 0;
foreach($numbers as $number) {
$i += 1;
if ($i < $total)
$sum += $number;
}
echo $sum;
Analysis
Step: 1 / 4
The value of $number is: 1 And The value of $i is: 1
Step: 2 / 4
The value of $number is: 2 And The value of $i is: 2
Step: 3 / 4
The value of $number is: 3 And The value of $i is: 3
When the loop again go for a check the value of $i increased by 1 and at 4. So trying to match the condition if ($i < $total), where the value of $i and $total is equal, so it will return false. So the loop only run for 3 time.
Result
6

Bailey-Borwein-Plouffe in php trouble

I am trying to implement the BBP algorithm in php. My code is returning a decimal which i thought was odd as it should be in hex. I was told to convert to decimal from hex by multiplying by 16 also but now its all just wrong. Here is a sample:
$n1=$n2=$n3=$n4=$n5=$n6=$n7=$n8 =0;
$S1=$S2=$S3=$S4=$S5=$S6=$S7=$S8 = 0; //initializing
$k = 0;
$m1= 8*$k + 1;
$m2 = 8*$k + 4;
$m3 = 8*$k + 5;
$m4 = 8*$k = 6;
$b =16;
$e=$n-$k;
while($k<$n){ //Sum 1 of 8
$S1 +=Modular($b, $m1, $e)/$m1; //see Moduler_Expansion.php
$k++;
}
$k = $n +1; //redefine for second sum, and every other
while($k<$limit){ //Sum 2 of 8
$S2 += (pow($b,$n-$k))/($m1);
$k++; //now repeat similar process for each sum.
}
and I repeat the process for each term of BBP then:
$S = 4*($S1 + $S2) - 2*($S3+$S4) -($S5+$S6) - ($S7+$S8);
`
Following the wiki page I then strip the integer and multiply by 16, but for $k =0 I get; 3.4977777777778
and for $k = 1: 7.9644444444448.
I dont think these are right, it could just be i do not know how to interpret th ouput properly. Can anyone offer any advice?

PHP Yield keyword - loop iteration

I am using PHP generator and can't explain this behavior.
This is the code I tried
<!-- language: PHP -->
<?php
function myfun($num1, $num2, $ctr = 1) {
for ($i = $num1; $i <= $num2; $i =+ $ctr) {
yield $i;
}
}
echo 'Odd numbers: ';
foreach(myfun(1, 7, 2) as $num) {echo $num;};
?>
Can someone explain me this behavior using PHP yield, entering a infinite loop?
result: Odd numbers: 122222222222222222222222222222222...............
Note: $i += $ctr works as expected
result: Odd numbers: 1357
The problem lies in the =+ operation, you probably meant to type +=, which would do the trick:
<?php
function myfun($num1, $num2, $ctr = 1) {
for ($i = $num1; $i <= $num2; $i += $ctr) {
yield $i;
}
}
echo 'Odd numbers: ';
foreach(myfun(1, 7, 2) as $num) {echo $num;};
Result: Odd numbers: 1357
$i =+ $ctr
=+ is not an operator. This will essentially do $i = $ctr.
The first time the loop occurs $i is set to $ctr, in this case this is 2. After this it is continually set to 2 and never goes higher. Hence the infinite loop. Use += instead.

Adding fractions by n denominator in for_loop (PHP)

I'm trying to make it so it loops fractions as 1, 1/2, 1/3, 1/4, etc up to 1/100, then add all them together and only show the sum and not the chain of fractions.
The code I have so far looks like this:
<?
$sum = 0;
for($n = 1; $n<=1/100; $n+=1/$n)
{
$sum = $sum + $n;
}
echo $sum;
But it never gives me the right answer, which is 5.18. Any advice?
You need to limit the loop. And you need to increment $n
$sum = 0;
for($n = 1; $n<=100; $n++)
{
$sum = $sum + (1/$n);
}
echo $sum;
//answer 5.1873775176396
Just FYI, for a simple operation like this, your for loop does not even need a body.
$sum = 0;
for($n=1; $n<=100; $sum +=(1/$n++));
echo $sum;
See example four in the php for documentation.
for($n = 1; $n<=; $n+=1/$n)
You must define a limit insecond parameter of for, it runs in infinity
EDIT:
Between second and third parameter, you must insert ;
<?php
$sum = 0;
for ($n=1; $n<=100; $n++) {
$sum = $sum + (1/$n);
}
echo $sum;
// or use round if you want it rounded
//echo round($sum,2);
This code is PSR-2 compliant
Try this:
for ($n = 0, $sum = 0; $n < 100; $n++, $sum += 1 / $n);
echo $sum;

Variable merging which doesn't make sense

Consider this code:
$x = 1.4;
$i1 = 0.5;
$i2 = 0.4;
echo ($i1 + $i2 = $x); // Outputs 1.9
Why is this? I've tried searching for this kind of variable setup without results. Is the variable $i2 being ignored? Why use this over echo ($x + $i1);? It outputs the same result.
The point is that it does two things in one statement.
It is shorthand for:
$i2 = $x;
echo ($i1 + $i2);
The assignment happens inline, saving the separate line. Not ideal style, but often used in if(), while() and other control statements.
that would be $i1 + the assignment.
The assignment evaluates to $x ($i2 = $x )
the end result is echo 0.5 + 1.4.
Even php has operator priorities http://php.net/manual/en/language.operators.precedence.php.
= is treated before +, which means that this is what happens:
echo ($i1 + ($i2 = $x));

Categories