The problem I'm facing is quite common I suppose but I didn't see a single thread in internet.
If I use isset($_POST)
,
true
?$_POST
is a variable? (it's a super global 'variable' after all). Because in php.net documentation, it is mentionedisset()
only works with variables as passing anything else will result in a parse error.
Will it always return true?
Yes, even if the page was opened using GET
method or nothing was POST
ed.
Does the response depend on the version of PHP I use?
No it does not (not sure about very old versions of PHP).
Is $_POST is a variable?
Yes
isset() only works with variables as passing anything else will result in a parse error.
This is explicitly mentioned in the manual so that people do not try to do cheaky stuff. These won't work for example:
function getVarName() { return '_POST'; }
isset(getVarName());
isset('$_POST');
Now, why would you want to check if $_POST
is set. Perhaps want to check if a certain variable (e.g. email) was posted, in that case you need to check:
isset($_POST["email"])
isset($_POST);
will always return true. If you want to check if it contains something use empty($_POST);
Does the response depend on the version of PHP I use?
No
Is $_POST is a variable ?
Yes
I would recommend using one of the following:
if($_POST){
}
if(!empty($_POST)){
}
1, Will it always return true?
isset($_POST) will always return true. If $_POST is empty it will return false.
2, Does the response depend on the version of PHP I use?
I'm not sure about versions below 4.* but the response has always been the same.
3, Is $_POST is a variable?
Yes, it is considered a superglobal like: $GLOBALS, $_SESSION, $_POST, $_GET
Whether or not you posted any data $_POST
will always be set as an array. What you are probably looking for is the empty()
method to see if any data was actually posted, like:
if(!empty($_POST)) {
// POST data was set
}
Yes it would always be true
No tested on PHP 4.3.0 - 5.4.10
$_POST
is a variable?Definitely Yes
Better way to validate $_POST
is to use empty
Isset()
return true only if it contains some value (it can be Zero 0). If it doesn't have any value then it returns false. If you want to prevent from (0) use
if(isset($_POST) && $_POST)
This will be true if only it has non-zero value
$_POST is global array
Response doesn't depend upon the version of PHP