optimization - Optimising a PHP If/Else statement -
i'm attempting optimise following php if/else statement. rewrite code make use case
, switch
, or should leave 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
i hate switch statements, personal preference honest. far further optimization i'd suggest taking @ form of assembly language. give general ideas on how make conditional statements more efficient. is, give different out on things.
if(!empty($_get['id'])) { if($_get['id'] == '13') { include('pages/servicestatus.php'); } else { $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(); } } } else { include('pages/home.php'); }
Comments
Post a Comment