What does the & (ampersand) symbol mean in this case?
I am new to php and mysql. Im following the phpacademy tutorial on youtube. There is a section that contains form data, but everyone who followed the tutorial (myself included) got undefined index errors. I have fixed it with &
(ampersand) symbol . But what does he do in the following circumstances? Why put the symbol and the symbol in front of the $ stop error? Is this the same as @ and is it just suppressing the error or is it really a fix?
$submit = &$_POST['submit'];
source to share
This means that instead of assigning a value, $submit
assign a reference to the value $submit
.
If you are familiar with C and pointers, it is like a pointer to a value that is automatically dereferenced when it is accessed. However, PHP does not allow 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 like ...
$submit = isset($_POST['submit']) ? $_POST['submit'] : 'default_value';
... or...
$submit = filter_input(INPUT_POST, 'submit');
source to share