PHP
  Home arrow PHP arrow Page 4 - 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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 14
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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 - Recursion in a more helpful sense: defining two handy recursive functions
    ( Page 4 of 4 )

    A nice conclusion for the first tutorial of this series consists of defining a pair of recursive functions that can be used in real PHP applications. The first example shows a recursive version of the PHP built-in “array_map()” function, which not surprisingly can be applied to recursive arrays, in order to escape conflictive characters. Here is the source code for the “escapeArray()” function:

    function escapeArray($inputData){
       if(is_array($inputData)){
           return array_map('escapeArray',$inputData);
       }
       return mysql_escape_string($inputData);
    }

    Despite the fact that the code for the above function is really compact, its functionality is actually worth noting. In short, all this function does is apply the “mysql_escape_string()” PHP built-in function to all the elements of a specific recursive array. This can be pretty useful in situations where a sequence of array values must be escaped before being stored in a database table.

    Now, have a look at the following snippet of code, which shows how to use the above function:

    // define recursive array
    $data=array("I'm Alejandro Gervasio",array("Take a look at some
    PHP's powerful recursive functions","Hey, what's up buddy?"));
    $data=array_map('escapeArray',$data);
    // print parsed array
    print_r($data);

    As you can see, after defining a recursive $data array, I used “escapeArray()” as the callback function for the PHP built-in “array_map()” function, in order to escape all the corresponding array elements. The respective output of this script is the following:

    Array ( [0] => I'm Alejandro Gervasio [1] => Array ( [0] => Take
    a look at some PHP's powerful recursive functions [1] => Hey,
    what's up buddy? ) )

    As illustrated above, each element of the sample array has been appropriately escaped by adding the typical leading slash to all the single quotes. Finally, the same “escapeArray()” function can be modified, in order to apply character escaping only when “magic_quotes” are disabled. Have a look at the improved version of this function:

    function escapeArray($inputData){
        if(is_array($inputData)){
            return array_map('escapeArray',$inputData);
       }
       if(!get_magic_quotes_gpc()){
           return mysql_escape_string($inputData);
       }
       return $inputData;
    }     

    In this case, the function remains nearly the same, except for the checking block that returns the escaped array elements only when the value for “magic_quotes” is turned off. Isn’t recursion a cool concept? You bet.

    Final thoughts

    In this first article I provided you with the basics of recursion in PHP. Through several, easy-to-grasp hands-on examples you learned how to define and use recursive functions, which can be handy for solving certain programming issues, instead of using conventional iteration.

    However, this is merely the beginning of the journey. Over the next part of the series, I’ll demonstrate how to use recursion in some real object-oriented PHP applications. See you then!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT