[PHP] Require() vs Include()

165 views
Skip to first unread message

Maxim Maletsky

unread,
Nov 13, 2000, 11:19:51 PM11/13/00
to
Hi,
A tiny question:

why to use require() when include() works just as well and even better when
there are some conditions like if{} ?

I ask it because although I read many articles on this issue I found no
reason for myself to use require() instead of include() and I have driven
some huge projects in PHP.

Why do I see these hardcore PHP gurus writing PHPLIB, mySQLadmin and
examples in their articles using require() where include() would fit just as
perfect?

WHERE THE TRIIIICK?

It escapes from me ...

Thanks.

Maxim Maletsky - ma...@j-door.com <mailto:ma...@j-door.com>
Webmaster, J-Door.com / J@pan Inc.
LINC Media, Inc.
TEL: 03-3499-2175 x 1271
FAX: 03-3499-3109

http://www.j-door.com <http://www.j-door.com/>
http://www.japaninc.net <http://www.japaninc.net/>
http://www.lincmedia.co.jp <http://www.lincmedia.co.jp/>


Lawrenc...@dfait-maeci.gc.ca

unread,
Nov 13, 2000, 11:38:41 PM11/13/00
to
Ok,

Require will always add in the file even if its not executed.


Eg

if (isset($includeme)) {
require ("xxx.php");
}

require will be included in the file parsed by php

but the following will not get included in the file if $includeme is not
set.

if (isset($includeme)) {
include ("xxx.php");
}

An important note about how this works is that when a file is include()ed or
require()ed, parsing drops out of PHP mode and into HTML mode at the
beginning of the target file, and resumes PHP mode again at the end. For
this reason, any code inside the target file which should be executed as PHP
code must be enclosed within valid PHP start and end tags.

Unlike include(), require() will always read in the target file, even if the
line it's on never executes. If you want to conditionally include a file,
use include(). The conditional statement won't affect the require().
However, if the line on which the require() occurs is not executed, neither
will any of the code in the target file be executed.


The manual is actually pretty good on this topic imho.

http://www.php.net/manual/function.require.php


-----Original Message-----
From: Maxim Maletsky [mailto:maxim.m...@japaninc.net]
Sent: November 14, 2000 12:34 PM
To: 'Lawrenc...@dfait-maeci.gc.ca'
Cc: 'PHP General List. (E-mail)'
Subject: RE: [PHP] Require() vs Include()


Well,
with include() if an included file does not exists it WILL fail.

so where's the difference?

-----Original Message-----
From: Lawrenc...@dfait-maeci.gc.ca
[mailto:Lawrenc...@dfait-maeci.gc.ca]
Sent: Tuesday, November 14, 2000 1:26 PM
To: Maxim Maletsky
Subject: RE: [PHP] Require() vs Include()


include won't fail out if the includes not there, but require will.

If you have something that *has* to be in there, then require is used, if
something could be optional - eg a generated header or something than might
be or might not be there at a given time, use include.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-general...@lists.php.net
For additional commands, e-mail: php-gene...@lists.php.net
To contact the list administrators, e-mail: php-lis...@lists.php.net

Maxim Maletsky

unread,
Nov 13, 2000, 11:32:12 PM11/13/00
to

Mark Peoples

unread,
Nov 13, 2000, 11:58:57 PM11/13/00
to
Hi Maxim,
Suppose you have this:

--file1.php--
<?php print("\nin file1.php"); ?>

--file2.php--
<?php print("\nin file2.php"); ?>


Now, suppose you want to include certain files, based on conditions:

--test.php--
if($myCatVioletHasBlackFur == "no")
require(file1.php);
if($myCatVioletHasBlackFur == "yes")
include(file2.php);

Well, my cat, Violet <g>, has black fur, so the 2nd condition is valid, the
1st is false. However, require() is executed regardless, so, the output of
test.php is:

in file1.php
in file2.php


So, what if I change test.php to:

--test.php--
if($myCatVioletHasBlackFur == "no")
include(file1.php);
if($myCatVioletHasBlackFur == "yes")
include(file2.php);


Well, Violet still has black fur, so, the output is:

in file2.php


Hope this helps. =)


marco


-----Original Message-----
From: Maxim Maletsky [mailto:maxim.m...@japaninc.net]
Sent: Monday, November 13, 2000 9:21 PM
To: 'PHP General List. (E-mail)'
Subject: [PHP] Require() vs Include()


Hi,
A tiny question:

WHERE THE TRIIIICK?

Thanks.

Joe Stump

unread,
Nov 13, 2000, 11:52:33 PM11/13/00
to
I haven't the foggiest - I've pondered configuring without it.

--Joe

