It sound a Stupid question but I don know what is the problem each time i try to declare a variable inside a function it gives me an error !
function emptyInputSignup($name, $email, $password, $confirm_password)
{
$result;
if (empty($name) || empty($email) || empty($password) || ($confirm_password)) {
$result = true;
} else {
$result = false;
}
return $result;
}
I’ve tried to declare it and i got an error.
2
Answers
You don’t declare variables in PHP, you define them.
There’s no need for
$result;
and it actually means "read the value of the variable$result
and then do nothing with it" – which will give you an undefined variable error if$result
isn’t defined (which, in your case, it is).What you have to do instead is initialize
$result
to some value, or leave it out completely:$result = false;
For example.
Your code can be greatly simplified by just using assigment:
No need for the
if
. And you can actually ditch the$result
variable entirely:You can initialize a variable like this:
You can try using any of the following:
All these constructs produce the same result.