5

In various PHP tutorials I see this syntax -

if ($_POST) {
  do something
}

I want to know whether this is equivalent to either isset or !(empty) (either one) or has different properties.

6 Answers 6

5

It attempts to evaluate the expression and cast it to boolean.

See 'Converting to boolean' at http://php.net/manual/en/language.types.boolean.php to see which values will be equivalent to true and which to false.

It does NOT however check for array key existence (i.e. isset), so if you try if ($_GET['somekey']) but somekey does not exist, you will see PHP Notice: Undefined index: somekey and then false will be assumed.

The best practice would be to perform empty() or isset() checks manually first as fits.

Sign up to request clarification or add additional context in comments.

Comments

2

Good question. You are adressing one of PHPs dark sides if you ask me.

The if statement Like in any other language I can imagine if evaluates the parameter to either true or false.

Since PHP doesn't really know types you could put any expression as parameter which will then be casted to bool as a whole

Following values are considered to be "FALSE"

  • boolean FALSE
  • integer 0
  • float 0.0
  • empty string
  • string "0"
  • any array with zero elements
  • NULL e.g. unset variables or $var = null
  • SimpleXML objects when created from empty tags

EVERY other value or expression result is casted to bool TRUE

Now, knowing this, all we need to find out is, what an expression or function returns when executed

If no POST data is set, the following expression would be TRUE

$_POST == FALSE

The isset function

isset returns bool TRUE when the given variable is set and not null.

parameters can be variables, array elements, string offsets and data members of objects.

In PHP 5.4 they fixed the behaviour with string offsets

$var = FALSE;

isset( $var ) === TRUE;
$var === FALSE;

More here https://www.php.net/manual/en/function.isset.php

The empty function

Returns false when a variable is considered to be empty or does not exist.

Those values are considered empty:

Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.

The following values are considered to be empty:

  • "" (empty string)
  • 0 (integer)
  • 0.0 (float)
  • "0" (string)
  • NULL
  • FALSE
  • array() (empty array)
  • Also declared variables without value are empty

compare table

$var = FALSE;

isset($var) === TRUE;
empty($var) === TRUE;
$var === FALSE;

Comments

2
if ($_POST)

This will evaluate to true if there are any elements in the POST array.

if(isset($_POST))

This will always evaluate to true because the POST array is always set, but may or may not contain elements, therefore it is not equivalent to the first example.

if(!empty($_POST))

This however, is equivalent to the first example because empty() checks for contents in the array.

A good generic way of testing if the page was posted to is:

if($_SERVER['REQUEST_METHOD'] == 'POST')

1 Comment

In addition, for any server with php notice error on, please use if (@$_POST)
1

$_POST: this is used to find whether data is passed on using HTTP POST method and also extracting the variables sent through the same which are collected in an associative array

isset: checks whether a variable is set(defined) or is NULL(undefined)

Comments

0

PHP.net is an invaluable source of information for figuring out the intricacies and quirks of the language.

With that said, those are not equivalent. The if statement converts the expression to a boolean value (see here for information on what is considered false).

isset is used to "determine if a variable is set and is not NULL."

empty determines whether a variable is empty, i.e., "it does not exist or if its value equals FALSE."

Comments

0

Best practice if you want to check the value of a variable but you aren't sure whether it is set is to do the the following:

if(isset($var) && $var) { ... }

ie check isset() AND then check the variable value itself.

The reason for this is that if you just check the variable itself, as per the example in your question, PHP will throw a warning if the variable is not set. A warning message is not a fatal error, and the message text can be suppressed, but it's generally best practice to write code in such a way that it doesn't throw any warnings.

Calling isset() will only tell you whether a variable is set; it won't tell you anything about the value of the variable, so you can't rely on it alone.

Hope that helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.