3

I'm new to php and mysql. Im following a tutorial from phpacademy on youtube. There is a section that is of form data, but everyone who followed the tutorial (myself included) got undefined index errors. I fixed it with & (ampersand) symbol. But what does it do in the follow circumstances?? Why does putting the & symbol in front of the $ stop the error? Is it the same as @ and is it just suppressing the error or did it actually fix it?

$submit = &$_POST['submit'];
Jürgen Paul
  • 14,299
  • 26
  • 93
  • 133
Ryan Sinclair
  • 195
  • 1
  • 4
  • 11
  • http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php – Jürgen Paul Apr 08 '12 at 06:15
  • http://stackoverflow.com/questions/3526555/what-does-this-php-operator-means and http://stackoverflow.com/questions/1367454/what-do-operators-in-php-mean – Mike B Apr 08 '12 at 06:44

1 Answers1

7

It means instead of assigning the value to $submit, assign a reference to the value to $submit.

If you are familiar with C and pointers, it is like a pointer to the value that is automatically dereferenced when you access it. However, PHP doesn't allow things pointer arithmetic or getting the actual memory address (unlike C).

$a = 7;
$b = $a;
$c = &$a;
$c = 5;

echo $a, $b, $c; // 575

CodePad.

To stop the error you mentioned, you can use a pattern such as...

$submit = isset($_POST['submit']) ? $_POST['submit'] : 'default_value';

...or...

$submit = filter_input(INPUT_POST, 'submit');
alex
  • 479,566
  • 201
  • 878
  • 984