www.webdeveloper.com
Results 1 to 4 of 4

Thread: why use require_once

  1. #1
    Join Date
    Dec 2007
    Posts
    57

    why use require_once

    I know this sounds like a stupid question but why would you use require_once, why dont you just put the code in the same file?

  2. #2
    Join Date
    Mar 2008
    Posts
    123
    because you can use the same code in different pages, so if you had to make a change to that code, you just change the file thats being included vs changing the same code in every page. You just include once, because other pages included might also include the same file, so if it's already been included, then the rest of the includes are ignored.

    ex:
    index.php
    PHP Code:
    include_once("database.php");
    echo 
    'hello';
    include(
    "footer.php"); 
    footer.php
    PHP Code:
    //this database.php include is ignored.
    include("database.php");
    echo 
    'year 2008.'

  3. #3
    Join Date
    Mar 2008
    Location
    Sri Lanka
    Posts
    43
    You may also learn about
    require ('test.php')
    both include and require do the same job, but if test.php is not available then require will stop the program by creating a fatal error but include will allow you to continue but it'll show a warning mesg!

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,321
    require_once is typically used to require files which define classes and/or functions, as (1) you only need to define them once per script, and (2) you'll get a fatal error if you try to define the same class or function name more than once.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles