How to add numbers recursively in php
<p>I want to add the last digit of $colnum as you see in the following. Please refer to comments in the codes.</p>

<pre><code>for($c=2;$c&lt;$keynum;$c++){
...
...
$studentcomment = '';
for($col=1;$col&lt;5;$col++){
    $colnum="col".$col."_".$c;// this will find col1_2, col2_2, col3_2, col4_2 
    $colnum= $this-&gt;input-&gt;post($colnum);// this will get value from post. 
    //e.g. col11, col12 col13 col14 or col15
    echo "colnum is ";
    echo $colnum; // this will be col12
    echo "&lt;br /&gt;";
    $subgrade="";
    $subgrade = substr($colnum,-1);// get the last digit
    if($subgrade&lt;5 AND !empty ($subgrade)){// 5 has no comment so excluded
        echo "subgrade is ";
        echo $subgrade;// this will be 2,3 or 4
        $total="";
        $total += $subgrade;// add all the subgrade to find the total
        echo "&lt;br /&gt;";
        echo "Total collaboration marks is ";
        echo $total;
    }

    if(!empty($colnum)){
        $studentcomment .=$this-&gt;lang-&gt;line($colnum);//output all the comments 
from language file.
    }

}
</code></pre>

<p>However the output is the following and not adding numbers.</p>

<pre><code>colnum is col15
colnum is col24
subgrade is 4
Total is 4
colnum is col33
subgrade is 3
Total is 3
colnum is col42
subgrade is 2
Total is 2
</code></pre>

<p>I will appreciate your help.Thanks in advance.</p>
