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.

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!