如何正确使用$_SERVER['DOCUMENT_ROOT']识别该路径的文件

echo $_SERVER['DOCUMENT_ROOT'];

这时输出当前文件所在的路径 D:/phpStudy/WWW/study/php&mysql 

 $_SERVER['DOCUMENT_ROOT']/orders.txt 这时候以为输出的路径为 D:/phpStudy/WWW/study/php&mysql/orders.txt 

但是通过echo 输出的为结果为:

Notice: Use of undefined constant orders - assumed 'orders' in D:phpStudyWWWstudyphp&mysql	est.php on line 9

Warning: Division by zero in D:phpStudyWWWstudyphp&mysql	est.php on line 9

Notice: Use of undefined constant txt - assumed 'txt' in D:phpStudyWWWstudyphp&mysql	est.php on line 9
txt

我们给 orders.txt 两边加上双引号试试,变成 

$_SERVER['DOCUMENT_ROOT'].”/orders.txt“ 

这时才能正确识别orders.txt的地址

 D:/phpStudy/WWW/study/php&mysql/orders.txt 

还有一种方式就是给 $_SERVER['DOCUMENT_ROOT'] 两边加上大括号,然后总的路径打上双引号

"{$_SERVER['DOCUMENT_ROOT']}/orders.txt"

这样也能正确输出orders.txt的地址

原文地址:https://www.cnblogs.com/jacson/p/4849221.html