Why is the variable null defined in php first?

$name = $email = $gender = $comment = $website = "";

if ($_ SERVER ["REQUEST_METHOD"] = "POST") {
$name = test_input ($_ POST ["name"]);
$email = test_input ($_ POST ["email"]);
$website = test_input ($_ POST ["website"]);
$comment = test_input ($_ POST ["comment"]);
$gender = test_input ($_ POST ["gender"]);
}

is more standardized, because many languages must first define variables when using variables. Although php does not have such a mandatory requirement, it is easier for everyone to understand if it is written this way. Is that the reason?

Php
Jun.03,2021

. This way doesn't look very beautiful. Let's make a statement one by one.
but it is not for everyone to understand that php types are automatically checked and variables are initialized automatically. So there's no need for a mandatory declaration.


this depends on personal habits. There is no mandatory definition of variables before using variables in php.

but defining it in advance does make it easier for people who look at the code later to know what type the variable is.


is generally written to avoid missing variables, resulting in undefined variables, which are no longer needed in modern IDE.
and the use of your test_input function if it's just to get the value of post, then it's a bit of a killing knife.


generally defines an empty variable, which is mainly used for initialization.


because an error will be reported.
comment out the first line, and then you can access the file directly (that is, you won't have to try it in if),).

Menu