Skip to content

Check if $_POST exists

To check if the $_POST superglobal variable contains data in PHP, you can use the !empty() function. Note that $_POST is always defined as an array, even when empty.

How to Check if $_POST contains data in PHP?

php
<?php

if (!empty($_POST)) {
    // POST data was sent
    echo 'POST data exists';
} else {
    // No POST data was sent
    echo 'No POST data was sent';
}

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.

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'].

Example of an HTML form

html
<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>

How to access the value of the input field using $_POST in PHP?

php
<?php

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

Note that the $_POST variable is always defined as an empty array, regardless of the request method. It only contains data when the current script is requested via an HTTP POST request.

Dual-run preview — compare with live Symfony routes.