173

I often see examples in PHP that include.inc files. What is the meaning of .inc? What it is used for? What are the disadvantages and advantages of using it?

1
  • The extension .inc comes from include and means that it has some info that should be included in a php code, like some configuration, for example as ambient = production. So you load this file and then decode it to get the configuration keys and values.
    – NaN
    Sep 30, 2021 at 19:48

8 Answers 8

304

It has no meaning, it is just a file extension. It is some people's convention to name files with a .inc extension if that file is designed to be included by other PHP files, but it is only convention.

It does have a possible disadvantage which is that servers normally are not configured to parse .inc files as php, so if the file sits in your web root and your server is configured in the default way, a user could view your php source code in the .inc file by visiting the URL directly.

Its only possible advantage is that it is easy to identify which files are used as includes. Although simply giving them a .php extension and placing them in an includes folder has the same effect without the disadvantage mentioned above.

6
  • 9
    @domanokz Yes it will be safe to use then. It will be treated as php only when used in a require or include structure and treated as plain text the rest of the time. Treating it as text on your server won't hurt, it only hurts when your code is served up as text to the end user).
    – Paul
    Aug 20, 2011 at 5:34
  • @Paulpro I had been wondering this fore a while, thank you for making this clear! Oct 17, 2013 at 1:39
  • 1
    Great explanation. Just started at a new job and saw these littered throughout the application. I had no idea it was purely a convention. Thanks!
    – CatDadCode
    Jun 25, 2014 at 19:54
  • 4
    Regarding the server not being configured to parse.inc files as php. It doesn't actually need to be as they are read only from other php files in require/include. Is is safe? As Paulpro indicates you don't want the inquisitive end user reading code so you do want to configure the files to not be retrievable by the end user with something like "RedirectMatch 403 /.*\.inc$" in the Apache conf or .htaccess Nov 6, 2014 at 16:11
  • Note: This is not only for .php and .inc. PHP includes also work with other file types. Including an, '.html', '.js', '.css', '.txt' or any custom file extensions you can think of will also make PHP parse read PHP code from it. Useful when you want to create file based database and want to tell if it's a database file. like .data. You can then use @ClearCrescendo's comment above to secure it. Dec 8, 2015 at 11:28
80

If you are concerned about the file's content being served rather than its output. You can use a double extension like: file.inc.php. It then serves the same purpose of helpfulness and maintainability.

I normally have 2 php files for each page on my site:

  1. One named welcome.php in the root folder, containing all of the HTML markup.
  2. And another named welcome.inc.php in the inc folder, containing all PHP functions specific to the welcome.php page.

EDIT: Another benefit of using the double extention .inc.php would be that any IDE can still recognise the file as PHP code.

1
  • 5
    +1 That is worth mentioning, I did that in one of my projects.
    – kazinix
    Nov 23, 2012 at 2:52
21

Generally means that its a file that needs to be included and does not make standalone script in itself.

This is a convention not a programming technique.

Although if your web server is not configured properly it could expose files with extensions like .inc.

0
15

It's just a way for the developer to be able to easily identify files which are meant to be used as includes. It's a popular convention. It does not have any special meaning to PHP, and won't change the behaviour of PHP or the script itself.

0
5

This is a convention that programmer usually use to identify different file names for include files. So that if the other developers is working on their code, he can easily identify why this file is there and what is purpose of this file by just seeing the name of the file.

0
3

Just to add. Another disadvantage would be, .inc files are not recognized by IDE thus, you could not take advantage of auto-complete or code prediction features.

0
2

In my opinion, these were used as a way to quickly find include files when developing. Really these have been made obsolete with conventions and framework designs.

1
  • This is meaningless comment. I always use .inc for include files because it's easy and there are no negatives, and only positives. The term 'conventions' has no referent that I'm aware of in this context. And PHP frameworks, no idea, never touch them myself, I write code, so the notion that 'frameworks' have replaced PHP coding is just wrong. Nothing wrong with using the stuff if you like it, but believing that it's replaced standard programming, in code editors, is just wrong.
    – Lizardx
    Apr 5, 2016 at 19:11
1

Note that You can configure Apache so that all files With .inc extension are forbidden to be retrieved by visiting URL directly.

see link:https://serverfault.com/questions/22577/how-to-deny-the-web-access-to-some-files

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.