I'm always interested in more efficient ways and learning new things. Right now, I am using the code <?php include('config.php'); ?>
in each and every file. Depending on where the file is in my the folder structure, I would have <?php include('../config.php'); ?>
or even <?php include('../../config.php'); ?>
. How can I make it more efficient? Is there a way to just have it my config.php in root and make everything in root require config.php?
Copy the below code to include the 'config. php' file and get a print the name of the database and username. include ( 'config. php' );
php include("config. php"); at the top of the PHP file you want to access it in. You will then be able to access the config variables form that PHP file it is declared in.
The PHP configuration file allows you to configure the modules enabled, the email settings or the size of the upload files. It is located at installdir/php/etc/php. ini. For example, to modify the default upload limit for PHP, update the PHP configuration file following these instructions.
PHP Include Files. The include (or require ) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.
there is a way to include a file automatically (auto_prepend_file
ini setting), however the biggest improvement you can make is to abandon the usage of multiple php files and use index.php as a single entry point for the whole website.
suppose you write a SO clone ;) with the pages "questions", "tags", "users", etc. On each page you need some generic php stuff (db, session) + common html elements (header, footer). A popular approach is to have a bunch of php files (questions.php, tags.php, users.php) each of them includes the common stuff. For example, users.php will look like this then
include 'db.php';
include 'session.php';
include 'html.header.php';
.....users-specific stuff
include 'html.footer.php';
This is quite tedious (you have to repeat lots of code) and inflexible (think adding a sidebar to all pages on the site). My suggestion is to make includes "inside out" that is, have a "common stuff" file that includes page-specific code:
# index.php
db functions
session functions
html header
$page = isset($_GET['page'])
? preg_replace("/\W+/", "", $_GET['page'])
: "home";
include "$page.php";
html footer
Thus you'll have a single entry point on the website - this is more flexible and better for debugging. The only drawback is that urls are less "nice" (user.php vs index.php?page=user), but this can be easily solved with mod_rewrite
Put the path containing config.php
in your php include path and then you can simply do this:
include 'config.php';
or better yet:
require_once 'config.php';
require
is preferred over include
because it triggers an error instead of a warning when a file cannot be included for some reason (e.g. file not found, permissions error, etc.). The _once
suffix is a good addition to make sure the same file isn't needlessly included multiple times.
Also, please note that you don't need the parenthesis around 'config.php'
in your include
call. include
is not a function in php. It's a language construct. The parenthesis just serve as a needless grouping not unlike this example: $myVar = (2 + 2);
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With