PHP Build a Basic PHP Website (2018) Adding a Basic Form Using A Third-Party Library

Matt Corby
Matt Corby
2,385 Points

Why require mailer in the middle of the code?

I'm wondering why Alena put the require(inc/phpmailer/class.phpmailer.php); after the first 3 if statements. Would it be bad to write that line of code at the top of the file?

1 Answer

Corey Cramer
Corey Cramer
9,279 Points

Slightly performance improvement by not loading an extra file until you are sure you're going to use it. Once you're certain you have the bits and pieces to send the e-mail then you pull in the library and use it as opposed to pulling in PhpMailer and checking to find out they haven't submitted a form so delivering the form. Then when they fill out the form and submit it you include the PHP mailer again, confirm that the request method is a post and you have the required information and continue chugging along.