Imri Persiado
Imri Persiado

Reputation: 1889

Managing include statement

I have 4 files:

File a and b need to include config.php and index.php needs to include a.php & b.php

So the index.php will look something like:

<?php include('a.php'); include('b.php'); ?>

My question is, how would you manage the include of the file config.php. Should I include it in a.php and b.php or just once in index.php or do you have another idea.

Upvotes: 0

Views: 46

Answers (2)

ComFreek
ComFreek

Reputation: 29424

Do all your files (except index.php) need the configuration? If so, I would advise to always include config.php in index.php:

// index.php
require_once ('config.php');

You could of course use also include config.php in a.php or b.php, but I would not recommend that practice since this leads to lots of unnecessary includes (bad code).

Upvotes: 2

hargobind
hargobind

Reputation: 592

If a and b don't need to be accessed directly, there's no need to include config.php in them. They will pick up the variables that have already been included in index.php. But if they are separate pages that can be accessed directly and/or can be included in index.php, then you would want to have your include statement at the top of those pages as well.

Your best bet is to use include_once() or require_once()

Upvotes: 3

Related Questions