| | |||||
| ||||||||
| CODE This forum is for posting code snippets and example scripts that aren't quite tutorials, but could be useful for others. You may post code snippets and/or completed scripts that you've written and want to share here. |
![]() |
| | LinkBack | Thread Tools |
| | THREAD STARTER #1 (permalink) |
| NamePros Expert Join Date: Nov 2003 Location: Scotland
Posts: 5,074
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | Working with recursive functions within php 1 thing within PHP that some people have trouble with is recursive functions. In case you are not sure what a recursive function is, a recursive function is a function that performs a task and may have to call itself to perform the same task again. Hopefully the following tutorial will enlighten you somewhat. You maybe wondering why a recursive function might be necessary. The simple answer is that without recursive functions you are set to rely on nested if else statements which can get very messy. Also if you have nested if else statements you need to know how deep you need to go, a recursive function can effectively continuously go on (well until the server runs out of memory anyway) You may have noticed that magic_quotes_gpc has received a lot of flack in the last couple of years. I am not going to go into why this is but what I will say is that magic_quotes_gpc attempts to make safe any input that is gained from form methods such as GET and POST it also does the same for COOKIE data (hence why it has gpc at the end of it's name). The function we are going to create will check that this is enabled and make the arrays safe. Firstly let me show you the whole code then I will explain it a bit at a time:- PHP Code: PHP Code: PHP Code: PHP Code: The final part is the ending of the function:- PHP Code: Now lets as promised go back the recursive section of the function:- PHP Code: We can use this function by doing clean_array($array) but as we wish to see the output I will use print_r. The array I will use is:- $array = array("age"=>"28","name"=>"O\'reilly?", "favorite_bands"=>array('Iron Maiden'=>array("members"=>"Paul Di\'Anno"), 'Dimmu Borgir', "Guns \'N\' Roses")); As you can see there is a multidimensional array (an array within an array, in fact in this case there is an array within an array within an array). You would of course use this normally with $_POST, $_GET and $_COOKIE. The output we receive from this is as follows:- PHP Code: If you think I have been unclear in this post please feel free to let me know and I will try and correct that. Points to note. I do realise that array_map could have been used to do this in a much simpler way however you may not get a full sense of how they work using this function and it may not always be possible. Although recursive functions can be a very powerful tool you should have caution. As you will probably know an endless loop can cause problems on a server (and if it was not for your maximum execution time with the php configuration it would last forever, or at least until the server was restarted), so can a recursive function that is poorly implemented. If you create a condition in which a recursive function continuously calls itself and the condition to terminate is never met you will suffer a similar problem as an endless loop. Another problem you will have is your servers memory being consumed (or at least the max set by php) by 1 script. This can happen if you have a complex function that is poorly planned out. ????: NamePros.com http://www.namepros.com/showthread.php?t=363195 **ANY CODE I WRITE AND PUBLISH ON NAMEPROS CAN BE CONSIDERED FREE TO USE. IF HOWEVER YOU WISH TO DISTRIBUTE THE CODE EITHER BY ITSELF OR WITHIN AN APPLICATION CREDIT MUST BE GIVEN**
__________________ Manage your portfolio using my new Domain Portfolio Management script. Securing Your Domain Name From Theft
Last edited by Peter; 08-27-2007 at 11:46 AM.
|
| |
| | #2 (permalink) |
| Tech Support Join Date: Mar 2005
Posts: 4,944
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | Nice Peter, although, instead of using: PHP Code: PHP Code: |
| |
| | #3 (permalink) | ||||
| Traveller Join Date: Mar 2007 Location: Yet another city
Posts: 1,419
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
__________________ NameCooler.com | ||||
| |
| | THREAD STARTER #4 (permalink) | ||||
| NamePros Expert Join Date: Nov 2003 Location: Scotland
Posts: 5,074
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
__________________ Manage your portfolio using my new Domain Portfolio Management script. Securing Your Domain Name From Theft
Last edited by Peter; 08-27-2007 at 11:30 AM.
| ||||
| |
| | THREAD STARTER #7 (permalink) | ||||||||
| NamePros Expert Join Date: Nov 2003 Location: Scotland
Posts: 5,074
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
????: NamePros.com http://www.namepros.com/showthread.php?t=363195 Cheers SV noticed it never changed when I made the edits.
It can be a very powerful feature but if you do not plan it all correctly it can go horribly wrong and you can get lost trying to pick your way through the logic (i'm sure minds do not work properly recursively, maybe time for an update).
__________________ Manage your portfolio using my new Domain Portfolio Management script. Securing Your Domain Name From Theft | ||||||||
| |
| | #8 (permalink) | ||||
| Senior Member Join Date: Aug 2005 Location: East Yorkshire, England
Posts: 2,689
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
PHP Code: | ||||
| |
| | THREAD STARTER #9 (permalink) | ||||
| NamePros Expert Join Date: Nov 2003 Location: Scotland
Posts: 5,074
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
__________________ Manage your portfolio using my new Domain Portfolio Management script. Securing Your Domain Name From Theft
Last edited by Peter; 08-28-2007 at 07:38 AM.
| ||||
| |
| | #10 (permalink) |
| NamePros Regular Join Date: Jun 2006 Location: Sydney
Posts: 251
![]() ![]() | funny, i use recursive functions in one of the pieces of software i wrote it was decoding emails bcause you can have multi part attachments - which can be inbeded, can get to be a night mare, but good when used right =)
__________________ Site Armory, tools for your website | Webmaster SEO Forums | PHP Tutorial and coding forums |
| |