There are common errors when it comes to sending information on the same page and these errors are normal, so they are not declared or their value isNULO
So, since we know that it is not a serious error and that it is a normal error, so they are not declared or their value is null, a simple solution is to disable the warning errors among others.
error_reporting(0);
Although it is a solution, for me it is not correct to use it to disable errors, the code must be validated first.
If its use is good but first that all code is valid and then use them for future updates and thus avoid showing errors to the visitor or showing an error that a Hacker can take advantage of that error.
The following example:
When information sent by method is lostpost
$unpost = $_POST['post'];
echo $unpost;
It shows this error message:Notice: Undefined index: post
Another similar error is when it comes to displaying a specific data when there is SESSION
some data:
if( $_SESSION['datos']['radio'] == $key ) { else {}}
How can I properly validate undeclared variables and empty sessions when lost for some reason or reason?
Dynamic example:
<?php
$r =array(
1 => 'Opcione Uno',
2 => 'Opcione Dos',
3 => 'Opcion Tres',
);
// recorremos las opciones del radio button
// comprobamos si se selecciono antes una opción comparando los key
echo'<form method="POST" action="a.php">';
foreach ($r as $key => $value) {
// para recuperar los datos de un radio button
if( $_SESSION['datos_form']['radio'] == $key ) {
echo '<input name="radio" type="radio" id="'.$key.'" value="'.$key.'" checked="checked" >';
echo '<label for="'.$key.'" title="'.$value.'">'.$value.'</label>';
} else {
echo '<input name="radio" type="radio" id="'.$key.'" value="'.$key.'" >';
echo '<label for="'.$key.'" title="'.$value.'">'.$value.'</label>';
}
}
echo '<input type="submit" value="Submit"></form>';
if ($_POST["radio"] == 1) {
//Confirmacion($_POST["radio"]);
echo "<p>Has elegido Opcion uno</p>";
}
if ($_POST["radio"] == 2) {
//Confirmacion($_POST["radio"]);
echo "<p>Has elegido Opcion dos</p>";
}
if ($_POST["radio"] == 3) {
//Confirmacion($_POST["radio"]);
echo "<p>Has elegido Opcion tres</p>";
}
$unpost = $_POST['post'];
echo $unpost;
First you have to take into account $_POST,$_GET,$_SESSION and $_COOKIE without arrays and although they are global they can be treated as any array.
The error you mention is the result of using an element of an array that is not defined.
In php the definition of variables is not necessary as in other programming languages. Therefore, a variable can be used without having defined it previously, but the default value will be the default value resulting from the cast .
Although not necessary, it is good programming practice to validate that variables are defined before using them.
I recommend you not to use global variables directly in the body of your script, you can assign it to a variable and use this instead.
$unpost = isset($P['post']) ? $P['post'] : '';
This way even if you want to debug or switch from $_POST to $_GET you only need to change the assignment
$P = $_POST;
to$P = $_GET;
and all your code stays functional AND you'll have better control of what you get back.In the case of multidimensional arrays, you must validate the existence of each dimension before reaching the final dimension, for example:
Here my edit of your code to make the explanation clearer:
You can use to validate the isset() method which returns a value
true
if the variable exists, otherwise it returnsfalse
.Example:
As for the array, you could use empty() to check if it has any value:
Use the isset method for undeclared variables: http://php.net/manual/es/function.isset.php
You can also use empty, although in this case it also evaluates empty variables: http://php.net/manual/es/function.empty.php