优化 PHP If/Else 语句
我正在尝试优化以下 PHP If/Else 语句。我可以重写代码以使用 case
和 switch
,还是应该保持原样,或者什么?
代码:
if(empty($_GET['id'])){
include('pages/home.php');
}elseif ($_GET['id'] === '13') {
include('pages/servicestatus.php');
}elseif(!empty($_GET['id'])){
$rawdata = fetch_article($db->real_escape_string($_GET['id']));
if(!$rawdata){
$title = "";
$meta['keywords'] = "";
$meta['description'] = "";
}else{
$title = stripslashes($rawdata['title']);
$meta['keywords'] = stripslashes($rawdata['htmlkeywords']);
$meta['description'] = stripslashes($rawdata['htmldesc']);
$subs = stripslashes($rawdata['subs']);
$pagecontent = "<article>" . stripslashes($rawdata['content']) . "</article>";
}
include("includes/header.php");
echo $pagecontent;
if(!$rawdata){
error_404();
}
}
谢谢
I'm attempting to optimise the following PHP If/Else statement. Could I rewrite the code to make use to case
and switch
, or should I leave it as it is, or what?
Code:
if(empty($_GET['id'])){
include('pages/home.php');
}elseif ($_GET['id'] === '13') {
include('pages/servicestatus.php');
}elseif(!empty($_GET['id'])){
$rawdata = fetch_article($db->real_escape_string($_GET['id']));
if(!$rawdata){
$title = "";
$meta['keywords'] = "";
$meta['description'] = "";
}else{
$title = stripslashes($rawdata['title']);
$meta['keywords'] = stripslashes($rawdata['htmlkeywords']);
$meta['description'] = stripslashes($rawdata['htmldesc']);
$subs = stripslashes($rawdata['subs']);
$pagecontent = "<article>" . stripslashes($rawdata['content']) . "</article>";
}
include("includes/header.php");
echo $pagecontent;
if(!$rawdata){
error_404();
}
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我讨厌 switch 语句,但说实话,这是我个人的偏好。至于进一步优化,我建议看看某种形式的汇编语言。它将为您提供一些关于如何使条件语句更有效的一般想法。也就是说,它会让你对事物有不同的看法。
I hate switch statements, but its personal preference to be honest. As far as further optimization i'd suggest taking a look at some form of assembly language. It will give you some general ideas on how to make conditional statements more efficient. That is, it will give you a different out look on things.
如果您要检查的
$_GET['id']
有多个离散值,则switch
是合适的。为了可读性,我可以提出的一个建议是,
只需要
switch
would be appropriate if you had several discrete values for$_GET['id']
that you were checking for.One suggestion I can make for the sake of readability is that
only needs to be
好吧,我认为没有必要切换到开关
但你可以
改为
Well i don't think it's necessary to switch to a swith
but you could change
to just
您可能想要考虑将代码分解为 MVC 形式;这将使维护代码变得更加容易。至少将最后一个子句放入另一个文件中,可能名为
default.php
并include
它。另外,您可以创建一个 id => 的数组文件键/值集,查找 id,并包含该文件。You may want to look into breaking up your code into a MVC form; that would make it much easier to maintain your code. At least put the last clause into another file, probably called
default.php
andinclude
it. Also, you might create an array of id => file key/value sets, lookup the id, and include the file.是的,switch 只计算一次,比
if elseif
高效,并且使用这个给定的结构更容易维护
Yes, switch is evaluate once, is efficient than
if elseif
,and is easier to maintain with this given structure
我不知道你是否应该这样做,但在这里我不会。主要原因是,至少有一个语句,您可以省略,然后,您将只有一个
if-elseif-else
- 语句相同
与之后的块中 ,语句
if(!$rawdata)
也是重复的。I dont know, if you should, or should not, but here I wouldnt. The main reason is, that there is at least one statement, you can omit, and then, you will have just a
if-elseif-else
-Statementis the same as
In the block after that, the statement
if(!$rawdata)
is also duplicated.