Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

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.