PHP: Confusion in if condition - php

I trying to print simple numbers from 1 to 10 using a for loop like this:
for($i = 0; $i <= 10; $i++){
if($i != 4 || $i != 6){
echo $i."<br/>";
}
}
Output:
0
1
2
3
4
5
6
7
8
9
10
I just want the output from 0 to 10 but the output should not contain the numbers 4 and 6.
It is working fine if I use the && operator but does not work if I use||.
I do not understand why this is not working - I think it should work with ||.

You don't want to print either 4 or 6, so you should be using &&.
The statement if($i != 4 || $i != 6) will trigger whenever $i is not equal to 4, or whenever $i is not equal to 6. Considering 4 is not equal to 6, it will trigger in both cases. It will reach $i = 4, and realise that $i is not equal to 6. This will step into the condition, as you say it only has to hold true for one or the other.
The statement if($i != 4 && $i != 6) implies that $i is not equal to 4 and $i is not equal to 6. Both conditions must hold true at the same time. When $i = 4, $i != 6 will be true, but $i != 4 will be false. Both conditions need to be true, so it will fail. Essentially, this could be rewritten as:
for($i = 0; $i <= 10; $i++){
if($i != 4) {
if($i != 6) {
echo $i."<br/>";
}
}
}
To skip over the numbers 4 and 6 in the loop, you have to use the and condition:
for($i = 0; $i <= 10; $i++){
if($i != 4 && $i != 6){
echo $i."<br/>";
}
}
Hope this helps!

You want &&. It would not work with ||; that means something different.
"x && y" means "only true if x is true and y is also true; otherwise false."
"x || y" means "true if either x is true or y is true; only false if both are false."
The contrapositive (i.e., "opposite") of ($i != 4 && $i != 6) is ($i == 4 || $i == 6).
Mixing in the || without swapping the comparisons as well means, in your case, "true if $i is not 4, or also true if $i is not 6." Since one of those cases must always true, the result is also always true.

<?php for ($i=0; $i < 10 ; $i++) {
if ($i != 4 && $i !=6){
echo $i."<br>";}}
?>

Related

In loop, check for 6/16/26/36, etc?

If you have a simple counter loop, how do you detect special patterns, for example, every 10 increments but at 6/16/26/36. $i needs to start at 0 too.
The only approach I can think of is this one, but obviously it doesn't work for large loops:
for ($i=0; $i < 1000; $i++) {
// if ( $i == 6 || $i == 16 || $i == 26...... etc ) { do something }
}
There's not going to be one single answer for all types of patterns, but so long as there is a pattern, you can figure it out:
for ($i=0; $i<1000; $i++) {
if (($i-6)%10 == 0) {
// every time $i minus 6 is evenly divisible by 10
}
}

Php for loop issue

Hi I'm Learning php and trying to do a for loop look like this wtih this code:
for($x=1; $x<=20; $x++){
echo $x;
$x = $x + 3; //5
echo "<br/>";
}
It's produce
1
5
9
13
14
But I want it should be...
1
5
10
15
20
for ($x = 0; $x <= 20; $x += 5) {
echo ($x == 0 ? 1 : $x), '<br>';
}
Or:
foreach (range(0, 20, 5) as $x) {
echo ($x == 0 ? 1 : $x), '<br>';
}
You can't produce the sequence without 1 extra condition because the delta differs in the first step:
1 + 4 ...
5 + 5
10 + 5
15 + 5
20 + 5
there are more than one solution.
one is:
for($x=1; $x<=20; $x++){
if(!($x % 5) || $x==1)
echo $x . "<br />";
}
Explination
% is the modulo operator. It returns the devision rest.
lets say $x is 3 than 3 % 5 would return 3 because the result 3/5 = 0 rest 3
if its $x is 10, it return 0. 10/5 = 2 rest 0
In the if-statement I use !-not operator. This turns around the result.
Because if takes 1+ (one and more) as true and 0- (zero and less) as false
So rest of 3 would be positiv (true) but in this case i want it to be false. So I turn arount the true/false with !
% - Modulo
R - Rest
1 % 5 = 0 R 1 // would say true to if
2 % 5 = 0 R 2 // would say true to if
3 % 5 = 0 R 3 // would say true to if
4 % 5 = 0 R 4 // would say true to if
5 % 5 = 1 R 0 // would say false to if
6 % 5 = 1 R 1 // would say true to if
and so on...
try this.
$x = 1;
for ($j = 1; $j <= 20; $j++) {
echo $x, "<br/>";
if ($x == 1) {
$x = $x + 4;
} else {
$x = $x + 5;
if($x > 20){break;}
}
}
if this answer is work for you please mark the answer.
Try this:
for($x=1; $x<=20; $x++) {
if($x%5==0 || $x==1) {
echo $x;
echo "<br>";
}
}

