If/else that contains HTML blocks, which contain additional PHP functions I have a form that posts (a,b) values into the following .php file, and displays an error message when the user tries to reach the .php file directly, instead via POST request.

This code is probably pretty messy, for example, I'm not sure I can post &lt;p&gt; tags right away, instead of having them inside the &lt;html&gt; tags, but since it's an if/else function, I'm not sure how to build this in the most efficient way.

&lt;?php
    if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
?&gt;
    &lt;p&gt;You didn't come here through a Post.&lt;/p&gt;
&lt;?php
    } else {

if(isset($_POST['a'])){

    switch ($_POST['a']) {

    case "1":
        $var1 = "word1";
        break;

    case "2":
        $var1 = "word2";
        break;

    default:
        $var1 = "other";
    }
}

if(isset($_POST['b'])){

    switch ($_POST['b']) {

    case "1":
        $var2 = "word3";
        break;

    case "2":
        $var2 = "word4";
        break;

    default:
        $var2 = "other";
    }
}

?&gt;  
&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;test&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
&lt;style&gt;
#various-styles {
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div&gt;
HTML-blocks that contain PHP such as &lt;?php echo $var1; ?&gt;, and &lt;?php echo $var2; ?&gt;.
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;?php
    }
?&gt;
A few suggestions.


Instead of writing styles in same file use an external style sheet
Also you can restructure it like:

 &lt;!doctype html&gt;
 &lt;html&gt;
 &lt;head&gt;
 &lt;title&gt;test&lt;/title&gt;
 &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
 &lt;style&gt;
 #various-styles {
 }
 &lt;/style&gt;
 &lt;/head&gt;
 &lt;body&gt;
 &lt;div&gt;
 &lt;?php
     if ($_SERVER['REQUEST_METHOD'] !== 'POST')
     {
 ?&gt;
 &lt;p&gt;You didn't come here through a Post.&lt;/p&gt;
&lt;?php
     }
     else
     {
         if(isset($_POST['a']))
         {
            $status = "HTML-blocks that contain PHP such as ";
            switch ($_POST['a'])
            {
                case "1":
                  $status .= "word1";
                break;
                case "2":
                  $status .= "word2";
                break;
                default:
                  $status .= "other";
            }
        }

        if(isset($_POST['b']))
        {
            $status .= "and ";
            switch ($_POST['b'])
            {
                case "1":
                  $status .= "word3";
                break;
                case "2":
                  $status .= "word4";
                break;
                default:
                  $status .= "other";
            }
        }
        echo $status;
 ?&gt;
 &lt;!-- other html code else block here --&gt;
 &lt;?php
    }
 ?&gt;

 &lt;/div&gt;
 &lt;/body&gt;
 &lt;/html&gt;