On Tue, Nov 14, 2000 at 01:52:19PM +0900, Maxim Maletsky wrote:
> OK, then.
>
> what is the reason require() exists?
>
>
> If it works the exactly same way as include() except that include can be
> more flexible why would we use require() then?
>
> Any performance issues?
> usability?
> security?
>
> Thanks Lawrence,


>
>
> -----Original Message-----
> From: Lawrenc...@dfait-maeci.gc.ca
> [mailto:Lawrenc...@dfait-maeci.gc.ca]
> Sent: Tuesday, November 14, 2000 1:37 PM
> To: Maxim Maletsky; Lawrenc...@dfait-maeci.gc.ca
> Cc: php-g...@lists.php.net
> Subject: RE: [PHP] Require() vs Include()
>
>

> Ok,
>
> Require will always add in the file even if its not executed.
>
>
> Eg
>
> if (isset($includeme)) {
> require ("xxx.php");
> }
>
> require will be included in the file parsed by php
>
> but the following will not get included in the file if $includeme is not
> set.
>
> if (isset($includeme)) {
> include ("xxx.php");
> }
>
> An important note about how this works is that when a file is include()ed or
> require()ed, parsing drops out of PHP mode and into HTML mode at the
> beginning of the target file, and resumes PHP mode again at the end. For
> this reason, any code inside the target file which should be executed as PHP
> code must be enclosed within valid PHP start and end tags.
>
> Unlike include(), require() will always read in the target file, even if the
> line it's on never executes. If you want to conditionally include a file,
> use include(). The conditional statement won't affect the require().
> However, if the line on which the require() occurs is not executed, neither
> will any of the code in the target file be executed.
>
>
> The manual is actually pretty good on this topic imho.
>
> http://www.php.net/manual/function.require.php
>
>

> -----Original Message-----
> From: Maxim Maletsky [mailto:maxim.m...@japaninc.net]

> Sent: November 14, 2000 12:34 PM
> To: 'Lawrenc...@dfait-maeci.gc.ca'

> Cc: 'PHP General List. (E-mail)'
> Subject: RE: [PHP] Require() vs Include()
>
>
> Well,
> with include() if an included file does not exists it WILL fail.
>
> so where's the difference?
>
> -----Original Message-----
> From: Lawrenc...@dfait-maeci.gc.ca
> [mailto:Lawrenc...@dfait-maeci.gc.ca]
> Sent: Tuesday, November 14, 2000 1:26 PM
> To: Maxim Maletsky
> Subject: RE: [PHP] Require() vs Include()
>
>
> include won't fail out if the includes not there, but require will.
>
> If you have something that *has* to be in there, then require is used, if
> something could be optional - eg a generated header or something than might
> be or might not be there at a given time, use include.
>
>
>

================================================================================
= Joe Stump joest...@yahoo.com http://www.miester.org =
================================================================================
"Real programmers don't comment their code.
It was hard to write, it should be hard to understand."

Joe Stump

unread,
Nov 13, 2000, 11:42:02 PM11/13/00
to
My experience with require() was an absolute nightmare! AFAIK there isn't any
real performance increase between require() and include() and includes are a lot
more flexible in that they can be used as illustrated below without any
headaches.

My tip: stay the hell away from require()!

--Joe

================================================================================
= Joe Stump joest...@yahoo.com http://www.miester.org =
================================================================================
"Real programmers don't comment their code.
It was hard to write, it should be hard to understand."

--

Maxim Maletsky

unread,
Nov 13, 2000, 11:50:44 PM11/13/00
to
OK, then.

what is the reason require() exists?


If it works the exactly same way as include() except that include can be
more flexible why would we use require() then?

Any performance issues?
usability?
security?

Thanks Lawrence,


-----Original Message-----
From: Lawrenc...@dfait-maeci.gc.ca
[mailto:Lawrenc...@dfait-maeci.gc.ca]

Sent: Tuesday, November 14, 2000 1:37 PM
To: Maxim Maletsky; Lawrenc...@dfait-maeci.gc.ca
Cc: php-g...@lists.php.net

Maxim Maletsky

unread,
Nov 14, 2000, 12:11:27 AM11/14/00
to
Does that mean that we can freely write to the whole mailing list (the whole
world)


DO NEVER USE REQUIRE() SINCE THERE'S INCLUDE() WHICH IS 100.00% BETTER


?


I mean, in something require() is still being good, isn't it?

Cheers,
Maxim Maletsky

-----Original Message-----
From: Lars Torben Wilson [mailto:tor...@php.net]
Sent: Tuesday, November 14, 2000 3:06 PM
To: Maxim Maletsky
Cc: Lawrenc...@dfait-maeci.gc.ca; php-g...@lists.php.net
Subject: RE: [PHP] Require() vs Include()


