Discussion:
beginners question
Sachin Raut
2014-10-15 05:25:13 UTC
Permalink
hi group,

i just want to know if the following statements are same or not.

require 'includes/config.inc.php';

&

require './includes/config.inc.php';

regards
Sachin
Tobias Fichtner
2014-10-15 05:40:34 UTC
Permalink
Hi Sachin,

no. That's not the same.

In your first sample you have only a file name, this will be search into the include paths.

In your second sample you have a path to a file, the include path will be ignored.


from http://php.net/manual/en/function.include.php:
"If a path is defined — whether absolute (starting with a drive letter or \ on Windows, or / on Unix/Linux systems) or relative to the current directory (starting with . or ..) — the include_path will be ignored altogether. For example, if a filename begins with ../, the parser will look in the parent directory to find the requested file."

Mit freundlichen GrÌßen,
King regards,
Tobias Fichtner

--
[w] http://tobiasfichtner.com/ [gpg] 0x8498F4B2 [fp] 2978 57FB 155B 30BE 7316 AA7D D79C 4A0A 8498 F4B2

-----UrsprÃŒngliche Nachricht-----
Von: Sachin Raut [mailto:***@gmail.com]
Gesendet: Mittwoch, 15. Oktober 2014 07:25
An: PHP Mailinglist
Betreff: [PHP] beginners question

hi group,

i just want to know if the following statements are same or not.

require 'includes/config.inc.php';

&

require './includes/config.inc.php';

regards
Sachin
Jim Giner
2014-10-15 12:43:14 UTC
Permalink
What tobias said I disagree with. In both of your examples you have
included a path. The second one though adds that dot which I do believe
means to start at the current directory. I've never seen anyone use
that syntax since your first one will do the same thing.

If I'm wrong I'm sure we'll hear about this.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Andy McKenzie
2014-10-15 12:59:04 UTC
Permalink
Post by Jim Giner
What tobias said I disagree with. In both of your examples you have
included a path. The second one though adds that dot which I do believe
means to start at the current directory. I've never seen anyone use that
syntax since your first one will do the same thing.
If I'm wrong I'm sure we'll hear about this.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Tobias was more right, I think.

To break down the two constructions:

require 'includes/config.inc.php'; says "Look at the include path that PHP
knows about, and try to find a sub-directory called 'includes' that has a
file called 'config.inc.php'. If you can't find it there, try the
directory you're running in."


require './includes/config.inc.php'; says "Look at the directory you're
running in, and then the sub-directory 'includes' and the file
'config.inc.php'.


So the two constructions don't actually do the same thing; if there's a
file called 'includes/config.inc.php' anywhere in the global includes_path,
it will be called with the first setup. That's one reason I almost always
use a fully defined path, like the second construction. It makes it easier
to know exactly what file I'm going to be inserting into my code, and
reduces the risk of someone else on the system inserting their own code,
either accidentally or not.

Andy
Jim Giner
2014-10-15 13:26:10 UTC
Permalink
I think that we have a difference in definitions of what a filename is.
My def is that 'includes' is not part of a filename. The filename is
simply 'config.inc.php'. Anything appended to it is part of its
location (or path) and not part of the name. This is based upon the
fact that when you actually get to the right location and find the file
to be included it is called 'config.inc.php' and nothing else.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Tobias Fichtner
2014-10-15 14:04:31 UTC
Permalink
Hi,

the point is that both include the same files if the include path contains "./" already.

means

include 'config.php'; // include a file named config.php in (default: ".;/path/to/php/pear") follow paths "./" and "/path/to/php/pear/" in this order.
include './config.php'; // include a file named config.php in the same directory. If they would include a config.php from "/path/to/php/pear/" we would have a big problem.

And a god way to include files is to build an absolute path to include like:
[BEGINN define_important_thinks.php || in doc root ]
<?php
define('APP_ROOT' , dirname(__FILE__) . DIRECTORY_SEPARATOR ); // create an absolute path to doc root
[END]

[ BEGINN some_great_application.php || anywhere in your project ]
<?php
require_once './define_important_thinks.php'; //relative path to define_important_thinks.php

include APP_ROOT . 'any_needed_file.php'; // includes any_needed_file.php

echo 'some source';
[END]


Note: the difference between require and include is the error exception. Require means required and not able to run without (E_COMPILE_ERROR) and include give u an error notice (E_WARNING) that it could not included but PHP try to run the code.

Mit freundlichen GrÌßen,
King regards,
Tobias Fichtner

--
[w] http://tobiasfichtner.com/ [gpg] 0x8498F4B2 [fp] 2978 57FB 155B 30BE 7316 AA7D D79C 4A0A 8498 F4B2

-----UrsprÃŒngliche Nachricht-----
Von: Jim Giner [mailto:***@albanyhandball.com]
Gesendet: Mittwoch, 15. Oktober 2014 15:26
An: php-***@lists.php.net
Betreff: Re: AW: [PHP] beginners question

I think that we have a difference in definitions of what a filename is.
My def is that 'includes' is not part of a filename. The filename is simply 'config.inc.php'. Anything appended to it is part of its location (or path) and not part of the name. This is based upon the fact that when you actually get to the right location and find the file to be included it is called 'config.inc.php' and nothing else.



--
PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Andy McKenzie
2014-10-15 15:23:20 UTC
Permalink
Post by Tobias Fichtner
Hi,
the point is that both include the same files if the include path contains "./" already.
Except that they don't. They include one of the same files, but there's no
guarantee that it will be the one called.

Say I use "includes/config.inc.php". Suppose that my include_path is set
to "/usr/php/54/usr/lib64:usr/php/54/usr/share/pear:.". In this case,
there are three files that could be called. The system first looks for
/usr/php/54/usr/lib64/includes/config.inc.php. Then it looks for
usr/php/54/usr/share/pear/includes/config.inc.php. Then it looks for
./includes/config.inc.php. There are three possible files that can be
included there. If there's a file in the first location, that's the one
that will be included. Now, you can avoid that by making "." be the first
entry in the list (and, in fact, it is on my system), but unless you know
what the include path is you cannot guarantee what file will be called.

Now suppose I use "./includes/config.inc.php", and I'm calling it from
"/var/www/htdocs/myproject". There is only one possible file that can be
called.

Is this nitpicking? Yes, a bit. However, I've been in a situation where
it mattered. It was an early IT job, and I didn't really know what I was
doing, and neither did the other person working on the web server. They
wrote a new program, and needed to add something to the include_path.
Rather than do it in their program, they went in and edited php.ini, adding
their directory as the first entry in the list. Since we both had called
files called "config.inc", my program stopped working.

My point isn't so much that either way is wrong, as that if you don't
understand the behavior it can mess things up, and you won't understand why.

Andy

Loading...