require_once与模板的问题 [英] require_once problem with templating

查看:138
本文介绍了require_once与模板的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有 master.php
$ p $ <?php require_once(header.php); $ c> ?>



master.php header.php 是在同一个文件夹中,但我得到以下错误:

lockquote

警告:require_once (1)[function.require-once]:未能打开流:没有这样的文件或目录在/Users/MyName/Sites/MySite/Views/master.php在第15行



致命错误:require_once()[function.require]:在第15行的/Users/MyName/Sites/MySite/Views/master.php中打开所需的'1'(include_path ='.:')失败p>

以前没有发生过。 (文件都在根文件夹中)。我没有看到问题是什么,因为它们在同一个文件夹中 - 我不必返回'../'或引用任何其他文件夹。我在这里错过了什么?



谢谢。

解决方案

注释你说你的代码是:

 <?php require_once(header.php)or die; ?> 

请尝试省略or die - > ist,因为require_once会触发致命的错误,如果文件没有找到。

编辑:
我测试了它,离开了或死,它将工作。



编辑2:解释为什么发生这种情况:
上面的代码也可以这样写:

  require_once((header.php)or die()); 

由于require_once不是函数,而是php中的语句,所以大括号是可选的,php解释上面是一个布尔表达式,其中字符串header.php被计算为布尔值true。 True被转换为一个字符串(导致为1),并且这个字符串被传递回来需要一次。

所以上面的代码被解释为


$ b $ pre $ require_once(1)


$ p $ ((header.php)or die($ ))=(TRUE或die())
(TRUE或die())= TRUE
(string)TRUE ='1';

希望这有帮助;)


Im new to MVC and I'm re-doing a lot of my code.

I have master.php (index) which has:

<?php require_once("header.php"); ?>

Both master.php and header.php are in the same folder yet I get the following error:

Warning: require_once(1) [function.require-once]: failed to open stream: No such file or directory in /Users/MyName/Sites/MySite/Views/master.php on line 15

Fatal error: require_once() [function.require]: Failed opening required '1' (include_path='.:') in /Users/MyName/Sites/MySite/Views/master.php on line 15

This did not happen before. (Files were all in root folder). I don't see what the problem is though, since they are in the same folder - I don't have to go back '../' or reference any other folders. What am I missing here?

Thanks.

解决方案

In your comment you say your code is:

<?php require_once("header.php") or die; ?>

please try leaving out the "or die" -> ist is not necessary, because require_once will trigger a fatal error if the file is not found.

Edit: I tested it, leave out the "or die" and it will work.

Edit 2: Explanation why this happened: The above code can also be written like this:

require_once ( ("header.php") or die() );

Because require_once is not a function, but a statement in php, the braces are optional, php interprets the above as a boolean expression, where the string "header.php" gets evaluated to the boolean "true". True gets cast to a string (resulting in "1"), and this string gets passed back to require once.

So the above code gets interpreted as

require_once("1")

Here is how it gets interpreted step by step:

(("header.php") or die()) = (TRUE or die())
(TRUE or die()) = TRUE 
(string) TRUE = '1';

hope this helps ;)

这篇关于require_once与模板的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