Maxim Maletsky writes:
> OK, then.
>
> what is the reason require() exists?
>
>
> If it works the exactly same way as include() except that include can be
> more flexible why would we use require() then?
>
> Any performance issues?
> usability?
> security?
>
> Thanks Lawrence,

In PHP 3, it was quite a bit faster than include(). There were other
semantic differences between the two as well, such as return values
etc. Performance appears to now be pretty much the same; you can
return values with include() now, and so forth and so on.

BTW, require() will fail in a completely different way from include()
when it can't find the target file. include() will simply return a
false value, whereas require() will spit out a great big 'Fatal error'
and halt script execution.


--
+----------------------------------------------------------------+
|Torben Wilson <tor...@php.net> Netmill iTech|
|http://www.coastnet.com/~torben http://www.netmill.fi|
|Ph: 1 250 383-9735 tor...@netmill.fi|
+----------------------------------------------------------------+

Lars Torben Wilson

unread,
Nov 14, 2000, 12:07:21 AM11/14/00
to
Maxim Maletsky writes:
> OK, then.
>
> what is the reason require() exists?
>
>
> If it works the exactly same way as include() except that include can be
> more flexible why would we use require() then?
>
> Any performance issues?
> usability?
> security?
>
> Thanks Lawrence,

In PHP 3, it was quite a bit faster than include(). There were other


semantic differences between the two as well, such as return values
etc. Performance appears to now be pretty much the same; you can
return values with include() now, and so forth and so on.

BTW, require() will fail in a completely different way from include()
when it can't find the target file. include() will simply return a
false value, whereas require() will spit out a great big 'Fatal error'
and halt script execution.


--
+----------------------------------------------------------------+
|Torben Wilson <tor...@php.net> Netmill iTech|
|http://www.coastnet.com/~torben http://www.netmill.fi|
|Ph: 1 250 383-9735 tor...@netmill.fi|
+----------------------------------------------------------------+

--

Lawrenc...@dfait-maeci.gc.ca

unread,
Nov 14, 2000, 12:08:27 AM11/14/00
to
Just to be pedantic ;)


From the manual:

Because include() is a special language construct, you must enclose it
within a statement block if it is inside a conditional block

So

if ($x==$y) {
require ("xx.php");
}
else {
include ("yy.php");
}

BTW your output should still be correct, because the code in the require
isn't being run - it is however being added in before parsing by the php
preprocessor.

The include however, will not be added in if it doesn't get run.

Require is a throwback to #include from C days.

cheers,

Lawrence.

-----Original Message-----
From: Mark Peoples [mailto:gas...@gascairlines.com]
Sent: November 14, 2000 12:56 PM
To: 'PHP General List. (E-mail)'

Subject: RE: [PHP] Require() vs Include()

in file1.php
in file2.php

in file2.php


Hope this helps. =)


marco


Hi,
A tiny question:

WHERE THE TRIIIICK?

Thanks.

Joe Stump

unread,
Nov 14, 2000, 12:10:31 AM11/14/00
to
> My problem is that I feel a little disappointed by never using require()
> since it might be that I am missing something.

Don't be - trust me you aren't missing anything. We had a guy at work who used
require() religiously because he said it was "slightly faster" than include().
In the end it just killed our code because once you start programming under a
set of guidelines and you break that it breaks everything. We were having
horrible problems with it.

Like I said before - it's not something that is worth using (unless you really
like those migraines)

--Joe


>
> Do I?

================================================================================
= Joe Stump joest...@yahoo.com http://www.miester.org =
================================================================================
"Real programmers don't comment their code.
It was hard to write, it should be hard to understand."

--

Maxim Maletsky

unread,
Nov 14, 2000, 12:05:25 AM11/14/00
to
Thanks Mark, I understood this part (do you really have that kind of cat ???
)

my question now is WHY to use require() if include() is the same thing yet
more flexible?

Does require() parse the file any faster?
what is a cool example you could give me using require() whre include()
wouldn't be that good?

My problem is that I feel a little disappointed by never using require()
since it might be that I am missing something.

Do I?

Lars Torben Wilson

unread,
Nov 14, 2000, 12:16:19 AM11/14/00
to
Mark Peoples writes:
> Hi Maxim,
> Suppose you have this:
>
> --file1.php--
> <?php print("\nin file1.php"); ?>
>
> --file2.php--
> <?php print("\nin file2.php"); ?>
>
>
> Now, suppose you want to include certain files, based on conditions:
>
> --test.php--
> if($myCatVioletHasBlackFur == "no")
> require(file1.php);
> if($myCatVioletHasBlackFur == "yes")
> include(file2.php);
>
> Well, my cat, Violet <g>, has black fur, so the 2nd condition is valid, the
> 1st is false. However, require() is executed regardless, so, the output of
> test.php is:
>
> in file1.php
> in file2.php

