When a page is opened with only this data:
<?php
$value = $_POST['value'];
echo $value;
It will always show a common error like this:
Notice: Undefined index: value in C:\xampp\htdocs.. on line 2
I have observed that it is possible to catch the errors used catch
but it does not seem correct to me to catch them, the most correct thing is to validate them.
Can you explain to me how this method works?
try {
# code...
} catch {
# catch error / Since it's a decline will be caught
}
The php documentation on this subject is not that rich, there is not much explanation.
try {
#code ...
} catch () {
echo("Error no se recibieron los datos");
$declined = true;
}
if(!$declined) {
echo ("Se recibieron los datos");
}
When receiving data correctly show this error message:
Notice: Undefined variable: declined in C:\xampp\htdocs... on line 8
When not receiving data, it does not execute any error, it only shows its defined message.
Error the data was not received
If you mean the specific case of $_POST, the best way to validate it is with
isset
, which does two checks:For example:
For other cases, you can use a variable with a boolean value as a flag, to which you change the value according to what happens in the code.
In that case the variable can be defined in advance with a default value, that way, it will exist whatever happens.
(I have changed the sense of the code, because it was a bit backwards, I think).
No matter what happens it
$bolEstado
will exist, because it is defined before the blocktry
.