Jessica Brownie
Jessica Brownie

Reputation: 11

How to incude files runtime in PHP

I want to include different files, depending on a parameter passed to get method. E.g. if the id passed in the URL is 101 then I want to include file_101.php. Here is what I have so far:

if(cat_id == 101)
{
    $filename="file_101.php";
}

//some code here
include('$filename');

I have also tried:

include('echo $filename;');

but it is giving error

include() [function.include]: Unable to access echo $filename;

Upvotes: 1

Views: 81

Answers (2)

Filippo Alessi
Filippo Alessi

Reputation: 605

Your echo is too many! You can include file with:

include 'file.php';

Or you can use methods that others have written.

Replace with $cat_id cat_id also!

PS. I counsel you to specify the name of the file that you include without variables.

Upvotes: 0

John Conde
John Conde

Reputation: 219824

Get rid of the single quotes. They make PHP parse the $ as a literal dollar sign instead of the beginning of a variable name.

include($filename);

Upvotes: 6

Related Questions