Wednesday, July 15, 2009

How to get a PHP file to be included across files in different directories?

Currently I have created some php based webpages and uploaded them to my web hosting server.It has a PHP 5.1.6



installed.With this I am able to include files only that are in the same directory(they work well using require() fn).If not an error message displaying some %26quot;no such file%26quot; in directory appears in the page.I would like to use them to be included in different direcetories.What should I do?



How to get a PHP file to be included across files in different directories?





create the directories and include the names when using the require() function.



example :



your index.php is in the root directory



/index.php



and you have a directory with your other php files in



/lib/other.php



so in index.php you will have



require(%26quot;lib/other.php%26quot;);



How to get a PHP file to be included across files in different directories?



Yes thats right you could use a require() method and specify the path of the file to call or else you could use the include method. For example



vars.php



%26lt;?php



$color = %26#039;green%26#039;;



$fruit = %26#039;apple%26#039;;



?%26gt;



test.php



%26lt;?php



echo %26quot;A $color $fruit%26quot;; // A



include %26#039;vars.php%26#039;;



echo %26quot;A $color $fruit%26quot;; // A green apple



?%26gt;

No comments:

Post a Comment