I have one question about php. I don't know hot to solve it. When wrote code like this $x = $_POST['something'];
It is error. And when I wrote code like this,
if (isset($_POST['something'])) {
$x = $_POST['something'];
}
everything is fine. I don't know why? Can anyone explain me which is correct way? Sorry for bad english
$_POST['something']
will give an error if that variable is unassigned (ie doesn't have a value). isset()
checks to see if the variable is unassigned and will do nothing if that is the case. Therefore, the error you're getting stems from the fact that the $_POST superglobal has no variable in it called "something"
The problem is that $_POST['something'] may not be set, i.e. this variable may not exist (it is an element in an array, so it can be seen somehow as a variable, for simplicity, even if this is not 100% correct).
If you add the condition
if(isset($_POST['something']))
, then the assignment is made only when $_POST['something'] is set. So, you are sure that you can make that assignment.
AFAIK, it's not an error, but just a notice. It would be displayed if you're error_reporting is set to E_ALL or similar. It basically tells you that the variable is not set yet, very useful in case you have a typo or something.
You can use the if(isset()) thing, or just tell PHP that you want to disable such warning by using @
$x = @$_POST['something'];