SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,106
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    when should i use require_once,include_once,$_server

    Hi, can you help me to enlighten my mind when should i use this require_once,include_once,$_server ?

    does the require_once and include_once having the same functionality?

    Thank you in advance.

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    When you are not sure whether an include file has already been included, possibly higher up in your include files. This can happen when you are relying on external libraries and you are not sure what is happening higher up.

    The diff between include and require is in how it handles failure:

    Manual page on require states:

    "require is identical to include except upon failure it will also produce a fatal E_COMPILE_ERROR level error. In other words, it will halt the script whereas include only emits a warning (E_WARNING) which allows the script to continue."

    The same goes for the *_once() cousins.

  3. #3
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,106
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    When you are not sure whether an include file has already been included, possibly higher up in your include files. This can happen when you are relying on external libraries and you are not sure what is happening higher up.

    The diff between include and require is in how it handles failure:

    Manual page on require states:

    "require is identical to include except upon failure it will also produce a fatal E_COMPILE_ERROR level error. In other words, it will halt the script whereas include only emits a warning (E_WARNING) which allows the script to continue."

    The same goes for the *_once() cousins.

    Hi cups, thank you for the reply, what about the $_SERVER[] ? when to use this is this also different in require?

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    $_SERVER is an array of variables which are only available on the server side some to do with the script which is running, others to do with the server setup and others concerned with the request that was made for this script.

    These can be very useful, to see what they contain do this on one of your scripts (not on a live server, obviously).
    PHP Code:
    var_dump($_SERVER); 
    http://php.net/manual/en/reserved.variables.server.php

  5. #5
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,106
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    $_SERVER is an array of variables which are only available on the server side some to do with the script which is running, others to do with the server setup and others concerned with the request that was made for this script.

    These can be very useful, to see what they contain do this on one of your scripts (not on a live server, obviously).
    PHP Code:
    var_dump($_SERVER); 
    http://php.net/manual/en/reserved.variables.server.php
    Thank you cups

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    Generally speaking, I always use include_once or require_once, unless I -know- i'm going to repeat the same output multiple times.

    Call it lazy if you like, but it also helps me when I want to intercept a user - for example, when their session has timed out, and they're not logged in anymore, I want to give them the login screen again. My login page include_once's my header - but since my header would already have been called by the page they were trying to access, I dont want the header twice.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  7. #7
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,106
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    Generally speaking, I always use include_once or require_once, unless I -know- i'm going to repeat the same output multiple times.

    Call it lazy if you like, but it also helps me when I want to intercept a user - for example, when their session has timed out, and they're not logged in anymore, I want to give them the login screen again. My login page include_once's my header - but since my header would already have been called by the page they were trying to access, I dont want the header twice.
    Oh..I see ,...thank you for the reply.


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
  •