PHP for loop within for loop with different incrementing values - php

I am new to PHP and for loops but I am having trouble racking my brain around the math for this. I am trying to write a loop that will create an array with 49 items. The items have two incrementing values within them. The 49 items are below:
M1s1t1url
M1s1t2url
M1s1t3url
M1s1t4url
M1s1t5url
M1s1t6url
M1s1t7url
M1s2t1url
M1s2t2url
M1s2t3url
M1s2t4url
M1s2t5url
M1s2t6url
M1s2t7url
M1s3t1url
M1s3t2url
M1s3t3url
M1s3t4url
M1s3t5url
M1s3t6url
M1s3t7url
M1s4t1url
M1s4t2url
M1s4t3url
M1s4t4url
M1s4t5url
M1s4t6url
M1s4t7url
M1s5t1url
M1s5t2url
M1s5t3url
M1s5t4url
M1s5t5url
M1s5t6url
M1s5t7url
M1s6t1url
M1s6t2url
M1s6t3url
M1s6t4url
M1s6t5url
M1s6t6url
M1s6t7url
M1s7t1url
M1s7t2url
M1s7t3url
M1s7t4url
M1s7t5url
M1s7t6url
M1s7t7url
As you can see there are three numbers in each item. The first number is a constant. The second number counts up to 7 then resets back to 1. The third number adds 1 every time the second number resets back to 1. Here is what I have below but I know I am off on the calculations.
for ($i = 1; $i < 8; $i = $i + 1) {
for ($u = 1; $u < 8; $u = $u + 1) {
$urln[] = 'M1s'.[$u].'t'.[$i].'url';
}
}
I am getting an array to string error.

<?php
for ($i = 1; $i < 8; $i++) {
for ($u = 1; $u < 8; $u++) {
$urln[] = 'M1s' . $u . 't' . $i . 'url';
}
}

<?php
$urln = array();
for ($i = 1; $i < 8; $i++) {
for ($u = 1; $u < 8; $u++) {
$urln[] = 'M1s' .$i. 't'. $u .'url';
}
}
foreach ($urln as $i) {
echo "$i\n";
}
?>

Related

Add all the previous numbers in a for loop to get the current number

I have this for loop:
for($i = 1; $i <= 7; $i++) {
echo $i . "<br>";
}
Which outputs:
1
2
3
4
5
6
7
Now what I want is to add all the previous numbers on each loop. So the output should be:
1
2 // add all above to get this number
3 // add all above to get this number
6 // add all above to get this number
12 // add all above to get this number
24 // add all above to get this number
48 // add all above to get this number
96 // add all above to get this number
...etc
The first and second number doesn't necessarily have to be in the loop, that can be defined manually outside.
What I don't want is to add the value of $i on each loop, but to add all the previous numbers on each loop.
I have tried summing up using this code:
$sum = 0;
for($i = 1; $i <= 5; $i++) {
$sum = $sum + $i;
echo $sum . "<br>";
}
But I get this output:
1
3
6
10
15
21
28
How can I achieve my desired output?
Try this
<?php
$results = [];
for ($i = 0; $i <= 7; $i++){
$currentResult = 0;
if ($i < 2){
$currentResult = $i+1;
}
else{
foreach($results as $currenNumber){
$currentResult += $currenNumber;
}
}
echo $currentResult . '<br>';
$results[] = $currentResult;
}
?>
<?php
$value = 0;
for($i = 1; $i <= 8; $i++) {
if($value < 3){
$value = $value + 1;
} else{
$value = $value * 2;
}
echo $value . '<br>';
}
?>

For loop (php) that results in this: 12345678910987654321

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;
}
}

FOR Loop link generating by limits

How to generate links? For example I want to create 10 links starting from page/5 and generate 10 link dynamically - it seems skip first 5 and generate from link 5 to 15 e.g.
page/5, page/6, page/7, to page/15
$numOfPages = 10;
$startfrom = 5;
$pages = [];
for ($i = $startfrom; $i <= $numOfPages; $i++) {
$pages[] = 'page/' . $i;
}
Not working if work if $startfrom grater than $numOfPages
$startfrom is an offset, so you need to loop from that offset to the offset+the number of pages you want to have:
for ($i = $startfrom; $i < $startfrom+$numOfPages; $i++) {
Of you can do the addition inside the loop:
for ($i = 1; $i <= $numOfPages; $i++) {
$pages[] = 'page/' . ($i + $startfrom)
}
But since you probably have a maximum number of pages, which may not be a multiple of 5 of 10, I'd introduce an extra variable and determine the end of the loop before actually looping (using min()). That way, the loop itself and the code in it is nice and simple:
$totalPages = 28;
$pagesToShow = 10;
$startFrom = 5;
$loopTo = min($startFrom + $pagesToShow - 1, $totalpages);
for ($i = $startFrom; $i <= $loopTo; $i++) {
$pages[] = 'page/' . $i;
}

I need to find all amicable numbers up to a certain number

Here is my code:
$n = 300;
$set = 0;
$set2 = 0;
for($i = 1; $i<$n; $i++)
{
for($j = 1; $j <$i; $j++)
{
$qol = $i % $j;
if($qol == 0)
{
$set += $j;
}
}
for($s=1; $s<$set; $s++)
{
$qol2 = $set % $s;
if($s == 0)
{
$set2 += $s;
}
}
if($set2 == $i)
{
echo "$set and $i are amicable numbers</br>";
}
}
I do not know what the heck the problem is!
FYI: 220 and 284 are an example of amicable numbers. The sum of the proper divisors of one number are equal to other number and vice versa (wiki).
I am having troubles following your logic. In your code how would $set2 == $i ever be true? Seems to me that $i would always be greater.
I would do it the following way:
First make a separate function that finds the sums of the proper divisors:
// Function to output sum of proper divisors of $num
function sumDiv($num) {
// Return 0 if $num is 1 or less
if ($num <= 1) {
return 0;
}
$result = 1; // All nums divide by 1
$sqrt = sqrt($num);
// Add divisors to result
for ($i = 2; $i < $sqrt; $i++) {
if ($num % $i == 0) {
$result += $i + $num / $i;
}
}
// If perfect square add squareroot to result
if (floor($sqrt) == $sqrt) {
$result += $sqrt;
}
return $result;
}
Next check each iteration for a match:
$n = 1500;
for ($i = 1; $i < $n; $i++) {
// Get sum of proper devisors of $i, and sum of div. of result.
$currentDivs = sumDiv($i);
$resultDivs = sumDiv($currentDivs);
// Check for a match with sums not equal to each other.
if ($i == $resultDivs && $currentDivs != $resultDivs) {
echo "$i and $currentDivs are amicable numbers<br>";
}
}
Here a functioning phpfiddle.
Warning: Large numbers will take very long to process!

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;
}

Categories