I've been having issues with my PHP include function. So can anyone explain to me why this would work...
//testfile1.php
<?php
include 'file1.php';
?>
//testfile2.php
<?php
include 'file2.php';
?>
but this will NOT work..
//testfile.php
<?php
include 'file1.php';
include 'file2.php';
?>
Is there anyway to include the second file within the main file? I'm suppose to streamline two php files into one main file and run that. (All files are on the same directory and can 'see' each other no problem.)
Try changing your include statements to include_once. Look for includes within your included files and change them also.
If file1.php also includes file2.php itself, and file 2 had a function declaration, you could be re-declaring a function with the same name.
Looks like you're overlapping include with files which are probably being included twice or not at all. In this case you need to use either
include_once() instead of include()
You can also use
require_once() instead of include() //use this option if the include is required
Related
I am very new to PHP programming. I am trying to write this custom php code in Drupal and seeing this weird behavior. Basically I have two php files which users can hit and the first php file is showing output from the second one. I am not including (include) the second file in the first one.
Home.php - The first file (outputs 'why am i executing' at the end)
<?php include 'HomeView.BL.inc';
//Other links to access second file
?>
HomeView.BL.inc
<?php
include 'db.inc';
include 'dao.inc'; - has a class called IDA_Map
?>
FacultyInternshipDetail.php - The second file
<?php
include 'InternshipDetailView.BL.inc';
?>
InternshipDetailView.BL.inc
<?php
echo "why am i executing";
include 'db.inc';
include 'dao.inc';
?>
Apart from the output from the second file, I am also seeing this error - Cannot redeclare class IDA_Map.
I have read in other posts about 'include_once' but didn't expect re-declaration error since the class (IDA_Map) is declared once per request.
Thank you.
Flip all these to require_once.
<?php require_once'HomeView.BL.inc';
//Other links to access second file
?>
HomeView.BL.inc
<?php
require_once'db.inc';
require_once'dao.inc'; - has a class called IDA_Map
?>
FacultyInternshipDetail.php
<?php
require_once'InternshipDetailView.BL.inc';
?>
InternshipDetailView.BL.inc
<?php
echo "why am i executing";
require_once'db.inc';
require_once'dao.inc';
?>
include breaks when you try to include the same file twice.
Also look up composer and into using a php framework so you dont have to worry about any of this!
i have strange problem with include commnad
my files structure
/index.php
/files/init.php
/files/db.php
and sources of this files
index.php
<?
include ('files/init.php');
?>
init.php
<?
include ("db.php");
?>
Source from db.php is not executing.. NOW if i rename file db.php to db2.php and rename it in init.php NOW IT WORKS
there is some php cache or what ? i dont understand it.
conflict filename DB.php from PEAR.
DB.php from PEAR is included instead of your "db.php"..
So use filename other than "db.php".
EDIT or use include with DIR macro
try this
<?php
include ("db.php");
?>
or using ./
<?php
include ("./db.php");
?>
The included files are resolved according to the entry point (here index.php).
Moreover, require is better is the file MUST exist.
index.php
<?php require 'files/init.php'; ?>
files/init.php
<?php require 'files/db.php'; ?>
Update 1: As suggested by #Darsstar, here is an example using __DIR__:
files/init.php
<?php require __DIR__.'/db.php'; ?>
Update 2: Maybe you should consider PSR-0 or PSR-4 to deal with file inclusion.
My header.php looks like this.
include("sessions.php");
<h1>...
My sessions.php page looks like this
session_start();
function validate_user()
{
//validate user
}
I have a user_class.php in my body. can i just call validate_user() because i have already included it in the page or do i have to include it in my user_class.php and every .php that uses a session function?
Short answer, yes. What your asking is essentially the whole purpose of includes. Using includes you can divide you code into chunks (files) that represent functional units, such as your session handling. As some of the other posters have suggested, one of your chunks might be your initialization code.
Imagine includes as simply inlining the file into the point of the include/require statement, replacing it. In this model, as long as the function would appear above the point you are using it, you're good to go.
At the end of the day, it's you preference on how you divide up your code but books like the classic "Design Patterns" from the Gang of Four can help you make good decisions about how to design your code for maximum ease of maintenance and re-use.
If your user_class.php relies on sessions.php having been included, you may want to put a require_once "sessions.php"; at the top of your user_class.php. Additionally, I would recommend using include_once instead of just include.
I'd avoid mixing procedural code and function definitions.
Instead, move your procedural code (ie session_start()) to a separate file, eg bootstrap.php and include that at the top of your pages.
To ensure functions and classes defined in other files are included, use require_once. That way, you can safely include the required files without running into re-definition errors.
One more thing, session_start() should be called before anything is sent to the browser. Place it at the very top of your scripts for safety.
For example
bootstrap.php
<?php
// for development only
ini_set('display_errors', 'On');
error_reporting(E_ALL);
session_start();
require_once 'functions.php';
functions.php
<?php
function validate_user()
{
//validate user
}
header.php
<h1>...
user_class.php
<?php
require_once 'functions.php';
class User { ...
some_page.php
<?php
require_once 'bootstrap.php';
?>
<!DOCTYPE html>
<html>
<head>...</head>
<body>
<?php include 'header.php' ?>
</body>
</html>
I have a main.php file which has the following three included php files:
<!--TAB 1--->
<?php include "tab1.php"; ?>
<!--TAB 2--->
<?php include "tab2.php"; ?>
<!--TAB 3--->
<?php include "tab3.php"; ?>
When I open main.php in Dreamweaver, I have access to these 3 included files. My question is: What about files that are included inside one of these include files?
For example, the tab2.php file reference these php files:
<?php include "a.php"; ?>
<?php include "b.php"; ?>
<?php include "c.php"; ?>
In order to be able to edit these included files which are inside another include file, what needs to be done? Will I need to open those directly?
What I would like to be able to do is open main.php and then be able to add all 6 included files:
tab1.php
tab2.php
tab3.php
a.php
b.php
c.php
It seems as though I only have direct access to the first-level include files. Any suggestions? Thanks.
If you're using Dreamweaver CS5, it has an (default enabled) option to show includes in a line below the file tabs. If you have an include that has other includes, the only way of editting this includes directly from this file is to press CTRL+D in the include, then Dreamweaver will show a popup for you to select the includes to edit and it will open the selected one in a tab.
<html>
<head>
<!--#include virtual="header.php" -->
</head>
<body>
aa
</body>
</html>
I'm trying to put the header in a different file and call it with an include. But I can't make it work. Right now the only text in the header is the word header. The header is at: www.chusmix.com/game/header.php how can I call it? I don't care if I use include virtual, I just don't know how to do it.
Thanks
<?php include('header.php'); ?>
Your include needs to be in PHP -- the way you attempted to include it is a very old method of SSI (sever side includes) and has nothing to do with PHP.
In order to include this way, the page doing the including must be parsed as PHP, and on most servers that means ending the filename with a .php extension. You can have plain HTML in a .php file so you can just rename the file (if its not already) to .php and then include the line I wrote above.
include('header.php')
include_once('header.php')
require('header.php')
require_once('header.php')
take your pick ;)
To clarify: include and require differ only as much, that if file does not exist, include will only display warning, while require will throw fatal error.
The *_once functions on the other hands make sure file is included only once. Try including the same file two times, you'll get an error. *_once makes sure the 'including' is done only once.
On the other hand, *_once functions bring you a bit of overhead, but that should be taken in account when writing bigger applications.
You need to include it in the php code:
include (header.php);