Pascal's triangle in PHP Are there any optimization suggestions?

&lt;?php
# Pascal's Triangle using Recursion.
$rows_to_generate = 15; // Define number of rows you want to generate.
$storage = array();
function get_elem($row,$pos)    // generate element having number of row and number of position.
{
    global $storage;

    if(isset($storage[$row][$pos]))
    {
        return $storage[$row][$pos];
    }

    if($row==1 and $pos==1) // for first row.
    {
        return 1;
    }
    else if($pos==0)    // for left most row element that doesn't exist.
    {
        return 0;
    }
    else if($pos&gt;$row)  // for right most row element that doesn't exist.
    {
        return 0;
    }
    return get_elem($row-1,$pos-1)+get_elem($row-1,$pos);   // Recursion
}
?&gt;
&lt;pre&gt;
&lt;?php
for($i=1;$i&lt;=$rows_to_generate;$i++)
{
    for($j=1;$j&lt;=$i;$j++)
    {
        echo $storage[$i][$j]=get_elem($i,$j);  // generate
        if($i!=$j)
        {
            echo ' ';
        }
    }
    echo '&lt;br&gt;';
}
?&gt;
&lt;/pre&gt;
These conditions can be simplified:


if($row==1 and $pos==1) // for first row.
{
    return 1;
}
else if($pos==0)    // for left most row element that doesn't exist.
{
    return 0;
}
else if($pos&gt;$row)  // for right most row element that doesn't exist.
{
    return 0;
}



To this:

if ($pos == 1 or $row == $pos)
{
    return 1;
}


Coding style

The expressions are too packed.
It would be better to add more spaces:


around operators
after semicolons
after commas in parameter lists
before opening ( of if conditions and for loops


So instead of this:


for($i=1;$i&lt;=$rows_to_generate;$i++)
{
    for($j=1;$j&lt;=$i;$j++)
    {
        echo $storage[$i][$j]=get_elem($i,$j);  // generate
        if($i!=$j)



This is easier to read:

for ($i = 1; $i &lt;= $rows_to_generate; $i++)
{
    for ($j = 1; $j &lt;= $i; $j++)
    {
        echo $storage[$i][$j] = get_elem($i, $j);  // generate
        if ($i != $j)