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

Micah Grotte
PLUS
Micah Grotte
Courses Plus Student 8,645 Points

How do I put a VARIABLE inside INCLUDE path?

I am working on a project that I would like to include a variable inside my include path. Something like:

" <php include ('php/product' . $PageName . '.php'); ?>"

The variable, "$PageName" is previously defined in the header.

Is this possible? I've tried it a couple ways and can't seem to get it to work

3 Answers

Hi Micah,

That should work but you left off the question mark on your opening php block. Should be <?php

Was that just in the forums or were you leaving that off your actual code?

I made up a few files to test this out.

php/product1.php:

<?php echo "<h1>This is product 1</h1>"; ?>

php/product2.php:

<?php echo "<h1>This is product 2</h1>"; ?>

In test.php I tried out your way and also what Peter Ramsing suggested. These should be the same though. Either use string concatenation or if you use double quotes then variables will be evaluated.

test.php:

<?php
        $PageName = "1";
        include ('php/product' . $PageName . '.php');
        include ("php/product$PageName.php");

        $PageName = "2";
        include ('php/product' . $PageName . '.php');
        include ("php/product$PageName.php");
?>

html output:

<h1>This is product 1</h1>
<h1>This is product 1</h1>
<h1>This is product 2</h1>
<h1>This is product 2</h1>

Is this the general idea of what you were trying to do? Or is there more to it than this?

Michael Collins
Michael Collins
433 Points

Look up _autoload, spl_autoload and spl_autoload_register

This is an issue that people run into when implementing more substantial frameworks. Here's the issue ...

There are lots of ways to enter a site, invoking a page, invoking an application. Like Zend or Symfony, or other frame works, you may have thousands of files with Tens of thousands of functions and class methods. You don't want to load all of them on every page invocation--do you? That would make your app incredibly slow.

On the other hand, you don't know HOW someone is going to enter your site/application, so you don't really know which 7 or 10 Classes need to be loaded for any specific random request. What to do?

Somewhere, you have to create a relationship between the URL that is called to get to your application and the Classes and Methods that are called that make it work--only calling the tiny fraction you need for each specific request.

So most of these frameworks have a Router Function which matches URL requests to Classes and Methods.

And, they build a known pattern between the Class Name that is called and the name and location of the file that contains the class and it's method.

So, URL -> Class Name and Methods (handled by Router Functions) And, Class Name and Methods -> include_once("the correct files") (handled by __autoload and spl_autoload).

Peter Ramsing
Peter Ramsing
16,814 Points

Try:

<?php include ("php/product$PageName.php"); ?>