No offense, but this is incorrect. The above would do what you say,
but only in PHP 3, and not for the reason you think.

From the manual (http://www.php.net/manual/function.include.php):

Because include() is a special language construct, you must
enclose it within a statement block if it is inside a conditional

block.

/* This is WRONG and will not work as desired. */

if ($condition)
include($file);
else
include($other);

/* This is CORRECT. */

if ($condition) {
include($file);
} else {
include($other);
}

(Yes, it's about include(), but also applies to require()).

In PHP 3, you *need* those { } there to get around the fact that
include() and require() are not functions. If you treat them like
functions, they'll just act weird, doing what you want one minute and
then turning and biting you the next.

In PHP 4, the above would give the result:

in file2.php

Even though the require() is executed, execution must still pass
through it for the require()d code to be executed. So if execution
never hits the inside of the if() block, although the file has been
require()d, its *contents* never get executed.

> So, what if I change test.php to:
>
> --test.php--
> if($myCatVioletHasBlackFur == "no")
> include(file1.php);
> if($myCatVioletHasBlackFur == "yes")
> include(file2.php);
>
>
> Well, Violet still has black fur, so, the output is:
>
> in file2.php
>
>
> Hope this helps. =)
>
>
> marco

--

+----------------------------------------------------------------+
|Torben Wilson <tor...@php.net> Netmill iTech|
|http://www.coastnet.com/~torben http://www.netmill.fi|
|Ph: 1 250 383-9735 tor...@netmill.fi|
+----------------------------------------------------------------+

--

Andi Gutmans

unread,
Nov 14, 2000, 12:14:23 AM11/14/00
to
In the latest versions of PHP 4.0 (I think since 4.0.2) require() and
include() behave exactly the same *except* that a require() is not allowed
to fail. So feel free to use any of these two, they are the same speed. The
implementation of require() used to be different but both for consistency
reasons and due to the fact that the old require() wasn't faster than the
include() as it was supposed to be we chose consistency.

Andi

At 09:55 PM 11/13/00 -0700, Mark Peoples wrote:
>Hi Maxim,
> Suppose you have this:
>
>--file1.php--
><?php print("\nin file1.php"); ?>
>
>--file2.php--
><?php print("\nin file2.php"); ?>
>
>
>Now, suppose you want to include certain files, based on conditions:
>
>--test.php--
>if($myCatVioletHasBlackFur == "no")
> require(file1.php);
>if($myCatVioletHasBlackFur == "yes")
> include(file2.php);
>
>Well, my cat, Violet <g>, has black fur, so the 2nd condition is valid, the
>1st is false. However, require() is executed regardless, so, the output of
>test.php is:
>
>in file1.php
>in file2.php
>
>

>So, what if I change test.php to:
>
>--test.php--
>if($myCatVioletHasBlackFur == "no")
> include(file1.php);
>if($myCatVioletHasBlackFur == "yes")
> include(file2.php);
>
>
>Well, Violet still has black fur, so, the output is:
>
>in file2.php
>
>
>Hope this helps. =)
>
>
>marco
>
>

>-----Original Message-----
>From: Maxim Maletsky [mailto:maxim.m...@japaninc.net]
>Sent: Monday, November 13, 2000 9:21 PM
>To: 'PHP General List. (E-mail)'
>Subject: [PHP] Require() vs Include()
>
>

>--
>PHP General Mailing List (http://www.php.net/)
>To unsubscribe, e-mail: php-general...@lists.php.net
>For additional commands, e-mail: php-gene...@lists.php.net
>To contact the list administrators, e-mail: php-lis...@lists.php.net

---
Andi Gutmans <an...@zend.com>
http://www.zend.com/

Mark Peoples

unread,
Nov 14, 2000, 12:15:45 AM11/14/00
to
I'll send you an e-mail with a link with an example...don't wanna hammer the
web-server. =)

As far as speed, I'm not really sure. I only use include() because most of
my stuff is conditional. either one work well for maintaining a 'theme' on a
site (eg, faking shared borders in front page).

require() and include() have a few differences (you can use remote files
with require(), not with include(); include()s are eval'd every time, etc
etc...there's a ton of good stuff in the manual).

As for feeling bad about it, don't worry about it. Knowing that you've
pondered this now, in the future, if you're having problems, you'll likely
know what the solution is. =)

Chris Doyle

unread,
Nov 14, 2000, 12:24:20 AM11/14/00
to
Generaly everyone i have know to use require(""); uses it to include
librarys/classes and such. stuff that you definately are going to need. It
is more stable i think.
CD
_______________________________

