我正在尝试开发一个网站.我希望将用PHP编写的网站的不同部分放在不同的文件中.例如“ posts.php”,“ recents.php”等.我正在尝试使用功能“ include”来包含目录“ / codes /”中的文件.但这是行不通的.
发生的事情是,如果我把在原始HTML页面的中间(该页面以PHP格式保存).添加的行之前的代码部分正常工作,但是添加的行之后的代码部分消失了.不知道为什么.我究竟做错了什么?
解决方法:
解决分号问题后,还应考虑:
If a path is defined — whether absolute (starting with a drive letter or \ on Windows, or / on Unix/Linux systems) or relative to the current directory (starting with . or ..) — the include_path will be ignored altogether. For example, if a filename begins with ../, the parser will look in the parent directory to find the requested file.
因此要么包含“ ./codes/posts.php”;或包含’codes / posts.php’,第一个版本相对于当前工作目录查找文件,第二个版本使用包含路径.
PS:最好使用require_once或include_once,以避免多次包含相同的函数/类定义,这会引起问题(重新声明函数)
标签:include,php,function
来源: https://codeday.me/bug/20191030/1968939.html