下面的代码可确保当用户访问控制面板时,他们会通过快速验证过程来验证其实体是什么。例如,如果用户是1级,他们只能访问视频源,这意味着他们没有其他任何东西可用。PHP案例开关(效率)
当我查看代码时,我可以看到调用情况1和3时的视频馈送。我可能会喜欢另一种方法来提高代码的效率。
我被告知一个可能的数组可能会让事情变得更容易一些,但是这样会更快。
switch ($_SESSION['permission']) {
case 1: // Level 1: Video Feed
include ("include/panels/videofeed.index.php");
break;
case 2: // Level 2: Announcements/Courses/Teachers
include ("include/panels/announcements.index.php");
include ("include/panels/courses.index.php");
include ("include/panels/teachers.index.php");
break;
case 3: // Level 3: Announcements/Video Feed/Courses/Teachers/Accounts/Logs
include ("include/panels/announcements.index.php");
include ("include/panels/videofeed.index.php");
include ("include/panels/courses.index.php");
include ("include/panels/teachers.index.php");
include ("include/panels/accounts.index.php");
include ("include/panels/log.index.php");
break;
case 4: // Level 4: Teachers
include ("include/panels/teachers.index.php");
}
当您需要使混淆代码赢得0.0000001sec时,情况并非如此。每个开发人员都应该编写易于阅读的代码,因此 - 易于优化(但不像您所做的那样)。 – zerkms 2011-02-08 02:27:46
我认为这看起来相当可接受,对于电脑来说它肯定不是*效率低下的。你也许可以随便改变一些代码,但似乎并不能从根本上改变很多。 – deceze 2011-02-08 02:27:49
你应该停止使用幻数并定义一些描述权限级别的常量。例如`PERMISSION_VIDEO_FEED“。 – deceze 2011-02-08 02:31:28