RE: [PHP] Require() vs Include() - - c o n c l u s i o n

From: Date: Tue, 14 Nov 2000 08:28:57 +0000
Subject: RE: [PHP] Require() vs Include() - - c o n c l u s i o n
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-25156@lists.php.net to get a copy of this message
At 07:53 14-11-00, Maxim Maletsky wrote:
I feel bad guys .. Sorry, I started that thread while writing a file that would include a class under any circumstances and I remembered that I never use require() so I wanted to ask you all your opinions on should I start using it or not, is there any probs or not .. I knew the functionality issues but not the performance and usability (religious) ones... my *personal* conclusion is: Include() is better for many reasons one of which is it's flexibility. basically there's nothing to fear if you use include() at any time instead require();
I'm not sure why you say that, it's kind of like saying echo and printf() are exactly the same because they both print out information. I'll summarize the differences between include() and require() in the various versions of PHP. The shortest summary is that they have a semantic difference, and in old versions of PHP, require() used to be considerably faster than include() in some circumstances. 1. Today, as of PHP 4.0.2 and later, require() can be described, in PHP psuedo-code as: if (include($filename)==FAILURE) {
        print "Fatal error:  Unable to require $filename";
        exit();
} That's what Andi meant when he said it's not allowed to fail. Obviously, it means that if it fails, the script ends prematurely. 2. *NOTE* this point is no longer relevant if you're using PHP 4.0.3 or later In PHP 3.0.x, and PHP 4.0 versions prior to PHP 4.0.3, require() had another implementation difference - it was always, under all circumstances, performed exactly once. For instance: for ($i=0; $i<100; $i++) { require("foo.php"); } would have been converted to for ($i=0; $i<100; $i++) { ...contents of foo.php... } and then executed, getting the contents of foo.php iterated a hundred times. Another example: if (false) { require("foo.php"); } would *still* read the contents of foo.php, turning it to if (false) { ...contents of foo.php... } and only then PHP would figure out that it doesn't even have to execute this code. As of PHP 4.0.3, the implementation of require() no longer behaves that way, and processes the file 'just in time'. That means that in the 1st example, the file will be processed a hundred times, and in the 2nd example, it won't be processed at all. That's the way include() behaves (in all versions of PHP) as well. Other than that, include() and require() have no difference whatsoever, they both behave the same with URLs, safe mode, and anything you can think of. So, it still makes perfect sense to use require() when it'll make no sense for your script to continue if the file is not found, which is the case, more often than not. Zeev -- Zeev Suraski <zeev@zend.com> CTO, Zend Technologies Ltd. http://www.zend.com/

« previous php.general (#25156) next »