Why is 1 not being outputted? (Simple php problem) - php

I have a question which I don't know the answer of. I've been thinking about it for a while.
The following code:
$i = 1;
while($i < 10)
if(($i++) % 2 == 0)
echo $i;
It correctly outputs 3579, but why isnt 1 also included in the output?
I'm a beginner with PHP and am looking forward for someone to help me.
Thank you very much! :D

Two modifications:
$i = 0; // Make it 0 from 1
while($i < 10)
if(($i++) % 2 == 0)
echo "<br/>".$i; // Make $i instead of $1
Output:
1
3
5
7
9
Program hand run:
1) Set $i to 0.
2) If it is greater than 10, go ahead.
3) Increment it by 1
4) So, for $i => 0->1, 1->2
4) if new $i is even, print it. (So for first loop iteration, you are have $i -> 1 instead of 0 because of ++$i

Related

(PHP) Why does this generator-script adds the starting value + the step-value to the starting value?

the title may be pretty confusing. Here's a script which requires a start-end and step value to add numbers to the start value:
function addieren($start, $ende, $schritt = 1) {
if ($start < $ende) {
$erg = 0;
for ($i = $start; $i <= $ende; $i += $schritt) {
$erg += $i;
yield $erg;
}
}
}
foreach (addieren(2, 10, 2) as $erg) {
echo $erg . "<br>";
}
So the start value is 2, the end value is 10 (but it's not ending at 10 as the result shows), and it should add 2 to the $i every step.
Here's the output:
2
6
12
20
30
The first output is clear to me, since the $erg was 0 and it added the 2 because of 2 steps.
But the next output is 6 and I don't get why. In the second loop, $i is 2 and and the script says: $i += $schritt so when $i is 2 and the $schritt value is also 2, why doesn't that output 4 as the second output? Hope you get what I mean.. I guess it's a pure logic error in my head.

why does this php code return me 13 and not 3? I'm really confused

Can someone tell me why the output of echo of this Code is 13?
$a=10;
$b=2;
$j=$a/2;
for ($i=0;$i<$j;$i++){
if ($i % $b == 1)
echo "$i";
}
Try this
<?php
$a=10;
$b=2;
$j=$a/2;
echo $j;
echo "<br>";
for ($i=0;$i<$j;$i++){
if ($i % $b == 1)
echo "$i";
echo "<br>";
}
?>
because $a have 10 value , $b have 2 value and $j have 5 value
when start loop then $i start from 0 loop have max 5 loops from 0 to 4
so
then loop start first then $i have 0 value so $i % will be equal
1 so nothing display
when start second loop then $i have 1 value then $i % will be
equal 1 so display 1 because now $i have 1 value
when loop run third time then $i have 2 value then $i % will be
equal 0 so nothing display
when loop run fourth time then $i have 3 value then $i % will be
equal 1 so display 3 because now $i have 3 value
when loop run fifth time then $i have 4 value then $i % will
be equal 0 so nothing display
$a=10;
$b=2;
$j=$a/2; //which will be 5
for ($i=0;$i<$j;$i++){ //the loop executes 5 times
if ($i % $b == 1) // this condition satisfies when $i becomes 1 && 3
echo "$i"; //1 and 3 will be printed.
}
Check The comments written in your code
You code is like this,
echo 1; echo 3;
output 13
I hope that will help you understand how your code works.
$a=10;
$b=2;
$j=$a/2;
for ($i=0;$i<$j;$i++){
if ($i % $b == 1)
echo "output";
echo "$i";
}

Incrementing numbers doesn't give an error

How come this code below echo's 2 and does not give an error, does it just ignore +1+2+3+4 ?
I've searched but couldn't find an answer.
<?php
$i = 1;
$i+++1+2+3+4;
echo $i;
That line:
$i+++1+2+3+4;
Says:
Increment $i
Add the value of $i pre increment to +1+2+3+4, but don't store the result anywhere.
Hence $i == 2.
If you wouldn't want it to be ignored, you should store the result:
$i = $i+++1+2+3+4;
You never assign the completed operation anywhere:
These two are functionally equivalent:
$i++;
$i = $i + 1;
both will increment $i by 1, and save that incremented value in $i
With $i+++1+2+3+4 you're essentially executing
($i++) + 1 + 2 + 3 + 4
which is
$i = $i + 1;
1 + 2 + 3 + 4; // useless, result not stored anywhere
so which increments $i by 1, saves that to $i, then does the other additions. But since those aren't being saved anywhere, the result is thrown away.
if you had
php > $i = 1;
php > $i = $i+++1+2+3+4;
^^^^^----add this
php > echo $i;
11
then it would have worked as you expect.
All is fine. You just forgot the assignment, so i is affected only by ++ operator:
<?php
$i = 1;
$x = $i+++1+2+3+4;
echo "{$i} vs "{$x}";
would return
2 vs 11
$i++ means add 1 to $i.
and like python, the +1+2+3+4 means add the value of $i pre increment to +1+2+3+4 but don't store it anywhere.(so no memory address or anything like that...).
so what you get is just $i==2

PHP + Echo Data Routinely

I have the following PHP code working 'successfully' to display URL's:
<?php
for ($i = 0; $i <= $json->domaincount; $i++) {
echo '<td class="domainList">'.$json->$i.'</td>';
}
?>
Every forth echo I want to also
echo </tr><tr>
to start a new line in the table.
Is there an easy way to know which is every forth count?
I have $i which increments from 0 up so when it gets to 3, 7, 11 etc I need to change table line.
thx
Try this:
if ( ( $i + 1 ) % 4 == 0 ) { echo '</tr><tr>'; }
This is using the modulus operator. It divides a number and returns the remainder, so 7 % 4 = 3 (because 4 fits in once, and three is left over) and 8 % 4 = 0 (because 4 fits in evenly and there are no left overs)
Look into modulo operands (% in PHP). So the check that i % 4 == 0 would give you every fourth row.
<?php
for ($i = 0; $i <= $json->domaincount;) {
echo '<td class="domainList">'.$json->$i.'</td>';
if (!(++$i & 3))
echo '</tr><tr>';
}
?>
!($i & 3) is just a quick way of writing $i % 4 === 0 without having to use modulus. You can only use that trick for modding by powers of 2.

Each time the counter hits 6 - A basic math issue help

Using a php like so..
for($i = 0; $i < 30; $i++) ...
I have this html element that is rendered several times. I want to, each time we arrive at the sixth element, it adds a "style:margin-right: 0px;" for example.
My question is:
How can we find always the 6th element ?
Update: So that can mark the 6th element, then the 12th element, then the 18th element then the 24th and, at least, the 30th.
Thanks in advance,
MEM
You can use the modulo operator, %:
for ($i = 0; $i < 30; $i++) {
if ($i % 6 == 5) {
# Add what you want---I don't use PHP much
}
}
The modulo operator, %, divides the left hand side by the right hand side, and then reports the remainder of the result. So, for instance, 15 % 6 == 3, because 15 == 6*2 + 3. In the expression a % b == c, c will range from 0 to b-1. If you had $i % 6 == 0 in the above test, it would style the first element, the seventh element, etc.; this way, it'll style the sixth element, the twelfth element, etc. This is because when you're on the sixth element, $i == 5, and 5 % 6 is of course 6. For more information, check out what Wikipedia has to say about the modulo operation.
Check that the mod of $i and 6 is 0 (means that $i is evenly divisible by 6).
for($i = 0; $i < 30; $i++) {
if($i % 6 == 0) {
// this is a sixth element
}
...
}
If you don't want this to happen on the first iteration ($i == 0), you'll also need to add that check to the if statement:
if($i > 0 && $i % 6 == 0){
}
you can try using modulus (%)
if(!($i % 6)) {
// add style
}
or
if(($i % 6) == 0) {
// add style
}
EDIT: Kaleb beats me to it =/

Categories