douguio0185 2009-04-26 00:22
浏览 28
已采纳

PHP包括在函数中

I'm working on changing over a large, somewhat poorly written (but not terribly so), PHP website to use a class which stores functions which do things like write the standard page header with arguments to change things like the text of the header. The old version used a copied and pasted HTML header in each file with something like:

<div class="left"><?php include("left.php"); ?></div>

Where the included file has both non-echoed HTML and normal PHP. These includes seem ok to keep using, but I'm just not sure how they'll work, as to my knowledge, included files just replace the include() method directly.

Would it be better to:
-Re-write the included files to echo all standard HTML
-Simply move the content of these files into my new function where appropriate (to my knowledge, they're not used anywhere except where they would be in my new function)
-Something else

Thanks.

  • 写回答

2条回答 默认 最新

  • douxi3085 2009-04-26 00:34
    关注

    Read http://php.net/include:

    If the include occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function. So, it will follow the variable scope of that function. An exception to this rule are magic constants which are evaluated by the parser before the include occurs.

    When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags.

    PHP doesn't just copy and paste the code from the included file to replace the include statement, like C/C++ does. So you don't have to worry about the <?php and ?> tags in the included file messing up the file that is including it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程