PHP Loop do action for each 10, 20, 30 etc [duplicate]

This question already has answers here:
PHP: How do you determine every Nth iteration of a loop?
(8 answers)
Closed 3 years ago.
In php i have loop e.g.
for ($i = 0; $i <= 1000; $i++) {
if ($i = 10 || $i == 20 || $i == 30 || $i == 40 || $i == 50 || $i == 60) {
echo $i;
}
}
imagine i need to echo $i every 10,20,30,40,50,60,..970,980,990 there should be way to not write 100 conditions in if statement. Is there some logical way to see if $i increased by 10 then do something like:
if ($i == $i+10) {
...
}
P.S. if possible i dont want to introduce another variable to count i need to find solution with using only $i
Try:
if ($i % 10 == 0)
That will trigger whenever your index is exactly divisible by 10.
rewrite your for loop to:
for ($i = 0; $i <= 1000; $i+=10) {
And I don't know whether it worked for you with commas like this (as in your initial post):
for ($i = 0, $i <= 1000, $i++) {
Skip extra looping:
for ($i = 10; $i <= 1000; $i=$i+10) {
echo $i;
}
Or if you still want to loop every single digit between:
for ($i = 0; $i <= 1000; $i++) {
if( $i % 10 === 0 ) {
echo $i;
}
}
Test Here
Put this inside your main loop. '%', or 'mod', gives you the remainder of $i / 10. If the remainder is '0', then you want to print.
if(0 === ($i % 10)) {
echo $i;
}

while($array = mysql_fetch_array($queryresults) && $i <= '9' ){ echo "stuff" }

So I have a query, and though my query can possibly return 20 results, I only want it to show the first 9 results. There is a dumb reason I'm not just limiting the query results to 9, for this purpose I need to know how to stop the while function if $i reaches 9.
Code is
$i = 0;
while($array = mysql_fetch_array($queryresults) && $i <= '9')
{
echo $array['id'];
$i++;
}
How do I get it to stop putting out more echoes after 9th result? Thank you!
Putting the limiting condition first will also make sure you only fetch 9 rows, not and 10 and one discard because && is called lazy (if the first part is false, it wont even look at the next part).
$i = 0;
while($i < 9 && $array = mysql_fetch_array($queryresults)){
echo $array['id'];
$i += 1;
}
or if you want to be fancy:
$i = 9;
while($i-- > 0 && $array = mysql_fetch_array($queryresults)){
echo $array['id'];
}
But I find this error prone ..
$i = 0;
while($array = mysql_fetch_array($queryresults) && $i++ < 9)
echo $array['id'];
}
Notes:
No need to put 9 in quotes
You can increment-after-compare by doing $i++ in the while loop condition (although you don't have to, you could also put it after echo $array['id'] in the while loop body).
Careful! You want < 9 and not <= 9 since starting at zero, or you'll get 10 iterations of the loop.
Cheers
Your $i++ needs to be inside the while loop
You should also have $i <= 9 instead of $i <= '9' but it should still work as is
'9' needs to be 9
$i = 0;
while($array = mysql_fetch_array($queryresults) && $i < 9){
echo $array['id'];
$i += 1;
}

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