0

The below code is for sanitizing the posted values. Can some tell me What is the difference between,

<?php
  function sanitize_data(&$value, $key) {
  $value = strip_tags($value);
}
array_walk($_POST['keyword'],"sanitize_data");
?>

and

<?php
  function sanitize_data($value, $key) {
  $value = strip_tags($value);
}
array_walk($_POST['keyword'],"sanitize_data");
?>

Thanks

Code
  • 219
  • 2
  • 11

5 Answers5

0

The first uses value as a refrence, so any time you call it with some variable the variable will be changed in the outer scope, not only in the function itself.

Look in the manual for 'reference' if you want more info.

Nanne
  • 64,065
  • 16
  • 119
  • 163
0

It's called 'pass by reference'. &$value will relate to the original $value passed into the function by pointer, rather than working on a function version.

Please see the PHP Manual.

BenM
  • 52,573
  • 26
  • 113
  • 168
0

The first function the value of the first parameter is passed by reference and in the second not. If the variable is passed by referenced, changes to it will also be done on the value outside of the function scope (in the scope you call the function).

Also read the PHP documentation (pass by reference) and is also demonstrated on the array_walk doc page.

Styxxy
  • 7,462
  • 3
  • 40
  • 45
0

First method is called as "Passing value as reference". So $_POST array values are changed .

In second method will not change the value of $_POST

You can check SO Link: Great Explanation about it.

https://stackoverflow.com/a/2157816/270037

Community
  • 1
  • 1
Kumar V
  • 8,810
  • 9
  • 39
  • 58
0

The first function gets $value passed by reference so it can modify it directly, the second function gets passed $value's value.

Lars Beck
  • 3,446
  • 2
  • 22
  • 23