In your example isset()
is used to check if the $_POST
superglobal variable is set, but it's redundant code. empty()
already includes the functionality of isset()
but also does more. From the documentation:
Returns FALSE if var exists and has a non-empty, non-zero value.
Otherwise returns TRUE.
The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)
... so it will also check if the $_POST array has elements in it (is not an empty array).
For a simple web form checking, using !empty($_POST)
is easier than writing $_SERVER['REQUEST_METHOD'] === 'POST'
, but the result is the same, if the request was not made through POST, none of them will match.
Additionaly, in your code, you could use isset()
on the $_POST elements also to rule out the warnings issued by PHP if an index is not defined (if an element wasn't submitted in the form):
$email = isset($_POST['adresse_email']) ? $_POST['adresse_email'] : '';
$mdp = isset($_POST['motpasse']) ? $_POST['motpasse'] : '';