Home  >  Article  >  Backend Development  >  PHP文件引用的一个疑问,请帮忙看一下,谢谢

PHP文件引用的一个疑问,请帮忙看一下,谢谢

WBOY
WBOYOriginal
2016-06-23 14:22:47844browse

一个PHP疑问,请帮忙看一下,
我在index.php中定义一个常量
define('SCRIPT','index');
并引用文件title.php
require 'title.php';
在title.php中,我并没有引用index.php文件,但是也能正常打印出index.php中常量的值
echo SCRIPT; //结果index
请问下,被引用的php文件可以使用引用者php文件中的常量和变量吗?

例:index.php引用了title.php,title.php是不是就可以使用index.php中定义的常量和变量了?


回复讨论(解决方案)

可以的, 你注意先后顺序就可以了。 先定义,再使用

你如果直接浏览http://localhost/title.php,应该是打印不出index的,打印出来的值应该是SCRIPT

当时你如果浏览http://localhost/index.php.那是会被打印出来,值为index

其实引用我个人是单纯的理解把代码直接复制过来了!

require 'title.php';
之前定义的全局变量或常量,均可在title.php中访问

define('SCRIPT','index');

define定义全局常量,在任何地方都可以访问

试了下,“被引用页”可使用“引用页”里面定义的常量,不论是引用语句之前或之后。
但无法使用“引用页”里面的变量,不论是引用语句之前或之后。

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