This email may contain confidential and/or privileged information for the
sole use of the intended recipient. Any review or distribution by others is
strictly prohibited. If you have received this email in error, please
contact the sender and delete all copies. Opinions, conclusions or other
information expressed or contained in this email are not given or endorsed
by the sender unless otherwise affirmed independently by the sender.

_________________________________

----- Original Message -----
From: "Lars Torben Wilson" <tor...@pinc.com>
To: "Maxim Maletsky" <maxim.m...@japaninc.net>
Cc: <Lawrenc...@dfait-maeci.gc.ca>; <php-g...@lists.php.net>
Sent: Tuesday, November 14, 2000 4:18 PM
Subject: RE: [PHP] Require() vs Include()


> Maxim Maletsky writes:
> > Does that mean that we can freely write to the whole mailing list (the
whole
> > world)
> >
> >
> > DO NEVER USE REQUIRE() SINCE THERE'S INCLUDE() WHICH IS 100.00% BETTER
>

> Why on Earth would you want to do something like that?


>
> > ?
> >
> >
> > I mean, in something require() is still being good, isn't it?
> >
> > Cheers,
> > Maxim Maletsky
>

> If you have a serious problem with it, like Joe seems to :), then
> don't use it. No problem. But there is also no reason to tell
> everybody not to use it like that. Someone might have a valid reason.
>
> But in general, if you don't have a good reason to want it, and are
> happy without it, then there is no really persuasive reason to use it.

Lawrenc...@dfait-maeci.gc.ca

unread,
Nov 14, 2000, 12:18:37 AM11/14/00
to
As I've said before, the main difference (for moi)

is that I use require when (to paraphrase a delivery firm advert), it
absolutely positively has to be in there.

I use include when it can fail

so

<?php
require "perfect_php_code.php";
@include "crappy_header_stuff_from_some_dork.php";
?>

Will fail if perfect_php_code.php isn't there, but continue if
crappy_header_stuff_from_some_dork.php is there ;)


Another point:

<?php

for ($i=1;$i<5;$i++) {
require "file". $i . ".php";
}
?>

Will not work, because of the way require differs from include but

<?php

for ($i=1;$i<5;$i++) {
include "file". $i . ".php";
}
?>

Will work.


You can think of include as a souped up require.


Joe, I'm interested in what problems require was giving you.

I've found it to be problem free. I use require ($DOCUMENT_ROOT .
"/phpstuff/xxx.php"); style stuff a lot.

-----Original Message-----
From: Maxim Maletsky [mailto:maxim.m...@japaninc.net]

Sent: November 14, 2000 01:13 PM
To: 'Lars Torben Wilson'
Cc: Lawrenc...@dfait-maeci.gc.ca; php-g...@lists.php.net
Subject: RE: [PHP] Require() vs Include()

Does that mean that we can freely write to the whole mailing list (the whole
world)


DO NEVER USE REQUIRE() SINCE THERE'S INCLUDE() WHICH IS 100.00% BETTER


?


I mean, in something require() is still being good, isn't it?

Cheers,
Maxim Maletsky

-----Original Message-----


From: Lars Torben Wilson [mailto:tor...@php.net]
Sent: Tuesday, November 14, 2000 3:06 PM
To: Maxim Maletsky
Cc: Lawrenc...@dfait-maeci.gc.ca; php-g...@lists.php.net

Subject: RE: [PHP] Require() vs Include()


Maxim Maletsky writes:
> OK, then.
>
> what is the reason require() exists?
>
>
> If it works the exactly same way as include() except that include can be
> more flexible why would we use require() then?
>
> Any performance issues?
> usability?
> security?
>
> Thanks Lawrence,

In PHP 3, it was quite a bit faster than include(). There were other
semantic differences between the two as well, such as return values
etc. Performance appears to now be pretty much the same; you can
return values with include() now, and so forth and so on.

BTW, require() will fail in a completely different way from include()
when it can't find the target file. include() will simply return a
false value, whereas require() will spit out a great big 'Fatal error'
and halt script execution.

jeremy brand

unread,
Nov 14, 2000, 12:31:24 AM11/14/00
to
> Generaly everyone i have know to use require(""); uses it to include
> librarys/classes and such. stuff that you definately are going to need. It
> is more stable i think.
-------------------------

Again, a good example of a misunderstanding. There is no reason why
include() is less stable than require(). Not at all.

If you definately need a file, include() works just perfect anyway --
because if the file you need isn't there, it's not like require() is
going to magically create it for you.

:)

Hopefully people will leave this thread just understanding what the
difference between include() and require() really is, not that one is
better than another, because we all know those are just oppinions.

