Include, Include_Once, Require or Require_Once

Difference between require, include, require_once and include_once?

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.

include, include_once, require or require_once?

The only difference between the two is that require and its sister require_once throw a fatal error if the file is not found, whereas include and include_once only show a warning and continue to load the rest of the page. If you don't want PHP to attempt to load the rest of your page without the database info (which I would assume), then use require_once. You don't need to include the file more than once, so there is no need to use the regular require function.

What is the best way to randomize an array's order in PHP without using the shuffle() function?

You could use the Fisher-Yates shuffle.

What is difference between include_once and require_once in PHP?

Include will let the script keep running (with a warning) if the file is missing.

Require will crash if it's missing.

If you're using include for things which are 100% optional, then include will still work and require will explode.

If you include something that you think is optional, but some other piece of your script uses it, way down the line, then your script will explode there, and you'll probably have no idea why.

This is not a good way to write programs.

Otherwise, there isn't a difference between the two.

edit

In typical usage, it shouldn't matter whether you choose to use include or require, 95% of the time.

As such, you should stick to require (or require_once), to tell you when you're missing a file you need.

The bugs that come from including files inside of included files, inside of included files, when one include up top is missing, are really hard to track down.

Some people prefer include, because they want to use that "feature".

Douglas Crockford is a JavaScript/Java/C++ guy, rather than PHP, but he suggests that features that look like bugs, or side effects which are indistinguishable from bugs, should be avoided for your sanity.

Mind you, if your entire project is completely class-based (or functional), and entirely modular, then you shouldn't have much use for include, aside from, say resetting values on a configuration-object at application initialization (including admin-override options or dev-debugging options) on an object that was already required.

As you might guess, there are other ways of doing this.

And these are just suggestions, but suggestions that come from people who are smarter than I am, with decades of experience in dozens of languages.

Visual Studio 2005 Setup project install crashes over Terminal Server

I had LOTS of issues with developing installers (and software in general) for terminal server. I hate that damn thing.

Anyway, VS Setup Projects are just .msi files, and run using the Windows installer framework.

This will drop a log file when it errors out, they're called MSIc183.LOG (swap the c183 for some random numbers and letters), and they go in your logged-in-user account's temp directory.

The easiest way to find that is to type %TEMP% into the windows explorer address bar - once you're there have a look for these log files, they might give you a clue.

  • Note - Under terminal server, sometimes the logs don't go directly into %TEMP%, but under numbered subdirectories. If you can't find any MSIXYZ.LOG files in there, look for directories called 1, 2, and so on, and look in those.

If you find a log file, but can't get any clues from it, post it here. I've looked at more than I care to thing about, so I may be able to help

What is the difference between require and require_once in PHP?

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.

Is it possible to write code to write code?

Start by looking at quines, then at Macro-Assemblers and then lex & yacc, and flex & bison. Then consider self-modifying code.

Here's a quine (formatted, use the output as the new input):

#include<stdio.h>

main()
{
char *a = "main(){char *a = %c%s%c; int b = '%c'; printf(a,b,a,b,b);}";
int b = '"';
printf(a,b,a,b,b);
}

Now if you're just looking for things programmers can't do look for the opposite of np-complete.

What is the difference between PHP require and include?

  • include includes a file and throws a warning if the file was not found.

  • require includes a file and throws a fatal error if the file was not found.

  • include_once and require_once do the same thing, but only if the file was not already loaded.

However, the need for one of the _once functions is usually a sign of bad design. You should build your scripts in a way that clearly defines what gets included where.

Choose one place for settings.php to get included - probably index.php. There should be no need to additionally include it in database.php.



Related Topics



Leave a reply



Submit