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.