Home >Backend Development >PHP Tutorial >PHP basic learning includes require, include

PHP basic learning includes require, include

不言
不言Original
2018-04-24 14:04:181291browse

The main content of this article is about the basic learning of PHP including require and include. It has a certain reference value. Now I share it with you. Friends in need can refer to it

In PHP , you can insert the contents of another file into a PHP file before it is executed by the server.

a.php:


<?php
/** 
* Created by PhpStorm. 
* User: Administrator 
* Date: 2018/4/23 
* Time: 13:03 
*/
echo "123<br>";
$arr = array("php" => array("Tom", "123456"), "google" => array("Jake", "456123"), "taobao" => array("Lili", "789456"));

b.php:

<?php
/** 
* Created by PhpStorm. 
* User: Administrator 
* Date: 2018/4/23 
* Time: 13:08 
*/
require "a.php";
//require_once "a.php";
//require_once(realpath("./")."/a.php");
//require_once(realpath("../")."/require/a.php");
//echo realpath("./");
//echo realpath("../");
print("<pre class="brush:php;toolbar:false">"); 
// 格式化输出数组
print_r($arr);
print("
");

Output result:



include and require are identical except for how they handle errors:

  • require Generates a fatal error (E_COMPILE_ERROR), after which the script will stop executing.

  • include generates a warning (E_WARNING) and the script will continue execution after the error occurs.


is equivalent to copying the code of the a.php file into b.php. Similar methods include require_once(), include(), and include_once(). The methods with the word _once are declared to be loaded only once, because your require_once() file may already require_once(a.php). , if it is loaded again, it will cause repeated loading.

The above is the detailed content of PHP basic learning includes require, include. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn