PHP
  Home arrow PHP arrow Fundamentals of Recursion in PHP
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Fundamentals of Recursion in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 13
    2006-05-01

    Table of Contents:
  • Fundamentals of Recursion in PHP
  • Introducing the basics of recursion: developing an easy-to-grasp example
  • Emphasizing the fundamentals of recursion: defining an improved recursive function
  • Recursion in a more helpful sense: defining two handy recursive functions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Fundamentals of Recursion in PHP


    (Page 1 of 4 )

    Iteration is a straightforward concept. Recursion is a bit more complicated; it can be defined as a regular function that calls itself. PHP supports recursive functions. This article, the first of three parts, will explain recursive functions and help you see why they are useful.

    Introduction

    Although you've probably never turned your attention to counting the number of situations where you used iteration during the development of a Web application, writing iterating code is certainly a thing that you’ve done hundreds, not to mention thousands of times.

    Iteration is a direct consequence of the ability of computers to automate repetitive tasks, something that has been reflected in most programming languages with the introduction of different iteration structures. In fact, the issue is as simple as it seems: if there’s something that you need to do the same way a number of times inside an application, then you’ll use some kind of loop structure to repeat several blocks of code.

    With reference to executing repetitive code, PHP supports the use of the popular “for,” “while,” “do-while” loops and additionally the “foreach” loop, which can be used to easily traverse arrays. Also, there are a few additional built-in functions that perform some sort of iteration on data structures, such as the “array_map()” function, to cite a well-known example. Still, in this article, I'm definitely not going to bore you with an irrelevant list of iterating PHP functions, since you can read the corresponding manual.

    In general terms, iteration is a pretty straightforward concept, which as you saw, can be applied in different programming cases with minor hassles. However, let me go one step further in my explanation: say you’ve defined a PHP function that need to be called repetitively within an application, in order to traverse dynamic data structures such as trees or linked lists. In this case, you’re likely to use what is commonly called a “recursive function.”

    Essentially, a recursive function can be defined as a regular function that calls itself. Sounds like a confusing concept? It isn't. Fortunately, PHP supports the definition of recursive functions, which can be useful in certain situations. For instance, you'll want to use a recursive function if you’re going to do a lot of work with data trees, which are structured as database tables, or in cases where template files contain nested placeholders and need to be recursively parsed, and so on. You get the idea.

    Considering the important role that recursion plays in most programming languages, and specifically in PHP, over this series I’ll be demonstrating how to define and use recursive functions with numerous code samples, thus you can learn quickly how to include them in your own PHP scripts.

    Curious about using recursion in PHP? Let’s get started and not waste more time. 

    More PHP Articles
    More By Alejandro Gervasio


       · In this first article, you'll find numerous hands-on examples on how to apply...
     

       

    PHP ARTICLES

    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT