Check if $_POST exists

To check if the $_POST superglobal variable exists in PHP, you can use the isset() function like this:

<?php

if (isset($_POST)) {
    // The $_POST variable exists
    echo 'The $_POST variable exists';
} else {
    // The $_POST variable does not exist
    echo 'The $_POST variable does not exist';
}

The $_POST superglobal variable is an array that contains variables passed to the current script via the HTTP POST method. It is used to collect form data after submitting an HTML form.

Watch a course Learn object oriented PHP

For example, if you have a form with an input field for the user's name, you can access the value of the input field using $_POST['name'].

<form action="submit.php" method="post">
  <label for="name">Name:</label><br>
  <input type="text" id="name" name="name"><br>
  <input type="submit" value="Submit">
</form>
<?php

if (isset($_POST['name'])) {
  $name = $_POST['name'];
  // Do something with the name
}

Note that the $_POST variable will only exist if the current script was requested via an HTTP POST request. If the script was requested via an HTTP GET request, $_POST will not exist.