Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,472
There are 1411 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Business and Website Management     Web Hosting and Domain Names :

PHP: include & require, what is diff

Thread title: PHP: include & require, what is diff
Closed Thread    
    Thread tools Search this thread Display Modes  
05-13-2007, 10:37 AM
#1
montyauto is offline montyauto
montyauto's Avatar
Status: Junior Member
Join date: Apr 2006
Location:
Expertise:
Software:
 
Posts: 68
iTrader: 0 / 0%
 

montyauto is on a distinguished road

  Old  PHP: include & require, what is diff

Hope this is not stupid question.

I read the manual frequently, still could not understand what different between include() and require(), it makes no different to me.

Could you show me in an example script to show the different result between those two? Thanks.

05-13-2007, 11:44 AM
#2
Seb is offline Seb
Seb's Avatar
Status: Watermelon Man
Join date: Nov 2006
Location: London
Expertise:
Software:
 
Posts: 3,312
iTrader: 36 / 100%
 

Seb is on a distinguished road

  Old

I'm not really an expert, but I think include takes the script and inserts it into the new one, so if you included some HTML, and viewed the page source, you would simply see the HTML included, and require makes the script check say a config file to make the file work properly. Then again, I could be wrong.

05-13-2007, 11:57 AM
#3
Salathe is offline Salathe
Salathe's Avatar
Status: Community Archaeologist
Join date: Jul 2004
Location: Scotland
Expertise: Software Development
Software: vim, PHP
 
Posts: 3,820
iTrader: 25 / 100%
 

Salathe will become famous soon enough

Send a message via MSN to Salathe

  Old

To quote the PHP manual:

require() and include() are identical in every way except how they handle failure. They both produce a Warning, but require() results in a Fatal Error.
So, if you try to include a file that does not exist then a warning (technically E_WARNING) will be raised but the rest of the script is allowed to run through until the end. If you try to require a file which doesn't exist then a fatal error (E_ERROR) warning will be raised and the script will immediately stop.

PHP Code:
// Both of these will run as expected, no errors
include ('file_that_exists.php');
require (
'file_that_exists.php');

// Will give a warning, but page will continue executing
include ('file_not_exists.php');

// Will give a fatal error, page will stop executing here
require ('file_not_exists.php'); 
Hope that clears things up a bit.

05-13-2007, 12:12 PM
#4
ChrisM is offline ChrisM
ChrisM's Avatar
Status: Junior Member
Join date: Mar 2007
Location: Cape Town, South Africa
Expertise:
Software:
 
Posts: 96
iTrader: 0 / 0%
 

ChrisM is on a distinguished road

Send a message via MSN to ChrisM

  Old

Spot on Salathe!

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.

Just to give the full story at once

Ref: Self Improvement

Closed Thread    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed