Any difference between require() in PHP and include in C?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kknight
Forum Newbie
Posts: 4
Joined: Tue Nov 15, 2005 7:45 pm
Location: CA, USA

Any difference between require() in PHP and include in C?

Post by kknight »

Are they doing the same thing?
Is require() in PHP equal to expanding the required PHP script at the require() location?
Thanks.
RobertPaul
Forum Contributor
Posts: 122
Joined: Sun Sep 18, 2005 8:54 pm
Location: OCNY

Post by RobertPaul »

From TFM:
require() and include() are identical in every way except how they handle failure. include() produces a Warning while require() results in a Fatal Error.
User avatar
trukfixer
Forum Contributor
Posts: 174
Joined: Fri May 21, 2004 3:14 pm
Location: Miami, Florida, USA

Post by trukfixer »

pretty much, yes..

require()
require_once() (better)
include()
include_once() (my preference)

All do the same thing in php , basically "insert the contents of teh file in this point in the script"

require and require_once will spit out a fatal error and kill script execution if teh file cannot be read or is missing, while include and include_once only issue a warning but continue with the rest of script execution ..

I prefer the use of include_once myself, as it will prevent re-loading an already loaded or included file.. (it happens sometimes in larger scripts)
kknight
Forum Newbie
Posts: 4
Joined: Tue Nov 15, 2005 7:45 pm
Location: CA, USA

Post by kknight »

Thanks for your reply.
But, I think require() is not like "insert the contents of the file in this point in the script".
For example, there are two files: a.php and b.php

a.php has the following content:
<?
require 'b.php';
?>

b.php has the following content:
<?
$a=1;
?>

If we only treat require() as "insert the contents of the file in this point in the script".
The resulting a.php will be something like this:
<?
<?
$a=1;
?>
?>

Is this a valid php script?
In this sense, require() is not the same as the #include in C language. Any thoughts?
trukfixer wrote:pretty much, yes..

require()
require_once() (better)
include()
include_once() (my preference)

All do the same thing in php , basically "insert the contents of teh file in this point in the script"

require and require_once will spit out a fatal error and kill script execution if teh file cannot be read or is missing, while include and include_once only issue a warning but continue with the rest of script execution ..

I prefer the use of include_once myself, as it will prevent re-loading an already loaded or included file.. (it happens sometimes in larger scripts)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

#include <file> //OK less so with the <>
and
#include "file"

are *similar* to include() require() etc in php yes but the behaviour... as you've noticed, is not exactly the same.

PHP will parse the contents of the file it is including during script execution.... rather than simply place the contents inline.

It's simply a way to reuse code and re-factor into smaller parts.

Each include(), include_once(), require() and require_once() have their own little diffrerences too.
Post Reply