当我们在HTML模式下出局时,php是如何工作的?

i am a bit confused when i use conditional statements like

<?php
    if(isset($_POST['somevalue'])){

?>

<h1> this is out side php mode now </h1>

<?php

}else {

?>

<h1> again its out php mode </h1>

<?php
}

?>

but yet it still works i mean if $_POST['somevalue'] is set then it outputs "this is out side php mode now" if not it outputs "again its out php mode" my question is if i am outside php mode how does it work then?

I think your question is "how PHP works".as we know php is a server side language.it executes in the server but the scope of the html code will be inside the if loop.so

<?php
    if(isset($_POST['somevalue'])){

?>

will be evaluated in server and not in the html part and that will be either true or false. so after the execution in server your code in front end i.e. in the html part will be like this

 <?php
        if(1){

    ?>
<h1> this is out side php mode now </h1>
//as above code is markup language so it will be interpreted by the browser

<?php

}else {

?>

<h1> again its out php mode </h1>

<?php
}

?>

NOTE: the delimiters is for the server to know that the code inside the tag is php code and it will execute it accordingly.

enter image description here

Although you have closed off the executable section of the PHP code, the surrounding if statement and the curly braces will actually have a higher priority as to what is executed and what isn't.

<?php
if
{
    // This is considered inside the statement 
    // and will only be sent if the execution 
    // makes it inside the statement.
    ?>
    ...
    <?php
}
else
{

}
?>
// Anything here is simply sent to the browser
// as it will always executed.
<?php

// more code etc

?>

Anything inside the IF statement is considered part of the IF - even if it contains close/open PHP tags.

Basically the PHP control structure overrides open/close tags. This means any sort of if, switch, function etc etc has a higher priority than open close tags.

That's one thing I love about php. Initially, the main reason is as @Fluffeh mentioned that you are still within the "if" statement.

One way I could put it is that, PHP allows to be embeded in side HTML code. As long as the file has a .php extention then (someone correct me if I'm wrong) Apache knows to use the PHP processor to process that file. It will process the php coding and display the HTML sections in it as well.

Your question is some what similar to

<?php
$name = "Tom";
?>

<h1>Hello <?php echo $name;?>!</h1>

The result will come out as Hello Tom!

A PHP file is processed as plain text/html until it reaches a <?php tag when it executes the php code. When it reaches the closing ?> it processes it as plain text again.

This is exactly the same, but with echo and quote marks around the html you want to print. If you're having trouble reading the code I suggest indenting as needed.

<?php
    if(isset($_POST['somevalue'])){
        echo "<h1> this is out side php mode now </h1>";
    }else {
        echo "<h1> again its out php mode </h1>";
    }
?>

The way you posted it.

<?php
    if(isset($_POST['somevalue'])){
        ?><h1> this is out side php mode now </h1><?php
    }else{
        ?><h1> again its out php mode </h1><?php
    }
?>

it is the same idea as it is inside the php.. satisfy each of the condition and it will run its corresponding statement..

this is one of the best features of php, which allow us to use native html codes rather than putting it inside echo.