473,385 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

include_once() and require_once() in function

Is I can use include_once() or require_once() in function or only include()?
Sep 29 '08 #1
4 7747

Andrew G. Koptyaev schreef:
Is I can use include_once() or require_once() in function or only include()?

Hi Andrew,

You can use all 3 mentioned.
Why don't you simply try and see?

Regards,
Erwin Moller

--
============================
Erwin Moller
Now dropping all postings from googlegroups.
Why? http://improve-usenet.org/
============================
Sep 29 '08 #2
r0g
Andrew G. Koptyaev wrote:
Is I can use include_once() or require_once() in function or only include()?

I tend to use require_once for most things.

The 'require' part causes your script to stop executing with a fatal
error if it can't include your file, which is generally what you would want.

The 'once' part means if a script has already been included further back
in time it won't be included again (as this could cause all manner of
problems).
Roger Heathcote.
Sep 29 '08 #3
Andrew G. Koptyaev wrote:
Is I can use include_once() or require_once() in function or only include()?

If you use include_once the file will only be included the first time
the function is called, if you use include it will be included each time
the function is called. (require and require_once follow the same
principle obviously.)

You need to consider what is in the file you are including, and what
scope it should have. For example:

If the file you include sets some variables and you use include_once,
the second time you call the function the file will not be included
again, though the variables won't be already defined because their scope
was only within the function.

However if the file you include defines a function and you use include,
the second time you call the function you will get an error trying to
redefine the function since it has global scope.
Sep 29 '08 #4
Jamie Furness wrote:
Andrew G. Koptyaev wrote:
>Is I can use include_once() or require_once() in function or only
include()?

If you use include_once the file will only be included the first time
the function is called, if you use include it will be included each time
the function is called. (require and require_once follow the same
principle obviously.)

You need to consider what is in the file you are including, and what
scope it should have. For example:

If the file you include sets some variables and you use include_once,
the second time you call the function the file will not be included
again, though the variables won't be already defined because their scope
was only within the function.

However if the file you include defines a function and you use include,
the second time you call the function you will get an error trying to
redefine the function since it has global scope.
Generally, the only times I use require or include as opposed to
require_once or include_once, is when I specifically want that include
to appear more than once in the script. For example, I might have files
with month, day, and year. If, on my form I have start date and end
date, then I would be using each of these twice.

Otherwise, I use the _once form to avoid things like double declarations.
Oct 1 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: the wonderer | last post by:
This is an elementary question, but I've not been able to find the answer, so here goes: I am developing a site using php. I have the html header information in a file that I include in all the...
6
by: Nik Coughin | last post by:
Say I have a script: http://www.mydomain.com/test/admin/foo.php That looks like this: <?php $baseDir = "http://" . $_SERVER . "/test"; require_once( $baseDir . '/helpers/helper.php' );
4
by: Kevin Newman | last post by:
The primary problem I've had with php is the lack of namespaces, which makes OOP very difficult to organize, since you end up with large number of classes cluttering up the same namespace - which...
11
by: Kimmo Laine | last post by:
I'm flipping my wig here, people. I'm using classes and making each class a file. when I'm including dependet classess, I use require_once to avoid multiple declarations - yet they happen. I put...
6
by: Shelly | last post by:
Here is a crazy question that has happend to me once before. I have an include file for the connection information to the server. It is like this: $hostname= "the_server_location"; $database...
15
by: Mikhail Kovalev | last post by:
Hi all, I have a file which is to be included in another script and which takes several seconds to load(!), and which is actually not always used by the script, so instead of using include or...
6
by: Royan | last post by:
Ok the problem is quite hard to explain, but i'll try to keep it as simple as i can. Imagine I have the following structure of my files and folders: /root/global.inc |__/files/foo.php...
7
by: Ronald Raygun | last post by:
I have been struggling with this all afternoon and I'm, well lats just say, very pissed off ... I have a require once in a file. I am passing the fully qualified (i.e. absolute pathname) to a...
1
code green
by: code green | last post by:
I use the line require_once '../includeFiles.php'; to pull in all required functions etc for my scripts. It is also possible that a script will include another, both with this line in, but the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.