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

Development Tools Using PHP with MySQL Querying the Database with PHP Avoiding Duplication

Leigh Maher
Leigh Maher
21,830 Points

How does the database.php know to get the values we defined in the config file?

I notice that Randy didn't include the config file in the database.php, but it still works fine.

How does the database.php know to get the values we defined in the config file?

3 Answers

Kevin Korte
Kevin Korte
28,149 Points

Because when the code is actually executed, the config data is there with the database. On files that show product, he has two files required...config and products. In the product file, he requires the database file.

So when this file gets executed, the config data comes in, the product data comes in, and the database file comes in. When you call a require, its like copying and pasting the code from that file into the file that has the require. So "technically" all the information is there at run time, and it doesn't have to "look" for the information.

I don't think I was clear there, but does that help?

Leigh Maher
Leigh Maher
21,830 Points

Thanks for the reply, Kevin.

So, I can see that in products.php he requires the database file, and on the shirt.php page he requires the products.php and config.php files. So, are you saying that when shirt.php loads, it effectively facilitates the link between the the config.php and database.php file?

Kevin Korte
Kevin Korte
28,149 Points

Yes, exactly, so when it loads the shirt.php file, it goes in down the file, first loading config.php, than it loads products.php, and while it's doing that, it loads the database.php, so as the PHP server views it, all the code from config.php, product.php, and database.php are loaded and loaded in an order where nothing is called before it's used.

Leigh Maher
Leigh Maher
21,830 Points

Great. Thanks for the explanation, Kevin.