dsf12123 2014-04-13 04:39
浏览 116
已采纳

php“require”和“includes”方法是否每次都会占用内存/时间/性能?

I have 30 html forms on a single page and I use ajax for 25 of those forms. All 30 of the forms can still be posted even if the user has javascript disabled since it will just use PHP and not ajax.

For example, my PHP code for the page looks something like this

if (!empty($_POST)) {
    require '/database-connect-file.php';
    require '/functions.php';
    $mysqli = connectToDatabase();
    require '/what-form-if-else-statements.php';
}

The functions.php page has 400 lines of code.

Will PHP have to process all 500 lines of code in the if statement that barely ever gets run since most users do have javascript enabled? Does PHP have to interpret the required files each and every time the page is loaded, even if there is empty post variable normally?

  • 写回答

1条回答 默认 最新

  • douxia2053 2014-04-13 04:47
    关注

    PHP will only execute the require directives and other lines of code in that block, if the expression in the if statement evaluates to TRUE.

    As described in the section about expressions, expression is evaluated to its Boolean value. If expression evaluates to TRUE, PHP will execute statement, and if it evaluates to FALSE - it'll ignore it.

    http://www.php.net/manual/en/control-structures.if.php

    Although, !empty($_POST) may not work if you are using POST with AJAX and method="POST" on your forms. PHP will not know the difference between AJAX and a form submit if both are using the POST method.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题