PHP何时访问网站时运行?

I have a quick question about PHP, say I am creating a form and there is a code snippet as follows:

<?php if($_POST){ do something here }else{ do something else } ?>

When does this script run? I am more familiar with python and java, where code runs line by line, a brief explanation would be appreciated, Thanks!

The script runs whenever the browser sends a request to the server with the URL scriptname.php. This is no different from if you use python or java to implement your server-side coding.

If the user simply goes to the URL via a link or entering it into the browser's address bar, there will not be any POST parameters. $_POST will be empty, and the do something here block of code will be executed.

But if the script is used as the action URL of an HTML form, the inputs will be in $_POST, and the do something else block will be executed.

This allows the same script to be used to display a form and also process the form data when the user submits it. This is a pretty common idiom in PHP webpage design.

One of the unique things about PHP is that it's embedded within a page of ordinary text (usually HTML, but it can actually be anything). When PHP is processing the file, anything that's outside <?php ... ?> is simply sent directly to the client. When it encounters <?php, it starts executing it as a script, until it reaches ?>. So if you have a PHP file like this:

<html>
<body>
<?php echo "This is some text"; ?>
</body>
</html>

it will put This is some text in the body of the resulting HTML document.