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

我在一个页面上有30个html表单,我使用ajax表示25个表单。 即使用户已禁用javascript,所有30个表单仍然可以发布,因为它只使用PHP而不是ajax。</ p>

例如,我的页面的PHP代码看起来像这样 </ p>

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

nn

functions .php </ code>页面有400行代码。</ p>

PHP必须处理几乎没有运行的if语句中的所有500行代码,因为大多数用户都启用了javascript ? PHP是否必须在每次加载页面时解释所需的文件,即使通常有空的post变量?</ p>
</ div>

展开原文

原文

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个回答



PHP只会在该块中执行 require </ code>指令和其他代码行,如果表达式在 if </ code>语句求值为 TRUE </ code>。</ p>


如关于表达式的部分所述,表达式将被计算为
它的布尔值。 如果expression的计算结果为TRUE,那么PHP将执行语句,如果计算结果为FALSE,它将忽略它。</ p>
</ blockquote>

http://www.php.net/manual/en/control-structures.if.php </ a > </ p>

尽管!empty($ _ POST)</ code>可能无效,如果您使用 POST </ code>与AJAX 和</ 表单上的em> method =“POST”</ code>。 如果两者都使用POST方法,PHP将不知道AJAX和表单提交之间的区别。</ p>
</ div>

展开原文

原文

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.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