Recursion Lecture

Recursion:recusion

Recursion is the process of repeating items in a self-similar way.(wikipedia)

Recursion In PhP:

A function call itself is known as recursion in php.
Mostly in php if-else is used for breaking recursion function.

Simple Example:

ABC(1);
function ABC($E)
{
echo $E." |";
ABC(1);
}

In the above Example a simple function ABC() created .Where i give $E=1 and when browser/Compiler Enters in the function it will print 1 again and again until the browser show an Allowed memory size error.

Because when you call ABC() function inside in the funtion it Prints $E value which is 1 in this case .And after that you can see that we again call the funtion ABC() and it will again call itself again and again.

recursion error

Most important thing recursion is also use for making viruses.

Mathamatical Power function in Php without using pow()

 <html>
<head>
 <title>Power function</title>
 <style>
 #Sbutton
 {
 margin-right: 40px;

 }
 input[type="text"]
 {
 margin-bottom:5px;
 display: block;
 }
 h1
 {
 text-align: center;
 }

 </style>
</head>
<body>
<h1>Power Function</h1>
 <form action="pow.php" method="post">
 <input type="text" name="txtBase" placeholder="Enter Base">
 <input type="text" name="txtExponent" placeholder="Enter Exponent">
 <input type="submit" name="txtSubmit" id="Sbutton">

 </form>
</body>

</html>
<?php
if(isset($_POST['txtSubmit']))
{
 $Base = $_POST['txtBase'];//Base textfield name
 $Exp = $_POST['txtExponent'];//Exponent textfield name
 power($Base,$Exp);
}
function power($Base,$Exp)
{
 $A=$Base;
 for($a=1;$a<$Exp;$a++)
 {
 $A=$A*$Base;
 echo "$Base x ";
 }
 echo "$Base = $A";
}

?>