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.

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?

$_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).


var_dump($_SERVER);

http://php.net/manual/en/reserved.variables.server.php

Thank you cups :slight_smile:

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.