Jump to content

How to get a variable to increase an increment within an echo


Darkmatter5

Recommended Posts

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?

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

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

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?

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.