for ($i = $field +1; $i < $field2; $i++) {
echo $i.'<br/>';
}
what i'm trying to do here is Echo numbers between the input values field and field2, this works, however i was wondering if there was a more efficient way of doing this (eg changing the $i = $field +1;)
Another using while loop example:
<?php
$i = $field;
while(++$i < $field2) {
echo $i.'<br/>';
}
Related
Can anyone help me understand why a variable takes its initial value after incrementing the variable? below is code:
$k= 0;
$l= 3;
for($i = 0; $i<3; $i++){
for($j = $k; $j<$l; $j++){
echo $j;
}
echo $k+3;
echo $l+3;
}
In this we have two for loops running one inside other. Here we run three times the outside for loop, inside this we are running other for loop again. The problem we are facing is that when inner for loop end we have incremented $k and $l both by 3 but it always take value 0 and 3 respectively.
we have incremented $k and $l both by 3
Nope, you only print the result of your values plus 3, but you do not set them anywhere in the loop:
Instead of
echo $k+3;
echo $l+3;
write
$k = $k + 3;
$l = $l + 3;
You should try removing the "echo" and incrementing the variable in each loop. Then print them out after.
Try this:
<?php
$k= 0;
$l= 3;
for($i = 0; $i<3; $i++){
for($j = $k; $j<$l; $j++){
$j = $j++;
}
$k = $k+3;
$l = $l+3;
}
echo $k.'<br>';
echo $l;
?>
Gives you:
9
12
TRY this.
$k += 3;
$l += 3;
echo $k;
echo $l;
Try This
$k= 0;
$l= 3;
for($i = 0; $i<3; $i++){
for($j = $k; $j<$l; $j++){
echo $j;
}
$k = $k+3;
$l = $l+3;
}
echo $k.'<br>';
echo $l;
First Increment the value and store it in the variable
$k = $k+3;
$l = $l+3;
Then You need to print using
echo $k.'<br>';
echo $l;
#Harinarayan First of all you need to read about echo() http://php.net/manual/en/function.echo.php
echo — Output one or more strings
echo does not manipulate expression as you did in your question like:
echo $k+3;
instead of using echo for the increment you should first increment the variable and then echo that variable like below:
<?php
$k= 0;
$l= 3;
for($i = 0; $i<3; $i++){
for($j = $k; $j<$l; $j++){
echo $j;
}
$k += 3;
$l += 3;
echo $k;
echo "<br>";
echo $l;
}
Okay so i am trying to retrieve the object value iterating through a for loop.
Manually it looks like this:
echo $game->stats->item0;
echo $game->stats->item1;
...
I want to do it something like this:
for($i = 0; $i < 6; $i++) {
echo $game->stats->item.$i;
}
The above however just returns the value of $i. How can i return the actual object value?
Thanks
Basically, your statement should be like this:
echo $game->stats->{'item'.$i}
Regards,
Instead of item.$i, use {"item$i"}.
for($i = 0; $i < 6; $i++) {
echo $game->stats->{"item$i"};
}
Another way you can do it is to set a variable to be equal to 'item' . $i.
for($i = 0; $i < 6; $i++) {
$item = 'item' . $i;
echo $game->stats->$item;
}
I am making a website that has x amount of input fields. Each of the input fields is labeled team0, team1, team2 ... teamx and they are wrapped in a post form. Upon posting, I would like to check the POST variables, store them in an array, print out their values, but it seems I cannot use a variable when calling $_POST. I have tried it like so:
for ($i = 0; $i < $NUMBER_OF_TEAMS; $i++) {
$teamNames[$i] = $_POST['team$i'];
echo $teamNames[$i] . "<br>";
}
What is the correct way to do this?
Try double quotes
for ($i = 0; $i < $NUMBER_OF_TEAMS; $i++) {
$teamNames[$i] = $_POST["team$i"];
echo $teamNames[$i] . "<br>";
}
OR
for ($i = 0; $i < $NUMBER_OF_TEAMS; $i++) {
$teamNames[$i] = $_POST['team'.$i];
echo $teamNames[$i] . "<br>";
}
<?php
$td = date("d");
for ($i = 1; $i <= $td; $i++)
{
$num18 = count($this->numbermonth18);
echo "['1'," . $num18 . "],";
}
The above code will display the output like, ['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],.
I need to replace the 18 with $i value in the above code. How can I use $i instead of 18 in the for loop to display all the values of $this->numbermonth1, $this->numbermonth2, $this->numbermonth3 etc. and print the array?
You could do with:
for($i=1;$i<=$td;$i++)
{
$num=count($this->{'numbermonth'.$i});
echo "['1',".$num."],";
}
Hi I have a list of values named:
$value1
$value2
$value3
...
and I'd like to assign each value to an array element; something like:
$my_array[1]=$value1;
$my_array[2]=$value2;
$my_array[3]=$value3;
How can I do this using a for cycle? The array is not a problem but I can't figure out how to write some code for the value, it should be something like:
for($i=1; $i<=10000; $i++)
{
$my_array[$i]=$value$i;
}
Try this:
for ($i=1; $i<=10000; $i++) {
$val_name = "value" . $i;
$my_array[$i]=$$val_name;
}
You are almost there:
for($i=1; $i<=10000; $i++)
{
$my_array[$i] = $value;
}
Or this, if you want to append the counter as well:
for($i=1; $i<=10000; $i++)
{
$my_array[$i] = $value . $i;
}
What you are looking for are the {}.
$my_array[$i]=${'value'.$i};
for ($i = 1; isset(${"value$i"}); $i++) {
$my_array[$i] = ${"value$i"};
}
This syntax is known as variable variables.
You can use the $$ syntax:
for($i = 1; $i <= 10000; $i++) {
$name = 'value' . $i;
$my_array[$i] = $$name;
}