简体   繁体   English

如果还要使用其他语句?

[英]How many else if statements should we use?

I have a PHP file ajax.php where i always send my ajax to. 我有一个PHP文件ajax.php ,我总是将我的ajax发送到该文件。 There are about 50 else if statements in the code. 代码中大约有50个if语句。 I always pass a POST Parameter cmd to my ajax.php file and check if the variable is set and execute different code depending on the variable content. 我总是将POST参数cmd传递给我的ajax.php文件,并检查变量是否已设置,并根据变量内容执行不同的代码。

I was wondering if i should rather split my ajax.php to multiple files to reduce the amount of else if statements and increase performance. 我想知道我是否应该将ajax.php拆分为多个文件,以减少else if语句的数量并提高性能。 But how many else if are we recommended to use and does it have a big impact on performance if we use eg 10000? 但是, else if我们建议使用10000个,那么我们建议使用多少个?它对性能有很大影响吗?

I don't think performance would be a problem with 50 or 100 different commands. 我不认为使用50或100个不同的命令会带来性能问题。 But what i would suggest is to use switch case and functions, like this 但是我建议的是使用开关盒和功能,像这样

$cmd = $_POST['cmd'];

switch ($cmd){
    case "command1":
        fnCommand1();
        break;
    case "command2":
        fnCommand2();
        break;
}

function fnCommand1(){
    //do command 1
}

function fnCommand2(){
    //do command 2
}

It would be easier to read and more manageable in case you decide to split it into files, 如果您决定将其拆分为文件,将更易于阅读和管理。

You can use key value pattern in php. 您可以在php中使用键值模式。 Make a array with your post paramaters as key and other as s values. 用您的post参数作为键,其他作为s值创建一个数组。 Whenever the paramater will come to ajax.php check for the key using array_key_exist() Array key value and Array 每当参数进入ajax.php ,使用array_key_exist() 数组键值数组检查键

i think that is barely insane to write 10k elseif by yourself, i would prefer to crack my head and find a valid algorithm, or use a switch statement instead. 我认为自己写10k elseif几乎是疯狂的,我宁愿crack之以鼻,找到一个有效的算法,或者改用switch语句。

Btw, try always to avoid this kind of situation, doesn't really matter if is a single file or 1000 files, the processing time will always be the same 顺便说一句,请始终避免这种情况,是单个文件还是1000个文件都没有关系,处理时间将始终相同

Separating file can not benefit performance but human readable and maintainable. 分隔文件不能提高性能,但易于阅读和维护。

If your code is something like: 如果您的代码是这样的:

if($_POST["cmd"]=="a"){
//case a
}else if($_POST["cmd"]=="a"){
//case b

For performance, you can change it to switch case. 为了提高性能,您可以将其更改为开关盒。 But if statement optimization always not performance-critical. 但是,如果语句优化始终不是性能至关重要的。

PHP doesnt have performance issues with some many lines but people do. PHP在很多方面都没有性能问题,但是人们确实有。 Maybe you can read the code because you remember it now. 也许您可以阅读代码,因为您现在可以记住它。 But if your somebody joins your team he will be lost and probably wont join because of that. 但是,如果您的某人加入您的团队,他将会迷失并且可能不会因此而加入。 Or if you came back to this script after a year would you be able to read it, add or delete something? 或者,如果您在一年后返回此脚本,是否可以阅读,添加或删除某些内容?

Every script or class schould do one thing or work with one entity. 每个脚本或类都应该做一件事情或与一个实体一起工作。 There are many approaches how to write better code. 有许多方法可以编写更好的代码。

single responsibilty principle 单一责任原则

Object oriented Programming 面向对象编程

and many more 还有很多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM