PHP-generated table with colored cells How can I make my PHP script more easier? It seems quite difficult and long but I don´t know how can I make it shorter. This code is inserted in my HTML5 code on my PHP tutorial webpage, but I need it to be shorter.

&lt;?php
$n=1;
$k=0;
for ($line=0;$line&lt;1000;$line++)
{
  echo "&lt;tr&gt;";
  if ($k==0)
{
  echo "&lt;td style=\"background:rgb(". $n*25 .", ". (10-$n)*25 .", 0);\" colspan=$n&gt;$n"; echo"&lt;/td&gt;";
  for ($p=0;$p&lt;10-$n;$p++)
  {
    echo "&lt;td&gt;"; echo"&lt;/td&gt;";                   
  }
  $n++;
  if ($n==11)
  {
    $k=1;
    $n=9;
  }
  echo"&lt;/tr&gt;";
}
  if ($k==1)
  {
    echo "&lt;td style=\"background:rgb(". $n*25 .", ". (10-$n)*25 .", 0)\" colspan=$n&gt;$n"; echo"&lt;/td&gt;";
    for ($column=0;$column&lt;10-$n;$column++)
    {
      echo "&lt;td&gt;"; echo"&lt;/td&gt;";                 
    }
    $n--;
    if ($n==1)
    {
      $k=0;
    }
    echo"&lt;/tr&gt;";
  } 
}


?>
First of all you should use a deeper indent than one space. Changing it to three spaces makes the logic more readable:

if($k==0)
{
 $k=0;
}


vs:

if($k==0)
{
     $k=0;
}


The next thing is, that you named your variables in a way, that makes them not readable. $k and $n are not readable at all. If you rename $n to $colspan and $k to $countAsc they express their meaning in your logic, which improves your code style by a lot. Other programmers (like me) can  read way easier into your code then and have a faster understanding of your formulas.

Speaking of formulas, you should not put the calculations into the echo statement, but extract them, to separate the calculation of your values from its usage. That increases the overview of your variables by alot.

$red = $colspan * 25;
$green = (10 - $colspan) * 25;
/* ... */ "background:rgb(" . $red . "," . $green . ",0);" /* ... */


Speaking of the echo statements, you can separate the PHP from the HTML code. Since PHP 5.6 this is really easy due to the short echo tags &lt;?= 'string to echo' ?&gt;. This also enables you to indent the HTML tags as well, giving the reader an easier way to understand your overall structure.

&lt;?php
$red = $colspan * 25;
$green = (10 - $colspan) * 25;
?&gt;

&lt;td style="background-color:rgb(&lt;?= $red ?&gt;, &lt;?= $green ?&gt;, 0);" colspan="&lt;?= $colspan ?&gt;"&gt;
    &lt;?= $colspan ?&gt;
&lt;/td&gt;


As a next step you can safely extract the building of the &lt;td ... &gt;&lt;/td&gt; out of the if clauses. They are equal in both cases and therefore are always echoed, no matter the if conditions. This is also true for your &lt;td&gt;&lt;/td&gt; loop. Both inner for loops are exactly the same, given the difference, that the variables have a different names. In addition to that, the $countAsc variable only has two states: Being 1 or 0. Because of that it can safely be changed to a boolean variable.

$colspan = 1;
$countAsc = TRUE;
for ($line = 0; $line &lt; 1000; $line++): ?&gt;
    &lt;tr&gt;
        &lt;?php
        $red = $colspan * 25;
        $green = (10 - $colspan) * 25;
        ?&gt;

        &lt;td style="background-color:rgb(&lt;?= $red ?&gt;, &lt;?= $green ?&gt;, 0);" colspan="&lt;?= $colspan ?&gt;"&gt;
            &lt;?= $colspan ?&gt;
        &lt;/td&gt;

        &lt;?php
        for ($column = 0; $column &lt; 10 - $colspan; $column++)
        {
            ?&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;?php
        }

        if ($countAsc) ...


After extracting all these things you can see, that the if clause only determines, whether the iteration of the $colspan variable is rising or falling. Therefore the if clause should represent only that, to make the logic more readable:

if ($countAsc &amp;&amp; $colspan &gt;= 10)
{
    $countAsc = false;
}
elseif (!$countAsc &amp;&amp; $colspan &lt;= 1)
{
    $countAsc = true;
}


After this logic is extracted, the calculation for the $colspan is that simple, that it can be shortened to the short if syntax (condition ? true value : false value;) to be in a more readable manner:

$colspan = $countAsc ? $colspan + 1 : $colspan - 1;


Defining the conditions in a more fitting way reduces the necessarity to redefine the $colspan at all and therefore makes your logic less complex. Also if you have huge for loops, it is more readable to use the for(condition): ... endfor; syntax, because like this you can see first hand what type of "closing bracket" is displayed at the end of the loop, giving the code, again, a better readability.
The whole code is:

&lt;?php
$colspan = 1;
$countAsc = TRUE;
for ($line = 0; $line &lt; 1000; $line++): ?&gt;
    &lt;tr&gt;
        &lt;?php
        $red = $colspan * 25;
        $green = (10 - $colspan) * 25;
        ?&gt;

        &lt;td style="background-color:rgb(&lt;?= $red ?&gt;, &lt;?= $green ?&gt;, 0);" colspan="&lt;?= $colspan ?&gt;"&gt;
            &lt;?= $colspan ?&gt;
        &lt;/td&gt;

        &lt;?php
        for ($column = 0; $column &lt; 10 - $colspan; $column++)
        {
            ?&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;?php
        }

        if ($countAsc &amp;&amp; $colspan &gt;= 10)
        {
            $countAsc = false;
        }
        elseif (!$countAsc &amp;&amp; $colspan &lt;= 1)
        {
            $countAsc = true;
        }

        $colspan = $countAsc ? $colspan + 1 : $colspan - 1;
        ?&gt;
    &lt;/tr&gt;
&lt;?php endfor ?&gt;