SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Enthusiast nguip's Avatar
    Join Date
    Apr 2001
    Location
    Malaysia
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi..

    I'm very new in php so allow me to ask some silly questions here

    Can someone explain to me what is the different between require and include ?

    eg: what's the different between:-
    Code:
     
    require('includes/header.php'); 
    include('includes/header.php');
    And how to specific the location in the include() ?
    If i'm working in c:\www\mysite\scripts\project1\ and I want to include a file in c:\www\mysite\includes\header.php Does this work ?

    Code:
    include('../../includes/header.php');
    Last edited by nguip; Apr 12, 2001 at 01:15.
    Ngu I.P.
    Web Developer

  2. #2
    SitePoint Wizard silver trophy Karl's Avatar
    Join Date
    Jul 1999
    Location
    Derbyshire, UK
    Posts
    4,411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Require and include both work pretty much the same now:

    To quote Zeev Suraski (CTO of Zend): "require() now works very much like in include(), only it bails out if it fails."
    Karl Austin :: Profile :: KDA Web Services Ltd.
    Business Web Hosting :: Managed Dedicated Hosting
    Call 0800 542 9764 today and ask how we can help your business grow.

  3. #3
    SitePoint Enthusiast nguip's Avatar
    Join Date
    Apr 2001
    Location
    Malaysia
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks..

    But I don't quite understand what's the "only it bails out if it fails"
    Ngu I.P.
    Web Developer

  4. #4
    SitePoint Zealot JagXJ220's Avatar
    Join Date
    Mar 2001
    Location
    Not worth the drive
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I've found is that there is a difference between include() and require().

    If you use the require() function to include a page on your site, it doesn't parse that page and then include it. It includes the file first, then it parses it.

    If you use the include() function to include a page on your site, it parses the page defined in the function first, then includes it on your page.

    If your page that you're going to include has variables that are defined in the page that you're calling the function on, then you should use the require() function.

    If your pages are just static html, then you could just use the include() function.

    Personally, I always just use the require() function, because it has more usability than the include() function.

  5. #5
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To parse and not to parse might affect the system resources (more code to parse, use more resources)
    But as JagXJ220 said, I use require more frequently.
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  6. #6
    SitePoint Enthusiast nguip's Avatar
    Join Date
    Apr 2001
    Location
    Malaysia
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.

    I guess I roughly get the idea now.

    I used a include() function while following Kelvin's tutorial on "managing a sessions". It was say that if you want to restrict any page you can just add the

    PHP Code:
    include ("accesscontol.php"); 
    But when I do this to one of my page, it give me error messages. Any ideas ?

    The page is working fine if I just add this to the pages within the same "C:\www\directory\sessions" but if I used it in one of the file in "C:\www\directory\project" it give me errors. Why ? Can anyone help ?

    The code I use to include the file is like this and I don't have any other php code within that page itself (I followed exactly what's in kelvin's tutorial)
    PHP Code:
    <?php include ("../project/accesscontrol.php"); ?>
    Ngu I.P.
    Web Developer

  7. #7
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What exactly was the error message?
    It's very useful to see what kind of error you got instead of guessing one
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  8. #8
    SitePoint Enthusiast nguip's Avatar
    Join Date
    Apr 2001
    Location
    Malaysia
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm...

    I get this :-
    Code:
    Warning:
    Failed opening 'common.php' for inclusion (include_path='')
    in /home/webuser/mydomain/public_html/sessions/accesscontrol.php on line 3
    
    Warning: 
    Failed opening 'db.php' for inclusion (include_path='')
    in /home/webuser/mydomain/public_html/sessions/accesscontrol.php on line 4
    
    Warning: 
    Cannot send session cookie - headers already sent by (output started
    at /home/webuser/mydomain/public_html/sessions/accesscontrol.php:3)
    in /home/webuser/mydomain/public_html/sessions/accesscontrol.php on line 6
    
    Warning:
    Cannot send session cache limiter - headers already sent (output started 
    at /home/webuser/mydomain/public_html/sessions/accesscontrol.php:3)
    in /home/webuser/mydomain/public_html/sessions/accesscontrol.php on line 6
    Last edited by nguip; Apr 13, 2001 at 00:04.
    Ngu I.P.
    Web Developer

  9. #9
    SitePoint Enthusiast
    Join Date
    Mar 2001
    Location
    Washington State
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have a look here :

    http://www.php.net/manual/en/configu...i.include-path

    Be sure your include_path has the . as shown in the above, it means includes will be searched relative to current directory first, you may be missing this.

    The "headers already sent" errors result from the above errors and have no real meaning here.

  10. #10
    SitePoint Enthusiast nguip's Avatar
    Join Date
    Apr 2001
    Location
    Malaysia
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thnaks...

    But I don't quite understand.. I guess I'll have to contact my admin now.
    Ngu I.P.
    Web Developer

  11. #11
    SitePoint Enthusiast pman's Avatar
    Join Date
    Apr 2001
    Location
    Planet Namek
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm also a novice at this. but, let me give it shot.

    I've configured Perl scripts before...or installed them (which sometimes is a BIG task)

    I've been in to PHP less than a week.


    but I'm pretty sure that the functions:
    Code:
    require($whateverfile) - takes information from $whateverfile 
    if needed.
    
    
    include($whateverfile) - takes the entire $whateverfile and 
    displays the file as it would be called seperately and integrate 
    that particular file with the file you are calling from.
    
    (i.e. - say if you have a file named "forum.php" and in this 
    particular file, you use the include() function to call another 
    file named "header.php", i.e. include(header.php), it would
     display the content of header.php in forum.php.)
    
    
    
    
    require() - i'm guessing that you already know what variables 
    are.
    
    (i.e. you might use the require function if you are creating 
    a .php file that you might want to use some variables in, but 
    you don't want to define them in this particular page because 
    you've already defined them in another page, and it will 
    gather the variables and such things from the $whateverfile.)
    I can probably try and give some better examples if you still don't understand.

    since I don't understand this stuff all that well, myself..I would apreciate it if some can tell me if I'm right or wrong ...either way I'd like to know.

    Thanks!
    Last edited by pman; Apr 14, 2001 at 16:10.
    in persuit of world domination...uhh..for the better!

  12. #12
    SitePoint Enthusiast nguip's Avatar
    Join Date
    Apr 2001
    Location
    Malaysia
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for those who reply

    I'm getting more confuse now Does anyone know exactly what's is the different ? I'm a total newbies here

    I mean I also setup and install perl cgi script b4... but I don't think this make any different here.

    I haven't experiment with require but I know php will actually parse include() file and replace it in that include page.

    No idea on require... but I see ppl sometime use require n sometime use include...
    Ngu I.P.
    Web Developer

  13. #13
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually pman, I think you have it around the wrong way.

    require()
    From the manual http://www.php.net/manual/en/function.require.php
    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.
    So require() will place the contents of the required file "inline" into the calling script during parsing before the script is interpreted and executed. It is just like the C preprocessor directive #include

    include()
    http://www.php.net/manual/en/function.include.php
    The contents of the include file are parsed and executed at run time if execution enters the path which has the include() directive. So the included file will only be included at run time if it needs to be. Whereas, the contents of the require() file will always be included inline in the code regardless of whether it is needed during run time or not.

    Example
    PHP Code:
    foo.inc
    -----
    <?php echo "foo"?>

    bar.inc
    -----
    <php echo "bar"; ?>

    zot.php
    -----
    <?php
    if ($flipflop) {
       require(
    "foo.inc");
    } else {
       require(
    "bar.inc");
    }
    ?>
    The php interpreter will never see the code as it is written above. During parsing (before it is interpreted and executed) it will be expanded as if you had written it like so:
    PHP Code:
    <?php
    if ($flipflop) {
       echo 
    "foo";
    } else {
       echo 
    "bar";
    }
    ?>
    However, had the code been written like so:
    PHP Code:
    zot.php
    -----
    <?php
    if ($flipflop) {
       include(
    "foo.inc");
    } else {
       include(
    "bar.inc");
    }
    ?>
    and lets assume that $flipflop is TRUE, then "foo.inc" would be parsed and executed during run time. However, "bar.inc" would never get parsed because execution never enters the path that contains include("bar.inc")

  14. #14
    SitePoint Enthusiast pman's Avatar
    Join Date
    Apr 2001
    Location
    Planet Namek
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh...alright. Thanks for making things clear.

    I just bought this book about 3-4 days ago by Wrox - Beginning PHP4..I read the first couple of chapters. so, I still have a long way to go.

    and I read and practiced a couple of tutorials from devshed.

    well, thanks again.
    in persuit of world domination...uhh..for the better!

  15. #15
    SitePoint Enthusiast nguip's Avatar
    Join Date
    Apr 2001
    Location
    Malaysia
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well alright... freak... thanks

    I think this pretty clear now for me

    pman: I haven't even bought a book yet....
    Ngu I.P.
    Web Developer

  16. #16
    SitePoint Enthusiast pman's Avatar
    Join Date
    Apr 2001
    Location
    Planet Namek
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    pman: I haven't even bought a book yet....
    I normally wouldn't buy a book either as there are many tutorials on the net. But, the book I bought seems in depth. the best part about this book is that you can download the tutorial/example files off their website..so, it makes it easier to follow along with out taking up too much time.

    so far, it looks like a good book.
    in persuit of world domination...uhh..for the better!

  17. #17
    SitePoint Enthusiast nguip's Avatar
    Join Date
    Apr 2001
    Location
    Malaysia
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm...

    I'm looking into buying a php book also.. just the matter of time and which book to buy. And of course MONEY

    I read that a lot of ppl seen to be recommending to buy the following books:-

    1. PHP Essentials by Julie Meloni
    2. Beginning PHP4 Programming (Wrox Press)
    3. Sam's "Teach Yourself PHP4 in 24 Hours"
    4. Wrox's Professional PHP Programming
    5. PHP & MySQL Developer by Luke Welling & Laura Thomson.
    6. PHP Developer's Cookbook

    What is your comment? I was hoping that I just want to buy 2 books here. Maybe 1 for begginer and 1 advance reference book. Which one would you suggest ?
    Ngu I.P.
    Web Developer

  18. #18
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have never bought one, but I should! There have been a few discussions about this and from what I remember the Wrox books were highly regarded. Do a search on "php books" and see what comes up.

  19. #19
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Professional PHP programming definitely... i think it is even easier to learn from than beginning PHP.. and that is from someone with almost no programming background at all.


Bookmarks

Posting Permissions

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