http://php.net/require
http://www.php.net/manual/function.include.php

Lars Torben Wilson

unread,
Nov 14, 2000, 12:19:44 AM11/14/00
to
Maxim Maletsky writes:
> Does that mean that we can freely write to the whole mailing list (the whole
> world)
>
>
> DO NEVER USE REQUIRE() SINCE THERE'S INCLUDE() WHICH IS 100.00% BETTER

Why on Earth would you want to do something like that?

> ?


>
>
> I mean, in something require() is still being good, isn't it?
>
> Cheers,
> Maxim Maletsky

If you have a serious problem with it, like Joe seems to :), then


don't use it. No problem. But there is also no reason to tell
everybody not to use it like that. Someone might have a valid reason.

But in general, if you don't have a good reason to want it, and are
happy without it, then there is no really persuasive reason to use it.

jeremy brand

unread,
Nov 14, 2000, 12:23:28 AM11/14/00
to
How can you not allow it to fail? if you go to
require('/tmp/the_file_that_does_not_exist'), how do you handle that
successfully and still finish the script?

This is the claim that Joe and I's co-worker claimed. Above was my
example back to him.

That was the end of the conversation.

I do understand what you are saying, but it is misleading when you say
it is not allowed to fail -- it implies (apparently to our co-worker)
that even if a file doesn't exist, somehow his code was magically
going to work anyway. This seems to be what is probably most
misunderstood.

-jeremy brand

http://www.JeremyBrand.com/Jeremy/Brand/Jeremy_Brand.html for more
---------------------------------------------------------------------
We cannot do everything at once, but we can do something at once.
-- Calvin Coolidge

On Tue, 14 Nov 2000, Andi Gutmans wrote:

> Date: Tue, 14 Nov 2000 07:16:22 +0200
> From: Andi Gutmans <an...@zend.com>
> To: Mark Peoples <gas...@gascairlines.com>,
"'PHP General List. (E-mail)'" <php-g...@lists.php.net>


> Subject: RE: [PHP] Require() vs Include()
>

Matt Stone

unread,
Nov 14, 2000, 12:30:56 AM11/14/00
to
Ummm... I can't really understand what you are saying there. Maybe wrong a
spell check first. Or maybe a English check. Or an idiot check :D

Mark Peoples

unread,
Nov 14, 2000, 12:31:32 AM11/14/00
to
I stand corrected. Thanks Lars. =)

Because include() is a special language construct, you must
enclose it within a statement block if it is inside a conditional
block.


I knew sleeping through that CSE100 class a few years ago when they covered
the differences between constructs and functions was a bad idea...waking up
with a kink in my neck wasn't peachy either. Had a pleasant dream though.
=)

marco

jeremy brand

unread,
Nov 14, 2000, 1:03:38 AM11/14/00
to
> used require() again, and don't miss it at all. I can't even think of
> a hypothetical scenario where I would want to use it.

These are exactly my feelings as well.

-jeremy brand

http://www.JeremyBrand.com/Jeremy/Brand/Jeremy_Brand.html for more
---------------------------------------------------------------------
We cannot do everything at once, but we can do something at once.
-- Calvin Coolidge

Mark Maggelet

unread,
Nov 14, 2000, 1:02:12 AM11/14/00
to
On Mon, 13 Nov 2000 21:30:35 -0800 (PST), jeremy brand
(jer...@nirvani.net) wrote:
>> Generaly everyone i have know to use require(""); uses it to
>>include
>> librarys/classes and such. stuff that you definately are going to
>>need. It
>> is more stable i think.
>-------------------------
>
>Again, a good example of a misunderstanding. There is no reason why
>include() is less stable than require(). Not at all.

In fact I've had the opposite experience:

A while back I had a problem with a php3 script that would crash
apache with a very bizarre error message. I tracked the problem down
to a require() statement, and when I changed it to include() my
problems magically went away.

I'm sure the problem is fixed now, but since that happened I never

used require() again, and don't miss it at all. I can't even think of
a hypothetical scenario where I would want to use it.

- Mark

Maxim Maletsky

unread,
Nov 14, 2000, 4:21:02 AM11/14/00
to

a small addition:
this is a piece of my code, I just gropped it here:

if( !$db_oracle_inc_def )
include "$DOCUMENT_ROOT/inc/db_oracle.inc";

as you can see there are no statement block (no brackets)
yet it perfectly wrong, I think what you read was related to the previouse
versions of php.

the code above runs on PHP 4.0.2pl2

Cheers!


-----Original Message-----
From: Lawrenc...@dfait-maeci.gc.ca
[mailto:Lawrenc...@dfait-maeci.gc.ca]

