Jump to content
MakeWebGames

Include or Require?


Karlos
 Share

Recommended Posts

Re: Include or Require?

Include and require are the same but the difference is the way they handle errors. They will both give a warning/error but require will return a fatal error.

Why use DIRNAME(__FILE__)? globals.php will most probably be in the same directory..

Link to comment
Share on other sites

Re: Include or Require?

 

it is indeed better to place the full path as php will "try to guess" the relative path upon the root folder. If you place the full path it will indeed increase the performance, as it will go dsrectly to the file and process it.

Very interesting i never knew this i have only every simply used,

include "file.extension";

or

require "file.extension";

Although i prefer to use the require, but now with this new information i'm going to edit my codes to require with the full path.

Link to comment
Share on other sites

Re: Include or Require?

 

it is indeed better to place the full path as php will "try to guess" the relative path upon the root folder. If you place the full path it will indeed increase the performance, as it will go dsrectly to the file and process it.

Very interesting i never knew this i have only every simply used,

include "file.extension";

or

require "file.extension";

Although i prefer to use the require, but now with this new information i'm going to edit my codes to require with the full path.

whats the full path then? /?

just use a relative file path, it will work just fine

Link to comment
Share on other sites

Re: Include or Require?

 

it is indeed better to place the full path as php will "try to guess" the relative path upon the root folder. If you place the full path it will indeed increase the performance, as it will go dsrectly to the file and process it.

What would the full path be?

/usr/share/blah/blah....

globals.php is in the same directory as the file viewed,therefore there is no need for any extra functions

Link to comment
Share on other sites

Re: Include or Require?

It's better if you use absolute paths rather than relative paths if you are including files that include other files, and all the files are in different directories. Saves a lot of confusion if you know (as well as the computer) exactly where your includes/requires are pointing to.

 

/ = current

../ = up one level

./ = find it (This is the one that may cause a problem, the other 2 I see nothing though)

Actually:

/ = a directory itself, the top directory (absolute)

../ = parent directory (relative)

./ = current working directory (relative)

Link to comment
Share on other sites

Re: Include or Require?

 

it is indeed better to place the full path as php will "try to guess" the relative path upon the root folder. If you place the full path it will indeed increase the performance, as it will go dsrectly to the file and process it.

I want some proof of this.

I have seen nothing that would suggest so and given that there are different wants to display stuff, I would assume the server knows where to go.

/ = current

../ = up one level

./ = find it (This is the one that may cause a problem, the other 2 I see nothing though)

Require() uses the relative path, and php processes as you said, on that way, though even that way, php needs to make a filessytem call to know the full path of the file... therefor is far better using the full path from the start, and not the relative path

Link to comment
Share on other sites

Re: Include or Require?

It's really up to personal preference which you use.

It's my opinion that most of the time, included files are required. So I normally use require_once.

require is a bit faster than require_once, they say (on php manual page, I'll take that as a solid ref)

As far as include paths, I prefer to use constants, defined in a config file for include paths.

require_once(BASE_DIR . 'blah.php');

Link to comment
Share on other sites

Guest Anonymous

Re: Include or Require?

Full paths need not be used - in fact it is better not to use them... Consider what if you want to move your site from one location to another -- using relative paths means you have no changes to make.

include (or include_once) is IMO far better over require as it prevents information disclosure as it can be trapped by an error handler.

Link to comment
Share on other sites

Re: Include or Require?

There's certainly an argument for relative paths.

But there's an equally valid argument for abs. paths (although when it comes to html links I'd highly recommend not using abs. links)

 

The argument for abs. paths, defined in one config file for the entire site, is that should you ever want to move your include folder, you aren't tied in by all those relative paths.

Should you ever move your site to a new domain, simply changing the BASE_URL and BASE_URI would change every other path as well.

I use BASE_URL as "http://example.com/"

and BASE_URI as "/home/user/public_html/"

Then the include path would be defined using the BASE_URI

define('BASE_INC', BASE_URI . 'includes/');

 

Doing it that way means there is only two things to change, and it's actually sort of relative, no?

Incidentally, Horizons Game Engine is setup that way :)

Link to comment
Share on other sites

Re: Include or Require?

 

/ = current

../ = up one level

./ = find it (This is the one that may cause a problem, the other 2 I see nothing though)

Actually:

/ = a directory itself, the top directory (absolute)

../ = parent directory (relative)

./ = current working directory (relative)

Well i never needed to use these before but "up one level" didn't work when it did it in:

require_once (DIRNAME(__FILE__) . '/globals.php');

and i had to make it:

require_once (DIRNAME(__FILE__) . '\..\globals.php');

to get it to work :-)

Link to comment
Share on other sites

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.   Paste as plain text instead

  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.

 Share

×
×
  • Create New...