Questions tagged [require-once]

The PHP require_once function

The PHP require_once function.

In contrast to require this function only includes the referred file if it has not been included before.

Use this tag only for questions that directly ask about require_once usage. Do not use this tag if your code contains require_once but you have no reason to believe that require_once is directly related to the issue you are asking about.

409 questions
1424
votes
28 answers

Difference between require, include, require_once and include_once?

In PHP: When should I use require vs. include? When should I use require_once vs. include_once?
Scott B
  • 38,833
  • 65
  • 160
  • 266
146
votes
14 answers

Why is require_once so bad to use?

Everything I read about better PHP coding practices keeps saying don't use require_once because of speed. Why is this? What is the proper/better way to do the same thing as require_once? If it matters, I'm using PHP 5.
Uberfuzzy
  • 8,253
  • 11
  • 42
  • 50
119
votes
4 answers

relative path in require_once doesn't work

I have the following structure otsg > class > authentication.php > database.php > user.php > include > config.inc.php > encryption.php > include.php > session.php > index.php > registration.php include.php file has the…
user525146
  • 3,918
  • 14
  • 60
  • 103
39
votes
5 answers

Is it better to use require_once('filename.php') or require_once 'filename.php';

Is this just a stylistic difference, or does using require_once('filename.php') vs require_once 'filename.php' have actual load/efficiency differences?
ina
  • 19,167
  • 39
  • 122
  • 201
38
votes
1 answer

Using require_once for up directory not working

I am using require_once like this require_once('../mycode.php') I am developing a wordpress plugin. My plugin folder is yves-slider where I have a file called yves-slider.php and a folder called admin. Inside admin folder I have a file called…
Yves Gonzaga
  • 1,038
  • 1
  • 16
  • 40
20
votes
1 answer

Why I cannot get the return value of require_once function in PHP?

I already know that include_once would return true or false based on including that file. I've read a question on Stackoverflow about using require_once to return your value and print it out. The problem is that I have an existing project in hand,…
Alireza
  • 6,497
  • 13
  • 59
  • 132
20
votes
5 answers

How does include path resolution work in require_once?

I was writing an web app in PHP, when I encountered a strange situation. To illustrate my problem, consider a web app of this structure: / index.php f1/ f1.php f2/ f2.php Contents of these files: index.php:
jrharshath
  • 25,975
  • 33
  • 97
  • 127
16
votes
5 answers

PHP require_once error when including file in parent folder

I have the following file structure -- Plugins -- inParent.php ---- Uploadify ------ inSame.php ------ Uploadify.php This function was working smoothly till yesterday. But now, If I try to include the inParent.php in uploadify.php using…
Sharad Saxena
  • 209
  • 1
  • 2
  • 9
15
votes
3 answers

Why is require_once echoing entire file contents?

I have a class in a file "evalmath.php". If I require it like this: require_once('evalmath.php'); the entire contents of that file is echoed out to the screen. If I do it like this, require_once( 'evalmath.php' );, it doesn't. HUH? EDIT - SOURCE…
richard
  • 12,263
  • 23
  • 95
  • 151
14
votes
4 answers

require_once to global scope within a function

It seems that if require_once is called within function, the included file doesn't extend the global variable scope. How to require_once a file to global scope from within a function? What I'm trying to do is some dynamic module loader: function…
Tomas
  • 57,621
  • 49
  • 238
  • 373
14
votes
4 answers

What is the scope of require_once in PHP?

Simple question: Is the scope of require_once global? For example: When foo is called, does it re-parse…
TMG
  • 500
  • 2
  • 6
  • 13
12
votes
7 answers

Is there a way to set the scope of require_once() explicitly to global?

I'm looking for a way to set the scope of require_once() to the global scope, when require_once() is used inside a function. Something like the following code should work: file `foo.php':
Stephan Kulla
  • 4,739
  • 3
  • 26
  • 35
11
votes
3 answers

Eclipse Php : variable undefined evenif it is defined in an require_once php file

I am using as a beginner : Eclipse IDE for PHP Developers Version: Photon Release (4.8.0) Build id: 20180619-1200 OS: Windows 10, v.10.0, x86_64 / win32 Java version: 1.8.0_77 I have a file index.php with a require_once(initialisation.php) The…
9
votes
3 answers

Use Require_once() to include database connection variables correctly

I'm a php newbie (but long time developer in other languages) and I'm trying some example db connections in "PHP, MySQL, & JavaScript". It shows an example file to include db connection variables (servername, username, password, database, etc.). I…
Jeff LaFay
  • 12,882
  • 13
  • 71
  • 101
9
votes
1 answer

Fatal error: require_once(): Failed opening required

I'm trying to run my online site on my local machine. I stumbled upon a problem. This is my htaccess AddDefaultCharset UTF-8 Options -Indexes #php_value include_path .:/home/sites/www.example.it/example.it/htdocs/conf/ php_value include_path…
Massimo Variolo
  • 4,669
  • 6
  • 38
  • 64
1
2 3
27 28