Recursion
No, I'm not dead yet =)
I've still been working on the WP Theme Generator, which nobody is using. I found that my webhost has the php setting for adding slashes turned on by default. My code relied on having no slashes. To fix this, I had to write a short recursive function, which I'll share here, and then explain.
Recursion, it's just a fancy word for a function, in which it calls to itself. Like function a() { a(); }. That would provide an infinite loop, but it's the most simple recursive function.
So, my situation, the $_GET and $_POST vars had slashes added. And I wanted to strip them out, but I would like my function to accept both strings and arrays, for sake of convenience and consistency (always use my function, instead of sometimes stripslashes()). What I wrote was this:
function ss_($in) { if (is_array($in)) { // It's an array, loop through all the keys and strip them. foreach ($in as $key => $value){ $in[$key] = ss_($value); // <= Reference to itself, recursion! } return $in; }elseif(is_string($in)){ // Regular stripslashes return stripslashes($in); }else{ return null; } }
Comments say it all, really.
Just another quick example would be a countdown function:
function countdown($num) { echo $num, '<br/>'; if ($num == 0) return false; // Stop countin at 0. countdown(--$num); // --$num subtracts the one from $num before the value is read. $num-- does it after. return $num; } countdown(20);
That all for now, folks.

August 1st, 2008 at 14:56 #Ilias
For your specific problem: why not just use something like
ini_set(”magic_quotes_gpc”, “0″);
ini_set(”magic_quotes_runtime”, “0″);
?
About the first recursion function: I use a quite similar function for mass-slash-stripping my values:
function strip_array($arr = array()) {
$rs = array();
while(list($key,$val) = each($arr)) {
if(is_array($val)) {
$rs[$key] = strip_array($val);
}
else {
$rs[$key] = stripslashes($val);
}
}
return $rs;
}
Based on recursion as well. Only minor differences :)
August 1st, 2008 at 15:22 #Kalkran
My host doesn’t allow
ini_set(”magic_quotes_gpc”, “0″);
ini_set(”magic_quotes_runtime”, “0″);
So I had to work around it. Thank GOD I usually have a php file I include on almost every page. Normally it contains the MySQL data, but now also this ;).
August 2nd, 2008 at 13:04 #Ilias
bool set_magic_quotes_runtime ( int $new_setting )
Tried that one, too?
August 2nd, 2008 at 19:48 #Kalkran
Nope, but this works fine ;-)
August 3rd, 2008 at 20:20 #Ilias
Of course. But using a recursive function over setting an ini value by choice would be madness :D
August 8th, 2008 at 11:59 #ilias
I was thinking about other common ini-settings that are either unsafe or unwanted, and how to circumvent them, and came to the following solution for circumventing register_globals:
if register_globals = on, and you want it to be off:
$globarr = array("_GET", "_POST", "_SERVER", "_SESSION");
foreach($globarr as $val){
foreach($$val as $key, $tmp){
if(isset($$key)){
unset($$key);
}
}
}
I register_globals is off, and you do need it (bad bad boy):
$globarr = array("_GET", "_POST", "_SERVER", "_SESSION");
foreach($globarr as $val){
foreach($$val as $key, $tmp){
if(!isset($$key)){
$$key = $tmp;
}
}
}
Admittedly, I haven’t tested this code, but as far as I know of, it works.
August 8th, 2008 at 13:48 #Kalkran
Why unset()?
August 8th, 2008 at 16:51 #ilias
Well, what register_globals does, when it’s turned on, is make a variable out of every POST/GET/SESSION var.
e.g. echo($_POST['var']); would equal echo($var);
Unsetting all those variable generated before anything else in your script (well maybe besides doing session_start()) would make your script act like nothing happened.
Of course you could do $$key = null; too.
Anything other than unset($$key) and $$key=null would make isset($$key) return true.
Maybe “variable variables and what you can do with them” is a fun subject for a next Post?
August 8th, 2008 at 19:40 #Kalkran
D’oh, I thought this was still about stripslashes, that’s why it made no sense!
=)
November 13th, 2008 at 03:36 #Victor Campos
1ocrqhab6uhxuyij