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/>
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
--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
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."
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."
--
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
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|
+----------------------------------------------------------------+
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|
+----------------------------------------------------------------+
--
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.
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."
--
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?
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
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/
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. =)
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.
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.
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
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.
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()
>
>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.
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
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
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
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.