Adding or condition in for loop - php

How can I add or in for loop check?
for example:
for ($i=1; ($i <= $untilPoint) or ($i <= $points); $i++){
.. code ...
}

Exactly what you typed is valid PHP. For example, this program:
<?
$untilPoint = 3;
$points = 5;
for ($i=1; ($i <= $untilPoint) or ($i <= $points); $i++){
echo("$i\n");
}
?>
prints this:
1
2
3
4
5
(tested in PHP 5.2.17, run from the Bash prompt).
That said, I wonder if maybe you really need and rather than or? If $i is an index into an array of points, where $points is the number of points and $untilPoint is an arbitrary cutoff, then you want and, not or.

This is how it should be done, probably it runs slightly faster.
You can separate with , see http://php.net/manual/en/control-structures.for.php for more information
<?
$untilPoint = 3;
$points = 5;
for ($i=1; $i <= $untilPoint, $i <= $points; $i++){
echo($i , "\n");
}
?>

Related

Exam Qn: Convert do while loop to for loop (PHP)

Recently, my exams got over. My last exam was based on PHP. I got the following question for my exam:
"Convert the following script using for loop without affecting the output:-"
<?php
//Convert into for loop
$x = 0;
$count = 10;
do
{
echo ($count. "<BR>");
$count = $count - 2;
$x = $x + $count;
}
while($count < 1)
echo ($x);
?>
Please help me as my computer sir is out of station and I am really puzzled by it.
Well, If I understand well, You have to use "for" loop, instead of "do...while", but the printed text must not change.
Try:
$count = 10;
$x = 0;
$firstRun = true;
for(; $count > 1 || $firstRun;) {
$firstRun = false;
echo ($count . "<BR>");
$count -= 2;
$x = $x + $count;
}
echo ($x);
By the way loop is unnecessary, because $count will be greater than 1 after the first loop, so the while will get false.
EDIT
$firstRun to avoid infiniteLoop
$count in loop
EDIT
Fixed code for new requirement
Removed unnecessary code
Hmmm I don't know if your teacher wanted to own you... but the do{} will execute only once since $count is never < 1.
The output of your teacher's code is:
10
8
I presume there was a mistake in the code and the while would be while($count > 1) which would make more sense (since it's weird to ask for a loop to output only 10 8) and would result in this output:
10
8
6
4
2
20
Then a good for() loop would have been :
$x = 0;
$count = 10;
for($i = $count; $i > 1; $i -= 2)
{
$count -= 2;
echo $i . "<br>";
$x += $count;
}
echo $x;
Which will output the same values.
If you can, ask your teacher for this, and comment the answer ^^ ahahah

Why does this loop execute only once?

I found such php code :
$a = 5;
$i = 4;
for($i = &$a; $i < 10; ++$i);
echo "a=$a, i=$i";
I would expect this loop to be executed 4 times since $i becomes a reference to a$(right?). However loop is executed only once and outputs :
a=10, i=10
I can't figure outh why it works like this. Any ideas?
It works because the for statement is being processed like a single line statement as a result of the semicolon.
for($i = &$a; $i < 10; ++$i);
can also be seen as
for($i = &$a; $i < 10; ++$i) echo $i;
Without the curly brackets, the following line will be considered part of the for statement, your line being just a semicolon to indicate the end of the line.
The proper code would look like this.
$a = 5;
for($i = $a; $i < 10; ++$i) echo "a=$a, i=$i";

Explain Alternative PHP For Loop Syntax: for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);

This example has been given as an alternative example (example 4 to be precise) for writing for loops on PHP.net.
for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);
I understand for loops, I just don’t understand why the variable, $j, is declared in this version of writing a for loop that prints the numbers 1 to 10.
FYI: Removing the variable from the for loop makes absolutely no difference to the result.
I think that it's just here for illustrate the fact that you can use multiple statement with commas.
It's useless here but show an example of the syntax for :
[...] Each of the expressions can be empty or contain multiple expressions separated by commas. In expr2, all expressions separated by a comma are evaluated but the result is taken from the last part. [...]
While it doesn't seem to be necessary in this example. It appears that $j is storing the summation of the iterations:
1+2+3+4+5+6+7+8+9+10 = 55
Which can be useful in some situations. So that would mean this style of looping is for the equivalent of doing several operations on each iteration, such as getting the summation, average, largest value, etc. The point of the example is that you can apply several statements separated by commas.
Explanation
for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);
for loops takes three part separated by semicolon (;).
Initialize
Compare and test
Increment or Decrements.
Here $i=1, $j=0 is initialization.
$i<=10; is compare and test
$j += $i, print $i, $i++ is increment or decrements part
Now in your increment or decrements part you have three task.
1. is increment $j with last $i
2. print $i
3. increment $i by 1
So in your program $j is not useful. Because it is not taking part of
either print or compare and test.
So the loop is just very simple if you remove $j from every where and write it as
for ($i = 1; $i <= 10; $i++){
print $i;
}
But that $j variable could be used after the loop where from you have taken this code block.
LIKE
for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);
print $j;
We can remove $j and have the same result:
for ($i = 1; $i <= 10; print $i, $i++);

Display random files from a directory?

how to display random files from a directory? The code below only shows random 1 file, output should be 10 files.
<?php
$path = "/files";
$files = scandir($path);
shuffle($files);
for($i = 0; ($i < count($files)) && (!is_file($files[$i])); $i++);
echo $files[$i];
?>
for($i = 0; ($i < count($files)) && (!is_file($files[$i])); $i++);
^
Putting the semicolon there terminates the for loop statement, hence it doesn't actually do anything, and just loops until the condition is false. Remove the semi-colon to fix.
As a clearer example, take the following code:
for($i = 0; $i < 5; $i++);
echo $i;
This will loop five times, as the for loop body statement is blank (due to the statement terminator, the semicolon). Because indentation doesn't matter in PHP, the echo $i will only echo once at the end of the loop, when $i == 5. You can view it better as the following:
for($i = 0; $i < 5; $i++)
;
echo $i;

Not sure the loop I need to use every two variables

I have a problem where I have an array $user.
I have $_SESSION['players'] which has the total amount of $user.
I need a function where I can take the user1 and 2 and use them. Then move on to user3 and 4 and use them, and so on.. until I have used all the players. Obviously the total $user[$i] would be players-1.
Anyone have a solution for this?
Thanks
Would this suit your needs? This requires that there be an even number of players to work properly though, unless you stick in a check for odd numbers:
for ($i = 0; $i < $_SESSION['players']; $i += 2) {
$userA = $user[$i];
$userB = $user[$i + 1];
// Do things with $userA and $userB variables...
}
just because you're taught how to use for loops in one way does not mean that you're stuck continuing to use them the way you were taught:
$length = count($users);
$length = $_SESSION['players'];
for ($i = 0; $i < $length; $i += 2)
{
if (!isset($user[$i], $user[$i + 1])) break;
$userOne = $user[$i];
$userTwo = $user[$i+1];
//do stuff
}
I realized that isset wasn't necessary, the for call could be modified more:
$length = $_SESSION['players'];
for ($i = 0; ($i + 1) < $length; $i += 2)
{
$userOne = $user[$i];
$userTwo = $user[$i+1];
//do stuff
}
EDIT to change how the length was calculated:
EDIT to validate that user exists
EDIT to add consolidated version

Categories