943,962 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 2251
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 11th, 2008
0

include Vs require

Expand Post »
Hi All,

I have started learning PHP now.
I am unable to find the exact difference between include and require in php?
Similar Threads
Reputation Points: 45
Solved Threads: 1
Junior Poster in Training
rati is offline Offline
78 posts
since Aug 2006
Aug 11th, 2008
1

Re: include Vs require

The require() function is identical to include(), except that it handles errors differently.

The include() function generates a warning (but the script will continue execution) while the require() function generates a fatal error (and the script execution will stop after the error).

check this :
http://www.tizag.com/phpT/require.php
Last edited by Shanti C; Aug 11th, 2008 at 7:09 am.
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008
Aug 11th, 2008
0

Re: include Vs require

and also check this:
---------------------------------------------------------------------------------------
PHP provides four functions which enable you to insert code from other files.

* include()
* require()
* include_once()
* require_once()

All four can take a local file or URL as input. None of them can import a remote file.
require() and include() functions are virtually similar in their function except for the way they handle an irretrievable resource. include() and include_once() provide a warning if the resource cannot be retrieved and try to continue execution of the program if possible. require() and require_once functions provide stop processing the page if they cannot retrieve the resource.

Why include_once() and require_once()
The include_once() and require_once() functions are handy in situations where multiple files may reference the same included code. For example:

File A.php includes File B.php and C.php
File B.php includes File C.php

File C.php has been included twice, so the interpreter would print an error. Since a function cannot be redefined once it’s declared, this restriction can help prevent errors.

If both File A.php and File B.php use include_once() or require_once() to import File C.php, no errors would be generated. PHP would understand that you only want one instance of the code in File C and would not try to redeclare the functions.

It is best to use require_once() to include files which contain necessary code and include_once() to include files that contain content which the program can run without e.g. HTML, CSS, etc
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008
Aug 11th, 2008
0

Re: include Vs require

hey;

I had been using the include and require if i want to have a continue execution. but i also use the include_once and require_once if I want the execution once only.

hope this help you.
Reputation Points: 10
Solved Threads: 0
Light Poster
god_1896 is offline Offline
39 posts
since Jul 2008
Aug 11th, 2008
0

Re: include Vs require

Thanks for your reply.
Another query is that is there any differnce in the opcode generated?
Is it something that the opcode for require is generated when the keyword is encountered and for include is generated when the function related to the file which has been included is called??
Reputation Points: 45
Solved Threads: 1
Junior Poster in Training
rati is offline Offline
78 posts
since Aug 2006
Aug 11th, 2008
0

Re: include Vs require

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.

In PHP 3, it is possible to execute a return statement inside a require()ed file, as long as that statement occurs in the global scope of the require()ed file. It may not occur within any block (meaning inside braces ({}). In PHP 4, however, this ability has been discontinued. If you need this functionality, see include().
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008
Aug 11th, 2008
0

Re: include Vs require

Difference between "require()" and "include()" in PHP
This should be well known, and people should be aware as to why they are using either or. But, I've noticed lately that a lot of people new to PHP or programming are not aware of the difference. Depending on what you need you, need to decide what the differences are. So...

* require() : If the file does not exist, you will get a fatal error.
* include() : If the file does not exist, you will get a warning and the next line of code will execute.
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008
Aug 11th, 2008
0

Re: include Vs require

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.

In PHP 3, it is possible to execute a return statement inside a require()ed file, as long as that statement occurs in the global scope of the require()ed file. It may not occur within any block (meaning inside braces ({}). In PHP 4, however, this ability has been discontinued. If you need this functionality, see include().


This means that if i give

PHP Syntax (Toggle Plain Text)
  1. if(5>6)
  2. include "a.php";
  3. else
  4. include "b.php";
only b will be included ..and if i say

PHP Syntax (Toggle Plain Text)
  1. if(5>6)
  2. require "a.php";
  3. else
  4. require "b.php";

both a.php and b.php are included?
Last edited by Tekmaven; Aug 12th, 2008 at 5:53 am. Reason: Code tags
Reputation Points: 45
Solved Threads: 1
Junior Poster in Training
rati is offline Offline
78 posts
since Aug 2006
Aug 11th, 2008
0

Re: include Vs require

yep...
see this example...
php Syntax (Toggle Plain Text)
  1. $value = true;
  2.  
  3. if($value == true)
  4. {
  5. echo "alright";
  6. }
  7.  
  8. else
  9. {
  10. require("error.php");
  11. }
  12.  
  13. // error.php will be included, even when $value is true
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008
Aug 11th, 2008
0

Re: include Vs require

hello sorry for my bad post above...its damn wrong...
my code is based on if condition ,not on require function...
so please kindly ignore the above post...please..
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Pliz help with MySQL query
Next Thread in PHP Forum Timeline: Super Global variables





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC