How can PHP_SELF be used to process form submissions?

When processing form submissions in PHP, the PHP_SELF variable can be used in the form action attribute to submit the form data to the same PHP script that loaded the form. This allows the same script to handle both the display of the form and the processing of the form data.

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <!-- Form fields go here -->
  <input type="submit" name="submit" value="Submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  // Process form data here
}