<?php

function rec_sum($n) {
  if (
$n <= 0) {
    return 
0;
  } else {
    return 
$n rec_sum($n-1);
  }
}

$self $HTTP_SERVER_VARS['PHP_SELF']; // get the name of this file

/* if $submit does not have a value 
   then user has not been to this page yet, so display the form */
if (!isset($_POST['submit'])) {
  print <<<END
<p>Enter a number: 
<form action="$self" method="post">
<input type="text" name="n" maxlength="5" />
<input type="submit" name="submit" value="Enter" />
END;

} else { 
// process the form if user submitted a number
  
$n $_POST['n'];
  
$result rec_sum($n);
  echo 
"<p> The sum of 1 + ... + $n = $result. </p>";
}

?>