首页 > 解决方案 > 使用函数中包含的函数和变量

问题描述

我想在函数的包含文件中使用在另一个 php 文件中定义的变量。

我有这样的事情:

/* INDEX.PHP */
require('main.php');
require('utils.php');

$error = ([condition]);

if ($error) {
    error_404();
}

require('page.php'); //The normal content of the page, replaced by error404.php in case of error


/* UTILS.PHP */
function error_404() {
    header('HTTP/1.0 404 Not Found');
    include('error404.php'); //This file needs the 'main.php' required outside the function.
    exit();
}

函数包含的error404.php文件error_404()需要使用函数main.php外包含的文件中声明的函数和变量。

我需要“将包含移动main.php到功能环境”之类的东西。有谁知道我该如何解决这个问题?

标签: php

解决方案


推荐阅读