For loop (php) that results in this: 12345678910987654321 - php

My niece is trying to create one for-loop (php), that results in this:
* 12345678910987654321
example for loop she tried:
for ($i = 1; $i <= 10; $i++ , $i = 10; $i <= 1; $i--) {
echo $i . ' ';
}
She can only use if's and elseif's. I'm not a programmer and can't really help her. Any ideas how this could be achieved in php?
Any information would be greatly appreciated.

The key is to add a variable instead of a number, then reverse that number when $i hits 10.
for($i = 1, $j = 1; $i> 0; $i+=$j) // Start i at 1, and j at 1
{
echo $i;
if($i == 10)
$j = -1; // i has hit 10, so use -1 to start subtracting
}

Another possibility is to loop up to 20, printing $i for the ascending part and 20 - $i for the descending.
for ($i = 1; $i < 20; $i++) {
if ($i <= 10) {
echo $i;
} else {
echo 20 - $i;
}
}

Related

how to display cycles in php added elements to each cycle in php?

I need your help to solve this problem, I am trying to write the necessary code so that the output is as follows:
PHP! PHP!! PHP!!! PHP!!!! PHP!!!!!
<?php
$y = ['!'];
for($i = 1; $i <= 5; $i++)
{
print "PHP!"."$y\t";
if($y+1 <5)
$y="!";
$y+'!';
}
?>
Until the second word, it adds one more exclamation point and in the rest of the repetitions it has the same number, that is two exclamation points.
How can I solve this problem?
Try this:
<?php
for ($i = 0; $i < 5; $i++) {
echo "PHP".str_repeat ("!", $i);
}
?>
You can try it:
<?php
for($i = 1; $i <= 5; $i++)
{
echo "PHP";
for($j = 1; $j <= $i; $j++){
echo "!";
}
}
?>
You can also use str_repeat():
<?php
for ($i = 1; $i <= 5; $i++) {
echo "PHP".str_repeat ("!", $i);
}
?>
Output will be: PHP!PHP!!PHP!!!PHP!!!!PHP!!!!!

How to execute an action of 10 in 10 interaction PHP

I have an array and I would like to take action on every iteration
for($i = 0; $i < count($array); $i++) {
0,1,3...10
// Execute
$create->save();
11, 12, 13... 20
// Execute
$create->save();
}
You can use the modulo of 10.
for($i = 0; $i < count($array); $i++) {
if($i%10 == 0{
$create->save();
}
}
Something like this?
$n = 10;
for($i = 0; $i < count($array); $i++) {
if($n == $i) {
$create->save();
$n = $n + 10;
}
}
But here in your loops 10 != 10, because is 11 loop. If you need to execute at every 10 loop, then $n = 9 or $i = 1;

add in loop in multi dimensional array

i am facing a problem
can some one suggest me
for ($i = 1; $i <= 2; $i++) {
$r2 = 0;
for ($t = 1; $t <= 2; $t++) {
echo $r2;
$r2++
}
}
output is 0101;
can i get output 0123 ??? please
if
for ($i = 1; $i <= 3; $i++) {
$r2 = 0;
for ($t = 1; $t <= 3; $t++) {
echo $r2;
$r2++
}
}
output is 010101;
can output 012345678 ??? please
and if
for ($i = 1; $i <= 4; $i++) {
$r2 = 0;
for ($t = 1; $t <= 4; $t++) {
echo $r2;
$r2++
}
}
output is 01010101;
can output 0123456789101112131415 ??? please
i think you understand
thanks
In all of these cases you are initializing $r2=0; in the inner loop. It should be outside the loop.
$r2=0;
for($i=1;$i<=2;$i++){
for($t=1;$t<=2;$t++){
echo $r2;
$r2++
}
}
This would produce "1234".
why are you using two nested for loops ?
why not just use one:
for ($i=0; $i<=15; $i++) echo $i . " ";
Try this:
$r2 = 10;
for($t = 0; $t <= $r2; $t++){
echo $r2;
}
Oh wait.. I get it now, why you have the two nested loops, you want to essentially raise a number to the power of 2 in order to control the number of values output. In that case, what you want is simply this:
// this is the variable you need to change to affect the number of values outputed
$n = 2;
// Square $n
$m = $n * $n;
// Loop $m times
for ($i = 0; $i < $m; $i++) {
echo $i;
}

decrese value of loop

I have this code, but i want in second loop a decrease of $p value. The first internal loop must be repeated three times, the second, two times and the last, one time. I am trying $p-- but without success.
Any idea ? thanks
$p = 3;
for ($i = 0; $i < 3; $i++) {
for ($o = 0; $o < $p; $o++) {
echo "something";
$p--;
}
}
Move your $p-- to outside the inner for loop:
$p = 3;
for ($i = 0; $i < 3; $i++) {
for ($o = 0; $o < $p; $o++) {
echo "something";
}
$p--;
}
Or better, just depend on the value of $i:
for ($i = 0; $i < 3; $i++) {
for ($o = 0; $o < 3 - $i; $o++) {
echo "something";
}
}
Or if you don't actually use $i:
for ($i = 2; $i >= 0; $i--) {
for ($o = 0; $o < $i; $o++) {
echo "something";
}
}
It's quite simple.
for ($i = 2; $i >= 0; $i--)
{
}
Set the starting number at the upper limit number, and then go down until equal to 0, $i minus 1;
You need to decrement $p outside the first loop

Can i do this (Loop in Loop)?

How can i do something like this:
for ($i = 1; $i <= $_SESSION['variable'] * $_SESSION['variable2']; $i++) {
$listo[$i] = $preparado1[$i].
for ($i = 1; $i <= $_SESSION['variable2']; $i++) {
$preparadoex[$i];
};
}
But doesnt work, i think this:
for ($i = 1; $i <= $_SESSION['variable'] * $_SESSION['variable2']; $i++) {
for ($i = 1; $i <= $_SESSION['variable2']; $i++) {
$listo[$i] = $preparado1[$i].$preparadoex[$i];
}
}
The script is simply, i have two classes the first have 2 numbers and the two have 3 numbers i should "connect" all numbers (2) in the first class with all the numbers of the second class:
F-S
1-1,
1-2,
1-3,
2-1,
2-2,
2-3
Thanks
You're re-defining $i inside of the second loop, which conflicts with the first loop's counter. Use another variable, like $j:
for($i = 1;$i <=$_SESSION['variable'] * $_SESSION['variable2']; $i++ ){
for($j = 1; $j <=$_SESSION['variable2']; $j++){
$listo[] = $preparado1[$i] . $preparadoex[$j];
}
}
You just need to use a different variable name to increment the second 'for' loop.
for($i = 1;$i <=$_SESSION['variable'] * $_SESSION['variable2']; $i++ )
{
for($j = 1; $j <=$_SESSION['variable2']; $j++)
{
$listo[] = $preparado1[$i] . $preparadoex[$j];
}
}
You can have multiple nested loop, not just two. The only thing you have to make sure is that they each use a unique loop counter.
for($i = 1;$i <=$_SESSION['variable'] * $_SESSION['variable2']; $i++ ){
for($j = 1; $j <=$_SESSION['variable2']; $j++){
$listo[$i] = $preparado1[$i] . $preparadoex[$i];
}
}
In your test case your second for should use another variable like $j.

Categories