这是index.php下的代码:
<?php
defined('admin') or define('admin', true);
echo admin;
echo "\n";
defined('root_path') or define('root_path', __DIR__ . '/');
echo root_path;
echo "\n";
defined('lib') or define('lib', root_path . 'lib/');
echo root_path;
echo "\n";
运行无报错
输出结果如下:
1
E:\php\huaiyouDome/
E:\php\huaiyouDome/
但是换一个php文件输出就会报错 例如Test.php下输出会报错:
PHP Fatal error: Uncaught Error: Undefined constant "admin" in E:\php\huaiyouDome\View\Test.php:2
我就不理解,因为看朋友在写代码的时候明明是可以的,而且变量还可以跨页使用,自己写的时候就不行。
IDE: phpstorm 2021.2.3
PHP:8.0