Assignment to not yet defined array index
$mailSent = mail($to, $subject, $message, $headers);
if (!$mailSent) {
$errors['mailfail'] = true;
}
I'm confused by this line:
$errors['mailfail'] = true;
$errors
is an empty array, so basically the index mailfail
开发者_开发知识库 doesn't exist. Right?
//use "Mail (Function)" and assign the result to a "Variable"
$mailSent = mail($to, $subject, $message, $headers);
//use "Control Structures" to check the state of the "Variable": true / false
if (!$mailSent)
{
//Assign the index 'mailfail' of an "Array" to a "boolean" of true
$errors['mailfail'] = true;
}
- Variables
- Mail (Function)
- Control Structures
- Arrays | (Extended)
- Booleans
$mailSent is a variable
mail($to, $subject, $message, $headers); Mail is a function name. This function will send the mail. The function has 4 parameters, to, subject, message and headers.
If the function fails to send the mail it will set the value of the variable to false. If it succeeds it will set the value of the veriable to true
The next line says if (!$mailSent) { $errors['mailfail'] = true; }
if the variable is NOT true. Actualy this means "If the variable contains "false" the ! signs means NOT
{ $errors['mailfail'] = true; } This line will ONLY be executed if the variable contains false (meaning that the function called "mail" was not succesfull
$errors['mailfail'] = true; $errors is a collection of variables. The variable called 'mailfail' inside $errors is set to true at this line.
It tries to send an email and then checks whether the sending of the email was successful or not, and sets the variable $errors['mailfail'] to true if the sending failed.
I suppose the one thing that might confuse you about it is that it has an exclamation mark before $mailSent in the if-statement. That exclamation mark simply means "not". So it's "if (not $mailSent), set $errors['mailfail'] to true." - it depends on the function in question (in this case the mail(...) function) what the result really means. This function returns "false" when it fails (as according to the documentation), so the question: "is not $mailSent?" returns a true if the $mailSent is false, meaning that the stuff within {...} gets executed. If you take out the exclamation mark, it will execute the stuff within {...} only if the mail() function returns true (which means "success" in this case).
Note that it doesn't check whether the mail was received or not, only whether it was sent or not.
It basically states : "If the mail failed to send - show that errors occurred"
It passes in the mail/message information (To, Subject, Message, Headers) and stores the result of that function into $mailSent
.
If $mailSent
has a value indicating success - then nothing else would occur (as it was sent correctly). But if has a value indicating failure (!$mailSent
~ Not Successful) then it would set an error for "mail failure" to true
.
You try to sent email to $to
with subject $subject
, message $message
and headers $headers
, if error occures then put in $errors['mailfail'] = true;
http://php.net/manual/en/function.mail.php
http://php.net/manual/en/control-structures.if.php
According to the docs (link), the PHP function mail
returns a boolean value indicating success or failure. There is, presumably, an array called $errors
. If the email fails, the array index mailfail
is set to true.
Anything that holds a value doesn't need to be declared before assigning to it, whether it's a plain variables, array index or object property; assignment will create them as needed. This means that after executing
$errors['mailfail'] = true;
the array $errors
will have an entry with key 'mailfail' and value true
, even if $errors['mailfail']
(or $errors
) wasn't defined before the line was executed.
Unlike reading from an undefined variable or index, assigning to one won't generate a warning. When it comes to values, arrays can also be auto-created. Objects will also be auto-created, but will generate a warning in strict mode.
<?php
# note that of PHP 5.4, E_ALL includes E_STRICT
error_reporting(E_ALL | E_STRICT);
# creates a variable named $foo:
$foo = 'bar';
# arrays don't need to be created explicitly; the following creates
# $array and $array['foo'] as necessary
$array['foo'] = 'bar';
# objects should be created explicitly,
$obj = new StdClass;
# but object properties don't
$obj->foo = 'bar';
You can even specify nested indices in arrays that don't exist, and the intervening arrays will be created as needed.
unset($array);
$array['foo'][42]['bar'][]['baz'][]['bam']='bug-AWWK!';
Unlike arrays, intervening objects will generate a warning in strict mode (which you should always use when developing) when created automatically, so it's better to create them explicitly.
$obj = new StdClass;
# The following will generate a "Creating default object from empty value"
# warning in strict mode,
//$obj->foo->bar[5]->baz->bam = 'bug-AWWK!';
# so you need to explicitly instantiate each intervening object,
$obj->foo = new StdClass;
# but don't need to assign arrays
# $obj->foo->bar = array();
$obj->foo->bar[5] = new StdClass;
$obj->foo->bar[5]->baz = new StdClass;
$obj->foo->bar[5]->baz->bam = 'bug-AWWK!';
精彩评论