I have a loop that has a dynamic variable in it, eg:
while(i < 10){
echo ${"dynamic" . $i . "var"};
$i++;
};
I want to only echo the variable if the original var (say $dynamic3var) is set so I add:
while(i < 10){
if(isset(${"dynamic" . $i . "var"})){
echo ${"dynamic" . $i . "var"};
$i++;
};
};
However this wont work as its still picking up $i.
Does anyone know a correct way of doing this?
Since global variables are bad ideas you should rethink your code. A plain refactoring would be to use an associative array (even if it remains a global variable at the first step). Then you could work with
if( isset($dynamic[$i]) ) ...
Why are Globals evil? Read this: http://tomnomnom.com/posts/why-global-state-is-the-devil-and-how-to-avoid-using-it
Try this:
while($i < 10){
$label = "dynamic".$i."var";
if(isset($$label))
echo $$label;
$i ++;
};
Related
I am trying to increment two separate numbers in the same for loop. For example I want the first number to be:
0,2,4,6,8,10 etc
I want the second number to be:
1,3,5,7,9 etc
I have tried what was suggested here but could get it to work:
How to increment a number by 2 in a PHP For Loop
My code so far:
$count = count($toyList);
echo $count;
for($i=0; $i<$count; $i++){
$json = '{"toy": {"toyname":"' . $toyList[$i+2] . ',"status":"' . $toyList[$i+3] . '"}}';
}
Any help would be greatly appreciated. Thanks.
Introduce an additional variable and increase that inside the loop after you have done with it whatever you intend to do. I don't have an environment for PHP at hand, but it should look something like this:
$count = count($toyList);
echo $count;
$j = 1;
for($i=0; $i<$count; $i++){
$json = '{"toy": {"toyname":"' . $toyList[$i+2] . ',"status":"' . $toyList[$i+3] . '"}}';
$j = $j + 2;
}
i have this code
$number = 1;
echo $number;
for ($i=0; $i < 10; $i++) {
$number++;
}
the output of echo $number is 1 not 11.
How can get the last $number value when I called it before it changed?
Once you output something to the browser, it's done. You cannot change it again later. The only way to handle this is to not output the variable until you have found its final value; ie in the example you move the echo statement to the bottom.
It's generally considered a good idea to first run all of your PHP code and determine all your variables and only then start outputting things to the browser in order to prevent the kind of problem you have now.
$number = 1;
echo 'before increment :'.$number;
for ($i=0; $i < 10; $i++) {
$number++;
}
echo 'after increment :'.$number;
Try this way, now you will get expected result:
$number = 1;
for ($i=0; $i < 10; $i++) {
$number++;
}
echo $number;
Reason, you have put echo $number before the increment, which was logically wrong:
Title might not make complete sense, couldn't think of a way to explain it so I will do a mock PHP example.
<?php
$startNumber = 1;
$endNumber = 15;
$fileExt = '.jpg';
?>
And then on the page it will echo from the $startNumber to the $endNumber in something like a foreach
<?php echo("<img src=\"#.jpg\">"); ?>
Hope this is making sense...
I'd also like to point out that the numbers < 10 start with a 0 so it will need to be in the image source link.
echo is not a function, it's a language construct. Don't use ().
Also you need to use a loop like for() to do that (Also see sidenote of Fred -ii).
$endNumber = 15;
for($i = 1; $i <= $endNumber; $i++) {
if($i < 10) {
$i = "0".$i;
}
echo "<img src=\"".$i.".jpg\" />";
}
If this is not what you asked, add more information.
I have a string called $columns which dynamically gets a value from 1 to 7. I want to create a loop of <td></td> for however many times the value of $columns is. Any idea how I can do this?
for ($k = 0 ; $k < $columns; $k++){ echo '<td></td>'; }
Here's a more readable way to achieve this:
foreach(range(1,$columns) as $index) {
//do your magic here
}
If you just need to use number of repeat count:
for ($i = 0; $i < 5; $i++){
// code to repeat here
}
just repeat $n times? ... if dont mind that $n goes backwards...
the advantage is that you can see/config "times" at the beginning
$n = 5;
while (--$n >= 0)
{
// do something, remember that $n goes backwards;
}
I like this way:
while( $i++ < $columns ) echo $i;
Just bear in mind if $columns is 5, this will run 5 times (not 4).
Edit: There seems to be some confusion around the initial state of $i here. You are welcome to initialise $i=0 beforehand if you wish. This is not required however as PHP is a very helpful engine and will do it for you automatically (tho, it will throw a notice if you happen to have those enabled).
There is a str_repeat() function in PHP, which repeats a string a number of times. The solution for your problem would be:
str_repeat( '<td></td>', $columns );
If $columns is a string you can cast to int and use a simple for loop
for ($i=1; $i<(int)$columns; $i++) {
echo '<td></td>';
}
A for loop will work:
for ($i = 0; $i < $columns; $i++) {
...
}
You can run it through a for loop easily to achieve this
$myData = array('val1', 'val2', ...);
for( $i = 0; $i < intval($columns); $i++)
{
echo "<td>" . $myData[$i] . "</td>";
}
Why use logic at all, don't waste those CPU cycles!
<td colspan="<?php echo $columns; ?>"></td>
foreach(($_POST["msg"] as $mg) AND ($_POST["control"] as $id))
{
echo $mg;
echo $id;
}
i need make something like that, any way to do? i'm trying to get 10 mysql records and edit all of them
No, that won't work. The closest thing I can see to what you're trying to do is:
for($i = 0; $i < count($_POST["msg"]); $i++) {
echo $_POST["msg"][$i];
echo $_POST["control"][$i];
}
Assuming that "msg" and "control" will always contain the same amount of items.
Assuming both $_POST['msg'] and $_POST['control'] are actually arrays, have numeric keys (thanks #iMoses), and have the same length, you could use a for loop -
for ($i = 0; $i < count($_POST["msg"]); $i++){
$mg = $_POST['msg'][$i];
$id = $_POST['control'][$i];
}