Viewed   185 times

In PHP:

  • When should I use require vs. include?
  • When should I use require_once vs. include_once?

 Answers

4

There are require and include_once as well.

So your question should be...

  1. When should I use require vs. include?
  2. When should I use require_once vs. require

The answer to 1 is described here.

The require() function is identical to include(), except that it handles errors differently. If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop.

The answer to 2 can be found here.

The require_once() statement is identical to require() except PHP will check if the file has already been included, and if so, not include (require) it again.

Tuesday, September 27, 2022
2

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;
Saturday, November 5, 2022
 
kekoa
 
1

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");
Monday, October 10, 2022
 
3

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;
};
  1. Node.JS looks to see if the given module is a core module. (e.g. http, fs, etc.) Always takes the precedence in the loading modules.
  2. If the given module is not a core module (e.g. http, fs, etc.), Node.js will then begin to search for a directory named, node_modules.
    It will start in the current directory (relative to the currently-executing file in Node.JS) and then work its way up the folder hierarchy, checking each level for a node_modules folder. Once Node.JS finds the 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 example
  3. If you make a request to load the module, "utils" and its a directory not a .js file then:
    Node.JS will search a hierarchical directory for node_modules and utils in the following ways:
    ./node_modules/utils.js
    ./node_modules/utils/index.js
    ./node_modules/utils/package.json
  4. If Node.JS still can't find the file in above steps, Node.js will then start to look into the directory paths from environment variables i.e. 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 stder
    E.g.: Error:Cannot find module 'yourfile'
    For more information: link is here even the cyclic require() is explained very well.
Thursday, August 18, 2022
 
rghome
 
1

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.

Why would you use `filename.h` ?

  • Compatibility with C compilers
  • Compatibility with very old C++ compilers

Why should you use `cfilename` ?

  • Names are within namespace std:: . No name-clashes anymore.
  • New C++ features (e.g. overloaded math functions for float, long)
  • C Compatibility Headers (filename.h) could disappear in future.
Tuesday, December 6, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :