PHP operator optimization If / Else

I am trying to optimize the following PHP If / Else statement. Can I rewrite the code to use caseand switch, or leave it as is, or what?

the 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();
    }
}

thank

+3
source share
6 answers

I hate switch instructions, but his personal preference is to be honest. As for further optimization, I would suggest taking a look at some assembly language. This will give you a general idea of ​​how to make conditional statements more efficient. That is, it will give you a different view of 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');
}
+2
source

MVC; . , , , , default.php include. , id = > file/value, .

if (isset($_GET['id'])) {
    $pages = array(
        0 => 'home.php',
        13 => 'servicestatus.php'
    );
    if (isset($pages[$_GET['id']])) {
        include('pages/' . $pages[$_GET['id']]);
    } else {
        include('pages/default.php');
    }
}
+2

, , swith

} elseif (!empty($_GET['id'])) {

}else{
+2

switch , $_GET['id'], .

, , ,

} elseif (!empty($_GET['id'])) {

} else {
+2

, , , if elseif,
.

switch ($_GET['id'])
{
  case 13: ... break;
  case 0 : ... break;
  default: ... break;
}
+1

, , , . , , , if-elseif-else -Statement

if (empty($_GET['id'])) { /* code */ }
elseif ($_GET['id'] === '13') { /* code */ }
elseif (!empty($_GET['id'])) { /* code* }

if (empty($_GET['id'])) { /* code */ }
elseif ($_GET['id'] === '13') { /* code */ }
else { /* code* }

if(!$rawdata) .

+1

Source: https://habr.com/ru/post/1777984/


All Articles