Just started learning. Here's what I have:
<?php
$i = 0;
$num = $i * 12;
for ($i=0; $i<13; $i++) {
echo($i." times 12 = ".$num."<br>");
}
?>
The outcome should be:
1 times 12 = 12
2 times 12 = 24
3 times 12 = 36
etc...
The outcome I actually get is:
1 times 12 = 0
2 times 12 = 0
3 times 12 = 0
any ideas?
It is because you have this declaration before for loop:
$i = 0; $num = $i * 12;
so always $num will be 0. Just place it into for:
for ($i=1; $i<13; $i++) {
$num = $i*12;
echo($i." times 12 = ".$num."<br>");
}
You don't need declare $i variable before for loop. This variable will be overwritten. There is simple test:
$i = 5;
for($i = 1; $i<10; $i++);
echo $i;
OUTPUT:
10
<?php
$i = 1; $num = 1;
for ($i=1; $i<13; $i++) {
$num = $i * 12;
echo($i." times 12 = ".$num."<br>");
}
?>
If you want that result you should put the calculation inside the for loop and start i with 1
<?php
for ($i=1; $i<13; $i++) {
$num = $i * 12;
echo($i." times 12 = ".$num."<br>");
}
?>
Something like this
<?php
$num = 12;
for ($i=1; $i<13; $i++) {
echo("$i times 12 = ".$num*$i);
echo "<br>";
}
?>
<?php
for ($i=0; $i<13; $i++) {
echo($i." times 12 = ".$i*12."<br>");
}
?>
Line $num = $i * 12; shift into loop
<?php
$i = 0;
for ($i=0; $i<13; $i++) {
$num = $i * 12;
echo($i." times 12 = ".$num."<br>");
}
?>
Your $num variable only ever increments on $i when its 0, try putting it in the for loop like this.
$i = 0;
for ($i=0; $i<13; $i++) {
$num = $i * 12;
echo($i." times 12 = ".$num."<br>");
}
Related
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;
I'm new in PHP. How can i achieve continously loop with adding different value?
it's something like this
<?php
$gap1 = 2;
$gap2 = 3;
$lenght = 10;
for( $i=0; $i<$length; $i++ )
{
//the code
}
?>
and the result will be : 0 2 5 7 10 12 15 17
thank you for your help :)
$gap1=2;
$gap2=3;
$lenght = 10;
$p=0;
for($i=0;$i<$lenght;$i++)
{
if($i==0){$p=0;}
elseif($i%2==0)
{
$p+=$gap2;
}
else{
$p+=$gap1;
}
echo $p.'<br>';
}
Try this code:
$gap1 = 2;
$gap2 = 3;
$length = 10;$i=0;
$x = 0;
while($i<$length)
{
echo $x." ";
if($i%2 == 0)
$x+=$gap1;
else
$x+=$gap2;
$i++;
}
Output:
0 2 5 7 10 12 15 17 20 22
$gap = array(2, 3);
$result = array(-1 => 0);
$length = 10;
for($i = 0; $i < $length; $i++) {
$result[] = $result[$i-1] + $gap[($i) % count($gap)];
}
echo implode(' ', $result);
i need help for this task.
i need a for loop who picks the closest duration
sample we have a duration with 120 seconds the 120 seconds are on every calculation divided by 8
so here is an example
120 Duration
Here are the 8 closest values
15
30
45
60
75
90
105
120
How i can realize this i have all tested
<?php
$count = 1;
$duration = 120;
$temp = 0;
for ($x = 1; $x < 120; $x++) {
#$j = $x / 8;
$temp = $x / 8;
echo '<pre>' . ($temp) . '</pre>';
if ($count == 8) {
break;
}
$count++;
}
?>
Your entire loop is totally redundant. Why not just this:
<?php
for ($i = 0; i < 8; ++$i)
{
echo '<pre>' . (15 * ($i+1)) . '</pre>';
}
?>
You can use $i directly as a counter in the loop.
Your question is quite unclear and the provided code doesn't go from 1->120 since you break it after 8 iterations.
To get the values 15 30 45 60 75 90 105 120 from the base 120 you would need something like this:
$result = array();
$duration = 120; //the duration in seconds as provided in the example
$divider = 8; //the divider 8 as provided by the example
for ($i = 1; $i <= $divider; $i++) {
//This will give 1* 120/8 = 15 for the first run
//2* 120/8 = 30 for the second and so on
$result[] = (int) $i * $duration / $divider;
}
would the following work?
function sample($max, $count) {
$samples = array();
for($i = 1; $i <= $count; ++i) {
$samples[] = (int)($max / $count * $i);
}
return $samples;
}
Do you mean something like
$result = array();
for ($i = 1; $i <= 8; $i++) {
$result[] = (int) ($duration / 8) * $i;
}
?
I want to create somthing like 100 var's which their names will be:
$numbr_1 = 1;
$numbe_2 = 2;
$number_3 = 3;
...
I won't write 100 vars of course, but there is a way to do it with foor loop or somthing? I thought about this:
for ($i = 1; $i <= 100; $i++)
$number_{$i} = $i;
You're talking about variable variables, and they are incredibly stupid to use. For one, they make debugging next to impossible.
What you want is an array:
for ($i = 1; $i <= 100; $i++) {
$numbers[$i] = $i;
}
Something like this should work:
for($i = 1 ; $i <= 100 ; $i++){
$var_name = "number_$i";
$$var_name = $i;
}
for($i=1;$i<=100;$i++) {
$j="number$i";
$$j = $i;
}
Why don't you use an array?
$number = array();
for ($i = 0; $i < 100; $i++)
{
$number[] = $i;
}
for($i = 1 ; $i <= 100 ; $i++){
${'number_'.$i} = $i;
}
Possible solution is usage of array.
$number = array();
for ($i = 1; $i <= 100; $i++) {
$number[$i] = $i;
}
Say I create a recursive array with this code:
$digits = 0;
$tens = 0;
$hundreds = 0;
for($i = 0; $i <= 100; $i++)
{
$myArray[$hundreds][$tens][$digits] = $i;
$digits++;
if($digits > 9)
{
$digits = 0;
$tens++;
}
if($tens > 9)
{
$tens = 0;
$hundreds++;
}
}
how could I echo out all the data fromt the 'tens array' == 2?
To be clear, I'd be looking for these results:
20 21 22 23 24 25 26 27 28 29
since im using base 10, I could just do this:
for($i=0; $i < 10; $i++)
{
echo $myArray[0][2][$i]
}
but what if i have no idea how many elements are in the digits array?
foreach($myArray[0][2] as $v) {
echo $v."<br>\n";
}
<?php
$tensArr = $myArray[0][2];
for($i= 0 ; $i < count($tensArr); $i++)
{
echo $tensArr[$i]."\n" ;
}