In PHP:
- When should I use
require
vs.include
? - When should I use
require_once
vs.include_once
?
In PHP:
require
vs. include
?require_once
vs. include_once
?Just make sure you call require() after setting the variables, and they should be available in b.php.
a.php:
$a = 'foo';
$b = 'baz';
require('b.php');
b.php:
echo 'a: '. $a;
echo 'b: '. $b;
You need to add the after the directory name:
include(__DIR__ . "\..\another_folder\file_2.php");
This will make the path be
C:xampphtdocsmain_folder..another_folderfile_2.php
instead of
C:xampphtdocsmain_folder..another_folderfile_2.php
Also, for portability, it is advisable to use /
instead of , which works on all platforms, including Windows:
include(__DIR__ . "/../another_folder/file_2.php");
Here is the answer:
Module.prototype.load = function(filename) {
debug('load ' + JSON.stringify(filename) +
' for module ' + JSON.stringify(this.id));
assert(!this.loaded);
this.filename = filename;
this.paths = Module._nodeModulePaths(path.dirname(filename));
var extension = path.extname(filename) || '.js';
if (!Module._extensions[extension]) extension = '.js';
Module._extensions[extension](this, filename);
this.loaded = true;
};
http
, fs
, etc.)
Always takes the precedence in the loading modules.http
, fs
, etc.), Node.js will then begin to search for a directory named, node_modules
. node_modules
folder, it will then attempt to load the given module either as a (.js) JavaScript file or as a named sub-directory; if it finds the named sub-directory, it will then attempt to load the file in various ways. So, for examplenode_modules
and
utils
in the following ways:
./node_modules/utils.js
./node_modules/utils/index.js
./node_modules/utils/package.json
NODE_PATH
set on your machine(obviously set by Node.JS installer file if you are on windows)
Not Found in all the above steps then, prints a stack trace to stderError:
Cannot find module 'yourfile'
C++ only include-files not found in the C standard never used filename.h
. Since the very first C++ Standard came out (1998) they have used filename
for their own headers.
Files inherited by the C Standard became cfilename
instead of filename.h
. The C files inherited used like filename.h
are deprecated, but still part of the C++ standard.
The difference is that names not defined as macros in C are found within namespace std::
in cfilename
in C++, while names in filename.h
are within the global namespace scope. So you will find ::size_t
in stddef.h, and std::size_t
in cstddef. Both are Standard C++, but use of ::size_t is deprecated (See Annex D of the C++ Standard).
Now those were the difference.
std::
. No name-clashes anymore. filename.h
) could disappear in future.
There are
require
andinclude_once
as well.So your question should be...
require
vs.include
?require_once
vs.require
The answer to 1 is described here.
The answer to 2 can be found here.