Sent: Tuesday, November 14, 2000 2:08 PM
To: gas...@gascairlines.com; php-g...@lists.php.net
Subject: RE: [PHP] Require() vs Include()

Just to be pedantic ;)


From the manual:

Because include() is a special language construct, you must enclose it


within a statement block if it is inside a conditional block

So

if ($x==$y) {
require ("xx.php");
}
else {
include ("yy.php");
}

BTW your output should still be correct, because the code in the require
isn't being run - it is however being added in before parsing by the php
preprocessor.

The include however, will not be added in if it doesn't get run.

Require is a throwback to #include from C days.

cheers,

Lawrence.

-----Original Message-----


From: Mark Peoples [mailto:gas...@gascairlines.com]

Sent: November 14, 2000 12:56 PM
To: 'PHP General List. (E-mail)'

Subject: RE: [PHP] Require() vs Include()

in file1.php
in file2.php

in file2.php


Hope this helps. =)


marco


Hi,
A tiny question:

WHERE THE TRIIIICK?

Thanks.

Andi Gutmans

unread,
Nov 14, 2000, 11:27:46 AM11/14/00
to
If include() fails the return value is false.

Andi

At 21:22 13/11/00 -0800, jeremy brand wrote:
>How can you not allow it to fail? if you go to
>require('/tmp/the_file_that_does_not_exist'), how do you handle that
>successfully and still finish the script?
>
>This is the claim that Joe and I's co-worker claimed. Above was my
>example back to him.
>
>That was the end of the conversation.
>
>I do understand what you are saying, but it is misleading when you say
>it is not allowed to fail -- it implies (apparently to our co-worker)
>that even if a file doesn't exist, somehow his code was magically
>going to work anyway. This seems to be what is probably most
>misunderstood.
>

>-jeremy brand
>
> http://www.JeremyBrand.com/Jeremy/Brand/Jeremy_Brand.html for more
>---------------------------------------------------------------------
> We cannot do everything at once, but we can do something at once.
> -- Calvin Coolidge
>

>On Tue, 14 Nov 2000, Andi Gutmans wrote:
>
> > Date: Tue, 14 Nov 2000 07:16:22 +0200
> > From: Andi Gutmans <an...@zend.com>
> > To: Mark Peoples <gas...@gascairlines.com>,

> "'PHP General List. (E-mail)'" <php-g...@lists.php.net>


> > Subject: RE: [PHP] Require() vs Include()
> >

Myke Hines

unread,
Nov 14, 2000, 1:37:47 PM11/14/00
to
Wasn't their an issue in using require/includes in classes, if's, whiles, or
something?
One would always be included where as the other would only be included if it
meet the condition it was in. Sorry so vague.


-----Original Message-----
From: Duke Normandin [mailto:0103...@3web.net]
Sent: Tuesday, November 14, 2000 3:21 AM
To: Lars Torben Wilson

Cc: php-g...@lists.php.net
Subject: RE: [PHP] Require() vs Include()


On 14 Nov 00 at 11:32, php-general-digest-help@lists wrote:

>Maxim Maletsky writes:
>> OK, then.
>>
>> what is the reason require() exists?
>>
>>
>> If it works the exactly same way as include() except that include can be
>> more flexible why would we use require() then?
>>
>> Any performance issues?
>> usability?
>> security?
>>
>> Thanks Lawrence,
>
>In PHP 3, it was quite a bit faster than include(). There were other
>semantic differences between the two as well, such as return values
>etc. Performance appears to now be pretty much the same; you can
>return values with include() now, and so forth and so on.
>
>BTW, require() will fail in a completely different way from include()
>when it can't find the target file. include() will simply return a
>false value, whereas require() will spit out a great big 'Fatal error'
>and halt script execution.

Is my paraphrasing correct?

require() is "absolute" -- so don't use it in a conditional statement
because it makes no sense (to me anyway)

require() is "absolute" -- make sure that what you "require" exists where
it's suppose to be, otherwise the script fails "hard".

include() is *not* absolute -- use it in a conditional

include() failures do so "softly" - returns garbage, e.g.

Up to php4.0.1 require() had the edge on speed -- none to very litle with
php4.0.2+.

-duke
Calgary,Alberta, Canada

Duke Normandin

unread,
Nov 14, 2000, 1:32:29 PM11/14/00
to

Maxim Maletsky

unread,
Nov 14, 2000, 9:05:45 PM11/14/00
to
not since the version 4.0.2 as Zeev says...

now require() and include() are *exactly* the same except that require()
exits the script prematurely on a failure. Everything else is identic.

Cheers!

