Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP If Statement if (!$_POST) - What does !$_POST mean?

Tags:

php

PHP If Statement if (!$_POST) - What does !$_POST mean?

For instance, in some legacy code I'm looking at, the code reads:

    <form action="paypalorders.php" method="POST">
         <input type="hidden" name="orderList" value="' . $orderList . '">
         <input type="submit" value="Archive">
    </form>';
if (!$_POST) {
    file_put_contents('/orders.txt', $output_line1);
}

I've looked at a bunch of other threads and haven't seen this exact question asked, so I'm trying to find out what it is. I've seen threads that format it like this:

if(!empty($_POST)) {

But not quite the same as what I'm looking for. Is it the same thing, just shorthand? I'm not sure, which is why I'm asking. I've Googled around and looked at a handful of threads and I'm still not sure.

Thank you.

like image 861
James Anderson Avatar asked Aug 15 '18 21:08

James Anderson


People also ask

What does $_ POST do in PHP?

PHP $_POST is a PHP super global variable which is used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables. The example below shows a form with an input field and a submit button.

What is $_ POST and $_ GET?

$_GET, and $_POST are array variables of PHP which are used to read submitted data by HTML form using the get and post method accordingly.

How do you check if there is a POST in PHP?

Check if $_POST Exists With isset() The isset() function is a PHP built-in function that can check if a variable is set, and not NULL. Also, it works on arrays and array-key values. PHP $_POST contains array-key values, so, isset() can work on it. To check if $_POST exists, pass it as a value to the isset() function.

What is the difference between $post and $_ POST?

$_POST is a superglobal whereas $POST appears to be somebody forgetting the underscore. It could also be a standard variable but more than likely it's a mistake.


3 Answers

The ! (not) logical operator returns true if the argument on its right-hand side is not true. It forces the argument to be evaluated as a boolean. In this case, when the $_POST array is evaluated as a boolean it will evaluate as true if it is not empty and false if it is. (See converting to boolean.)

if (!$_POST) { should be a safe way to detect whether or not anything is in $_POST if you want to do that. empty isn't necessary in that case because superglobals are always set, and since you aren't referring to a specific key, you don't need to worry about an undefined index notice.


I think it's also worth mentioning that if the only point of the check is to see what type of request was sent, it is probably better to just check the request method directly, because !$_POST does not mean the request wasn't a post, since a post request can be empty.

like image 185
Don't Panic Avatar answered Sep 21 '22 03:09

Don't Panic


Since $_POST is an array, if it's empty his value is null, so if(!$_POST) would look like this:

if(!null){
  //code 
}

The following code returns true or false, but the objective of both is the same.

if(!empty($_POST)){
  //code
}

Hope it helps you!

like image 30
fricardi Avatar answered Sep 20 '22 03:09

fricardi


(bool)$array evaluates to true if $array contains elements, and false if it is empty.

Since $_POST is an array, !$_POST returns true if $_POST is empty.

Another way to interpret this, you are performing conditional tasks for the case where this page was not reached through a HTTP POST method.

like image 35
Havenard Avatar answered Sep 20 '22 03:09

Havenard