legend_99 Posted December 4, 2012 Share Posted December 4, 2012 hi everyone, i came across the below code. could u pls help me with it? what does get_magic_quotes_gpc do? create_function has 2 variables: "v" (why is v a reference?) and "k" but it uses just "v", no "k". if(get_magic_quotes_gpc()){ array_walk_recursive($_GET,create_function('&$v,$k','$v = stripslashes($v);')); } thanks. Quote Link to comment https://forums.phpfreaks.com/topic/271595-what-does-this-piece-of-code-do/ Share on other sites More sharing options...
MDCode Posted December 4, 2012 Share Posted December 4, 2012 Without knowing $k it's hard to say. magic_quotes_gpc is a php setting that adds quotes to data that use $_POST $_GET or $_COOKIE Quote Link to comment https://forums.phpfreaks.com/topic/271595-what-does-this-piece-of-code-do/#findComment-1397492 Share on other sites More sharing options...
Pikachu2000 Posted December 4, 2012 Share Posted December 4, 2012 Magic quotes adds slashes, not quotes. The code above removes the slashes inserted by magic quotes. Quote Link to comment https://forums.phpfreaks.com/topic/271595-what-does-this-piece-of-code-do/#findComment-1397500 Share on other sites More sharing options...
kicken Posted December 4, 2012 Share Posted December 4, 2012 create_function has 2 variables: "v" (why is v a reference?) and "k" but it uses just "v", no "k". When array_walk_recursive invokes the defined callback function, it will pass it two parameters, the first being the value, the second being the key. As such, the function needs to expect two parameters in it's definition, hence the $v and $k. For the purpose of what the function actually does the key is unnecessary so it's not used in the function body but it needs to be in the parameter list to create the correct function signature. $v is a reference so that any changes made to it are reflected in the original array's value. For example, if you had: $num = array(1,2,3); function sq($v, $k){ $v = $v*$v; } array_walk($num, 'sq'); print_r($num); The result of the print_r would show the array $num is the same as it was before, 1,2,3 rather than 1,4,9 as one might expect/desire. By making the $v parameter a reference though: $num = array(1,2,3); function sq(&$v, $k){ $v = $v*$v; } array_walk($num, 'sq'); print_r($num); The code works as expected and $num becomes 1,4,9 after the array_walk call. Quote Link to comment https://forums.phpfreaks.com/topic/271595-what-does-this-piece-of-code-do/#findComment-1397583 Share on other sites More sharing options...
legend_99 Posted December 5, 2012 Author Share Posted December 5, 2012 Thank you for the answers. kicken, your explanation is very helpful. one more question now: do we have to always use the v parameter as reference when calling array_walk function? Quote Link to comment https://forums.phpfreaks.com/topic/271595-what-does-this-piece-of-code-do/#findComment-1397663 Share on other sites More sharing options...
Christian F. Posted December 5, 2012 Share Posted December 5, 2012 If you want to modify the values in the array, yes. If you want to modify the keys, then you have to send it as a reference. Generally speaking, it doesn't make a whole lot of sense using array_walk () without referencing the value. At least not for most applications of it. Quote Link to comment https://forums.phpfreaks.com/topic/271595-what-does-this-piece-of-code-do/#findComment-1397668 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.