Problem 1 previously encountered a problem in another project: when PHP uses foreach to send emails cyclically, the following error occurs: {code ...} the code execution result should be: one email is received in each of the three mailboxes, but the fact is that one email is received in the first mailbox and one email is received in the second mailbox... question 1
I have encountered a problem in other projects before: the following error occurs when PHP uses foreach to send emails cyclically:
Sample Code: $ emails = ['2017 @ qq.com ', '2017 @ qq.com', '2017 @ qq.com ',]; // $ Email is the mail sending class foreach ($ emails as $ email) {$ Email-> send ($ email, 'subobject', 'test ');}
The code execution result should be: one email is sent to each of the three mailboxes, but the fact is:
First
Email received 1
Email;
Second
Email received 2
Email;
Third
Email received 3
Email;
Why? Later, the project email was changedAmazon Mail Service
, So I didn't think about it.
Question 2
Recently, a similar problem has been encountered in the project: the scheduled script obtains remote URL content in batches. The code is roughly as follows:
$ Sites = ['www .site1.com ', 'www .site2.com', 'www .site2.com ',]; // $ CURL complement foreach ($ sites as $ site) {$ result = $ CURL-> get ($ site); var_dump ($ result );}
I thought it would be to print the return values of the three sites, but the time was not the case. When reviewing the database logs, I found that a lot of data is not correct.
It is confusing to use it in a loop. CURL
Are you sure you want to consider concurrency? Cainiao.
Reply content: Question 1
I have encountered a problem in other projects before: the following error occurs when PHP uses foreach to send emails cyclically:
Sample Code: $ emails = ['2017 @ qq.com ', '2017 @ qq.com', '2017 @ qq.com ',]; // $ Email is the mail sending class foreach ($ emails as $ email) {$ Email-> send ($ email, 'subobject', 'test ');}
The code execution result should be: one email is sent to each of the three mailboxes, but the fact is:
First
Email received 1
Email;
Second
Email received 2
Email;
Third
Email received 3
Email;
Why? Later, the project email was changedAmazon Mail Service
, So I didn't think about it.
Question 2
Recently, a similar problem has been encountered in the project: the scheduled script obtains remote URL content in batches. The code is roughly as follows:
$ Sites = ['www .site1.com ', 'www .site2.com', 'www .site2.com ',]; // $ CURL complement foreach ($ sites as $ site) {$ result = $ CURL-> get ($ site); var_dump ($ result );}
I thought it would be to print the return values of the three sites, but the time was not the case. When reviewing the database logs, I found that a lot of data is not correct.
It is confusing to use it in a loop. CURL
Are you sure you want to consider concurrency? Cainiao.
Paste the complete code
Question 1: What mail class library do you use?
$ Mailer-> send ($ emails,...) here $ emails can be imported into the Array
The reason why the number of emails received is incorrect may be related to internal pointers. This is also related to the PHP version. The same explanation applies to question 2.
You can take a look at the instructions on foreach in the official PHP documentation.
In PHP 5, when foreach first starts executing, the internal array pointer is automatically reset to the first element of the array. this means that you do not need to call reset () before a foreach loop.
As foreach relies on the internal array pointer in PHP 5, changing it within the loop may lead to unexpected behavior.
In PHP 7, foreach does not use the internal array pointer.
Problem 1: I guess the cause is that the topic and content of the message are saved during sending. The second call of send is to save one, so that the content is accumulated.
Solution: You can change the email library. Or you can create a new email object in foreach, so that each email object is different.
Problem 2: curl is a synchronous request. A simple pull request can be replaced by the file_get_contents function.
Problems found temporarily:
Your question 1 $ Email class and loop variable $ email is a duplicate name, right?
Question 2: Is there an error in the get method logic?