Karlos Posted January 1, 2009 Share Posted January 1, 2009 What do people use? I use require_once (DIRNAME(__FILE__) . '/globals.php'); why? Give the excat path to the file :wink: Quote Link to comment Share on other sites More sharing options...
POG1 Posted January 1, 2009 Share Posted January 1, 2009 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.. Quote Link to comment Share on other sites More sharing options...
Lithium Posted January 1, 2009 Share Posted January 1, 2009 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. Quote Link to comment Share on other sites More sharing options...
03laceys Posted January 1, 2009 Share Posted January 1, 2009 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. Quote Link to comment Share on other sites More sharing options...
POG1 Posted January 1, 2009 Share Posted January 1, 2009 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 Quote Link to comment Share on other sites More sharing options...
Karlos Posted January 1, 2009 Author Share Posted January 1, 2009 Re: Include or Require? Why use DIRNAME(__FILE__)? globals.php will most probably be in the same directory.. Well I always use it because it better to have a direct path I personally believe. Quote Link to comment Share on other sites More sharing options...
POG1 Posted January 1, 2009 Share Posted January 1, 2009 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 Quote Link to comment Share on other sites More sharing options...
Zeggy Posted January 1, 2009 Share Posted January 1, 2009 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) Quote Link to comment Share on other sites More sharing options...
Lithium Posted January 1, 2009 Share Posted January 1, 2009 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 Quote Link to comment Share on other sites More sharing options...
Floydian Posted January 2, 2009 Share Posted January 2, 2009 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'); Quote Link to comment Share on other sites More sharing options...
Guest Anonymous Posted January 8, 2009 Share Posted January 8, 2009 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. Quote Link to comment Share on other sites More sharing options...
Floydian Posted January 9, 2009 Share Posted January 9, 2009 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 :) Quote Link to comment Share on other sites More sharing options...
Karlos Posted January 10, 2009 Author Share Posted January 10, 2009 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 :-) Quote Link to comment Share on other sites More sharing options...
POG1 Posted January 10, 2009 Share Posted January 10, 2009 Re: Include or Require? try require_once ("../globals.php"); Quote Link to comment Share on other sites More sharing options...
Karlos Posted January 10, 2009 Author Share Posted January 10, 2009 Re: Include or Require? Naa, I already tryed require_once (DIRNAME(__FILE__) . '\..\globals.php'); and it works fine. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.