Jump to content

Recommended Posts

Ok, let's say I have some files:

 

index.php

/includes/header.php

/includes/images/logo.jpg

 

Now the logo appears in the header.php file and the header.php appears in the index.php file.

 

I've been using things like:

<?php require_once('includes/header.php'); ?>

 

but it doesn't appear to work in all instances - is there a limit on the number of nested includes?

Link to comment
https://forums.phpfreaks.com/topic/82541-php-includes-explanation/
Share on other sites

No there isn't a limit.  Firstly, avoid using

 

require_once();

 

instead use

 

require();

 

It basically does the same thing and uses less resources.

 

When you say it doesn't work, what exactly doesn't it do?  Does it not show any of the content in the include file at all?

ok, require it is!

 

Well I'm building a site and I'm trying to build a 'template' file for all the pages - ie so a) it's simpler and b) easier to update.

 

I want to for example include the header, footer and layout info using an include and then be left with simply the actual content needing written.

 

The way I do it at work (using ASP mind you!) is say to specify the CSS I have an introductory slash for the root - so for the css it'd be something like:

 

/css/styles.css

 

This'll work in all directories.

 

Now this doesn't work on my one (I 'think' this could be because I'm on shared hosting and / tries to specify the root of the server.

 

So I then created a variable with my server location in it:

 

$server = "http://www.test.com/";

 

And I then concatenate using an ugly-bugly line like:

 

<?php include($root."test/mysql_connect.php"); ?>

 

However this still requires me to specify the server root in each file!

 

Can anyone explain how they do this?

Because an include/require uses a file system path, the leading slash refers to the root of the current hard disk instead of the document root.

 

See this post for how to do what you want - http://www.phpfreaks.com/forums/index.php/topic,172711.msg766014.html#msg766014

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.