-----Original Message-----
From: Tim Ward [mailto:tim....@stivesdirect.com]
Sent: Tuesday, November 14, 2000 9:01 PM
To: Maxim Maletsky
Subject: RE: [PHP] Require() vs Include()


if you're in a loop require will only run the first time, hence use it for
including external functions.

Tim Ward
Senior Systems Engineer

Please refer to the following disclaimer in respect of this message:
http://www.stivesdirect.com/e-mail-disclaimer.html


> -----Original Message-----
> From: Maxim Maletsky [mailto:maxim.m...@japaninc.net]

> Sent: 14 November 2000 04:52
> To: 'Lawrenc...@dfait-maeci.gc.ca'
> Cc: php-g...@lists.php.net
> Subject: RE: [PHP] Require() vs Include()
>
>

> OK, then.
>
> what is the reason require() exists?
>
>
> If it works the exactly same way as include() except that
> include can be
> more flexible why would we use require() then?
>
> Any performance issues?
> usability?
> security?
>
> Thanks Lawrence,
>
>

> -----Original Message-----
> From: Lawrenc...@dfait-maeci.gc.ca
> [mailto:Lawrenc...@dfait-maeci.gc.ca]

> Sent: Tuesday, November 14, 2000 1:37 PM
> To: Maxim Maletsky; Lawrenc...@dfait-maeci.gc.ca

> Cc: php-g...@lists.php.net
> Subject: RE: [PHP] Require() vs Include()
>
>

> -----Original Message-----
> From: Maxim Maletsky [mailto:maxim.m...@japaninc.net]

> Sent: November 14, 2000 12:34 PM
> To: 'Lawrenc...@dfait-maeci.gc.ca'
> Cc: 'PHP General List. (E-mail)'

> Subject: RE: [PHP] Require() vs Include()
>
>

> Well,
> with include() if an included file does not exists it WILL fail.
>
> so where's the difference?
>

> -----Original Message-----
> From: Lawrenc...@dfait-maeci.gc.ca
> [mailto:Lawrenc...@dfait-maeci.gc.ca]

> Sent: Tuesday, November 14, 2000 1:26 PM
> To: Maxim Maletsky

> Subject: RE: [PHP] Require() vs Include()
>
>

> include won't fail out if the includes not there, but require will.
>
> If you have something that *has* to be in there, then require
> is used, if
> something could be optional - eg a generated header or
> something than might
> be or might not be there at a given time, use include.
>
>
>

Maxim Maletsky

unread,
Nov 14, 2000, 9:50:22 PM11/14/00
to
As I already tried to mention in here:

Now (since the PHP4.0.2) require() and include() are working in the EXACTLY
same way.
the only difference remains the premature exit of the file on a failure of
require().

It is what Zeev wrote in his previous email on my thread.

Cheers,
Maxim Maletsky.

-----Original Message-----
From: Myke Hines [mailto:My...@WebSite.WS]
Sent: Wednesday, November 15, 2000 3:39 AM
To: php-g...@lists.php.net
Subject: RE: [PHP] Require() vs Include()

Wasn't their an issue in using require/includes in classes, if's, whiles, or
something?
One would always be included where as the other would only be included if it
meet the condition it was in. Sorry so vague.


-----Original Message-----
From: Duke Normandin [mailto:0103...@3web.net]
Sent: Tuesday, November 14, 2000 3:21 AM
To: Lars Torben Wilson

Cc: php-g...@lists.php.net
Subject: RE: [PHP] Require() vs Include()

On 14 Nov 00 at 11:32, php-general-digest-help@lists wrote:

>Maxim Maletsky writes:
>> OK, then.
>>
>> what is the reason require() exists?
>>
>>
>> If it works the exactly same way as include() except that include can be
>> more flexible why would we use require() then?
>>
>> Any performance issues?
>> usability?
>> security?
>>
>> Thanks Lawrence,
>

>In PHP 3, it was quite a bit faster than include(). There were other
>semantic differences between the two as well, such as return values
>etc. Performance appears to now be pretty much the same; you can
>return values with include() now, and so forth and so on.
>
>BTW, require() will fail in a completely different way from include()
>when it can't find the target file. include() will simply return a
>false value, whereas require() will spit out a great big 'Fatal error'
>and halt script execution.

Is my paraphrasing correct?

require() is "absolute" -- so don't use it in a conditional statement
because it makes no sense (to me anyway)

require() is "absolute" -- make sure that what you "require" exists where
it's suppose to be, otherwise the script fails "hard".

include() is *not* absolute -- use it in a conditional

include() failures do so "softly" - returns garbage, e.g.

Up to php4.0.1 require() had the edge on speed -- none to very litle with
php4.0.2+.

-duke
Calgary,Alberta, Canada

--

Reply all
Reply to author
Forward
0 new messages