Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Leigh Maher
Leigh Maher
20,340 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,106 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
20,340 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,106 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
20,340 Points

Great. Thanks for the explanation, Kevin.