Darkmatter5 Posted November 13, 2008 Share Posted November 13, 2008 Here's my code echo "<tr bgcolor='$fillcolor'> <td>$row[mem_username]</td> <td width='20'><input name='$row[member_id]name' type='text' class='text_boxes' tabindex='$tab' size='20' maxlength='100' /></td> <td align='center'><input type='checkbox' tabindex='$tab+1' name='$row[member_id]activated' $act></td> <td align='center'><input type='checkbox' tabindex='$tab+2' name='$row[member_id]priv_admin' $admin></td> <td align='center'><input type='checkbox' tabindex='$tab+3' name='$row[member_id]priv_file' $file></td> <td align='center'><input type='checkbox' tabindex='$tab+4' name='$row[member_id]priv_guest' $guest></td> <td align='center'><input type='checkbox' tabindex='$tab+5' name='$row[member_id]rights_news' $news></td> <td align='center'><input name='delete' type='submit' class='button' tabindex='$tab+6' value='DELETE' /> | <input name='save' type='submit' class='button' tabindex='3' value='SAVE' /></td> </tr>"; $tab=$tab+7; This code is inside a while loop, so instead of existing the echo each time to increase the variable, how can I do this on-the-fly within the echo? Quote Link to comment https://forums.phpfreaks.com/topic/132572-how-to-get-a-variable-to-increase-an-increment-within-an-echo/ Share on other sites More sharing options...
Mchl Posted November 13, 2008 Share Posted November 13, 2008 echo $tab++ Quote Link to comment https://forums.phpfreaks.com/topic/132572-how-to-get-a-variable-to-increase-an-increment-within-an-echo/#findComment-689336 Share on other sites More sharing options...
.josh Posted November 13, 2008 Share Posted November 13, 2008 echo $tab++ will not work in an echo. $x = 1; echo "blah" . $x++ . "moreblah"; // will inc $x but will not echo result echo "blah" . ($x+1) . "moreblah"; // echoes 2 but does not inc $x echo "blah" . ($x=$x+1) . "moreblah"; // incs and echoes 2 Quote Link to comment https://forums.phpfreaks.com/topic/132572-how-to-get-a-variable-to-increase-an-increment-within-an-echo/#findComment-689340 Share on other sites More sharing options...
Mchl Posted November 13, 2008 Share Posted November 13, 2008 echo $tab++ will not work in an echo. On what basis you state that? try echo ($i++." ".$i++." ".$i++." ".$i++); Quote Link to comment https://forums.phpfreaks.com/topic/132572-how-to-get-a-variable-to-increase-an-increment-within-an-echo/#findComment-689344 Share on other sites More sharing options...
.josh Posted November 13, 2008 Share Posted November 13, 2008 echo comes before ++ in order of operation, so the var will be echoed first, then incremented. Try your own code. It only echoes 3 vars, because on your first $i++, $i doesn't exist, so it echoes nothing. <?php $x = 1; echo $x++; // output: 1 echo $x; // output: 2 ?> Output: 12 Quote Link to comment https://forums.phpfreaks.com/topic/132572-how-to-get-a-variable-to-increase-an-increment-within-an-echo/#findComment-689346 Share on other sites More sharing options...
Mchl Posted November 13, 2008 Share Posted November 13, 2008 But it still would work for the original code, where $tab was declared before the loop. And there's also preincrementing available. echo ++$i." ".++$i." ".++$i." ".++$i; Quote Link to comment https://forums.phpfreaks.com/topic/132572-how-to-get-a-variable-to-increase-an-increment-within-an-echo/#findComment-689348 Share on other sites More sharing options...
.josh Posted November 13, 2008 Share Posted November 13, 2008 But it still would work for the original code, where $tab was declared before the loop. And there's also preincrementing available. echo ++$i." ".++$i." ".++$i." ".++$i; You suggested $tab++ this will not work because OP wants the next incremented value to echo. $tab++ will have it "lag behind". And yes, ++$i will work, but that's not what you suggested, now is it? Quote Link to comment https://forums.phpfreaks.com/topic/132572-how-to-get-a-variable-to-increase-an-increment-within-an-echo/#findComment-689350 Share on other sites More sharing options...
Mchl Posted November 13, 2008 Share Posted November 13, 2008 Look at the code OP posted. We can safely assume, that $tab is declared as an integer number somewhere before it, because otherwise tabindex='$tab' would echo tabindex='' and I doubt it was OP's goal. What's more, $tab has a value of first tabindex to be used in this section of code. So in this case postincrementation would work perfectly well, now wouldn't it? Quote Link to comment https://forums.phpfreaks.com/topic/132572-how-to-get-a-variable-to-increase-an-increment-within-an-echo/#findComment-689352 Share on other sites More sharing options...
.josh Posted November 13, 2008 Share Posted November 13, 2008 Ah you are right. I did not notice the tabindex='$tab' (without the +x) because it wasn't lined up with the rest. My bad. So yeah, as long as OP puts $tab++ on the first one, that should work. Quote Link to comment https://forums.phpfreaks.com/topic/132572-how-to-get-a-variable-to-increase-an-increment-within-an-echo/#findComment-689358 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.