Recursive Function

A recursive function is a function that calls itself. If the function keeps calling itself, how does it know when to stop? You set up a condition.

<?php
class Recursion{
	function recursive($num){
		echo $num,"<br/>";
		if(isset($num) && $num < 10){
			return $this->recursive($num + 1);
		}
	}
}
$obj = new Recursion;
$obj->recursive(1);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s