The include() statement includes and evaluates the specified file.

The documentation below also applies to require().

Files are included based on the file path given or, if none is given, the include_path specified. If the file isn’t found in the include_path, include() will finally check in the calling script’s own directory and the current working directory before failing. The include() construct will emit a warning if it cannot find a file; this is different behavior from require(), which will emit a fatal error.

If a path is defined — whether absolute (starting with a drive letter or \ on Windows, or / on Unix/Linux systems) or relative to the current directory (starting with . or ..) — the include_path will be ignored altogether. For example, if a filename begins with ../, the parser will look in the parent directory to find the requested file.

For more information on how PHP handles including files and the include path, see the documentation for include_path.

When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.

Example #1 Basic include() example

vars.php
<?php

$color = 'green';
$fruit = 'apple';

?>

test.php
<?php

echo "A $color $fruit"; // A

include 'vars.php';

echo "A $color $fruit"; // A green apple

?>

If the include occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function. So, it will follow the variable scope of that function. An exception to this rule are magic constants which are evaluated by the parser before the include occurs.

Example #2 Including within functions

<?php

function foo()
{
global $color;

include 'vars.php';

echo "A $color $fruit";
}

/* vars.php is in the scope of foo() so *
* $fruit is NOT available outside of this *
* scope. $color is because we declared it *
* as global. */

foo(); // A green apple
echo "A $color $fruit"; // A green

?>

When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags.

If “URL fopen wrappers” are enabled in PHP (which they are in the default configuration), you can specify the file to be included using a URL (via HTTP or other supported wrapper – see Supported Protocols and Wrappers for a list of protocols) instead of a local pathname. If the target server interprets the target file as PHP code, variables may be passed to the included file using a URL request string as used with HTTP GET. This is not strictly speaking the same thing as including the file and having it inherit the parent file’s variable scope; the script is actually being run on the remote server and the result is then being included into the local script.

Quieres ver como te envuelven a diario las ondas del WIFI? Quienes ver como te envuelven a diario las ondas del WIFI? Esta es una recopilación de un trabajo que ha hecho un fotografo utilizando una técnica ...
Opencart Module for Youtube Banner (VQMod) This Mod Requires VQMod To Work This modification allows you to put a Youtube Video ID in the System -> Design -> Banners link column. When ...
Que es el Link building Linkbuilding es una de las estrategias del SEO que consiste en conseguir que otras páginas web enlacen a la página que nos interese para hacer que los...
Skype con soporte para Windows 8 Tras la compra de Skype por parte de Microsoft el pasado mes de mayo, el programa de comunicación sobre IP con mayor número de usuarios en todo el mun...
Módulo Opencart para descuentos y tarifas Esta extensión le permite establecer descuentos avanzadas e impuestos en su tienda, y se puede calcular la cantidad, total , peso o volumen. Configur...
Como Descargar los Albúnes de Fotos de Facebook en un PDF Facebook albums to PDF, es una sencilla yútil apps que nos permite descargar un albúm de fotos completo desde faceb0ok generado en un archivo .pdf, in...
Convertir tráfico a resultados es igual a hacer dinero con su web De nada sirven las tecnologías que se utilicen, ni lo bonito de los sitios Web si no cumplen con su objetivo. Tres componentes fundamentales para conv...
¿Insólito? Lee aquí cual es el smartphone usado por el 92% de los viajeros Gopili.es ha realizado una curiosa encuesta sobre las costumbres y las diferencias entre los usuarios de distintas marcas de smartphone a la hora de h...
Skype ahora permite “invitados” a sus conversaciones La persona que no tenga una cuenta en Skype ya pueden ser parte de una conversación a través de este servicio, ya que la popular aplicación de videoll...
Los usuarios de WhatsApp pronto podrán confirmar cuándo se leen los mensajes que envían os usuarios de WhatsApp pronto podrán tener una confirmación de cuándo se leen los mensajes que envían, o al menos eso es lo que han transmitido un gr...
include function on PHP
Tagged on: