PHP解析包括 [英] PHP parsing on includes

查看:120
本文介绍了PHP解析包括的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我包含一个文件 init.php ,它定义了路径常量。因此,如果我在文件( index.php )中包含 init.php ,然后在另一个文件( layout / header.php )中包含.. 。 init.php 在被添加到这些文件之前被解析了,还是被添加到父文件中,然后将父文件作为一个整体进行解析?

I'm including a file init.php which defines path constants. So if I include init.php in a file (index.php) and then in another file (layout/header.php)... is init.php parsed before being added to these files or is it added to the parent file and then the parent file is parsed as a whole?

编辑:为什么这很重要是因为 init.php 定义了相对于解析位置的路径变量。

Why this is important is because init.php defines path variables relative to location of where it is parsed.

推荐答案

实际上 include require 除了 require之外都是相同的将以 E_ERROR 失败,而 include 将发出警告。这两个语句只有在实际执行脚本内部时才会被激活。因此,以下代码将始终有效:

Actually include and require are identical in all except require will fail with E_ERROR while include will issue a warning. Also both of the statements are only activated when they actually executed inside script. So the following code will always work:

<?php
echo "Hello world";
if (0) require "non_existing.php";

您的问题的答案是 index.php 将首先解析并执行。然后当包含init.php遇到文件 init.php 在当前范围内被解析并执行。对于 layout / header.php 也一样 - 它将首先被解析。

The answer to your question is that index.php will be parsed first and executed. Then when include "init.php" encountered the file init.php is parsed and executed within current scope. The same for layout/header.php - it will be parsed first.

如前所述 init.php 每次 include / require ,因此您可能希望使用 include_once require_once

As already noted init.php will be parsed and executed each time include / require is called, so you probably will want to use include_once or require_once.

这篇关于PHP解析包括的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