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'];
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With