Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,683
    Thanks
    28
    Thanked 4 Times in 4 Posts

    Using require vs require_once() - i am a bit confused !

    Hello all,

    I have been using require_once() throughout my scripts because I thought it would save the processor time keep looking up the same function include file.

    But it seems I might be wrong !

    The Manual says
    The require_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the require() statement, with the only difference being
    Does this mean the require_once() is only useful if you use it in the same script more than once ?

    I have about 30 different scripts that start like this:

    PHP Code:
    /*
    *  a_get_detail.php
    *
    *
    * Connects to tables and extracts the data
    */

    require_once("my_functions.php");

    other code .... 
    I have also just read that require_once() is slower than the standard require() function.

    In this case is it better for me to be using require() as I don't think I am gaining anything from using require_once().

    Thanks for any advice.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #2
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    require_once is the "safer" function as it will not allow you to include a file more than once which can couse errors. You shuoldn't use it when you do want to include a file more than once.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #3
    Senior Coder
    Join Date
    May 2006
    Posts
    1,683
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Well in this case the file just contains my
    data cleaning functions eg

    PHP Code:
    function safe_sql_br$value )
    {
         
    $value nl2br($value);
         
    $value trim(strip_tags($value,'<br>'));     
        
    // Stripslashes
        
    if (get_magic_quotes_gpc()) {
            
    $value stripslashes($value);
        }
        
    // Quote if not integer
        
    if (!is_numeric($value)) {
            
    $value mysql_real_escape_string($value);
        }
        return 
    $value;
    // End of Function 
    So in this case would you say its best to use

    require_once()
    or require()

    ???
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #4
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    By those errors I ment that the programmer may not fully know what he is doing and get errors he wouldn't expect (for example a "cannot redeclare function" error). If you know what you are doing you can use require().
    require_once can still be useful for cases where you make sure a file is included (and not incuded if it already was).
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #5
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    actually, if you read carefully, from fastest to slowest:

    include
    require
    include_once
    require_once

    Why?
    Include just brings the file in.

    Require first stats the file and then brings the file in

    Include_once has to go through the entire loaded files table to ensure the file hasn't been included, then it brings it in.

    require_once first stats the file, then looks through the entire loaded files table to ensure the file hasn't been included, then brings it in.

  • #6
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    include() would also stat the file. It returns a warning if the file does not exist.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #7
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    You're right, I was mis-remembering this from the PHP documentation.

    Note: Prior to PHP 4.0.2, the following applies: require() will always attempt to read the target file, even if the line it's on never executes. The conditional statement won't affect 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. Similarly, looping structures do not affect the behaviour of require(). Although the code contained in the target file is still subject to the loop, the require() itself happens only once.


  •  

    Posting Permissions

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