The isset
checks if a variable has been defined/initialized, it is not checked if it has a value.
The isset
will always return true
for $_POST
because it will always be set, but it may be empty, which is probably the source of your problem.
See the documentation of $_POST
:
This is a superglobal, or automatic, variable global. This
simply means that it is available in all scopes by
script.
To check if the variable is empty, use the empty
:
$titulo = empty($_POST['titulo']) ? 'Sem titulo' : $_POST['titulo'];
Similar to isset
, the empty
also checks whether the variable was initialized, but does not issue a warning. What is considered empty by empty
:
""
(one string empty)
0
(0 as a whole)
0.0
(0 as a floating point)
"0"
(0 as a string)
NULL
FALSE
array()
(one array
emptiness)
$var;
(a declared variable but no value)
According to the documentation, the empty
is essentially equivalent to !isset($var) || $var == false
.
Another alternative is to use the isset
and a condition to check if variable is not empty:
$titulo = isset($_POST['titulo']) && $_POST['titulo'] !== '' ?
$_POST['titulo'] : 'Sem titulo';
See also:
exact.. to take the test do
print_r($_POST)
– Daniel Omine
http://answall.com/a/90026/4793
– Daniel Omine
You say that in
$_GET
worked, but it depends on how you were feeding the$_GET
, simply remove/omit the variable from the href link, this right,isset
will returnfalse
, but if you send a form, both in get and post, even if you leave the field blank, they will have the variables defined asNULL
, in this case theisset
will returntrue
...– Jader A. Wagner
@Danielomine I think it is not a duplicate, that is about validating the data received from the form, this however, focuses on the
isset
and$_POST
specifically.– stderr
@Jadera.Wagner yes, friend, it worked by omitting from the URL.
– Daniel Bonifácio
In this other that marked as duplicate shows what you ask here and more essential details, including, when reading can understand the reason for the negative in the reply.
– Daniel Omine
Ahh.. not to be too vague, in parts it is certain what answered, but not always the post will bring the specified index. If the index does not exist, when entering the
empty()
, will issue index Undefined error as theempty()
does not perform the function ofisset()
. And that’s the problem here. It may even work but it’s bad practice.– Daniel Omine
@Danielomine Actually does yes, but does not issue the error if the variable is not set, only returns phony. The
empty
internally does this:!isset($foo) || !$foo
. Behold here and here for details. Theempty
in this case is valid as AP wants to check if the variable is empty.– stderr
If you want to feel safer you can do something like this:
isset($_POST['titulo']) && !empty($_POST['titulo'])
but it’s kind of redundant. ;-)– stderr
The problem itself is not redundancy. It is consistency. For like the
empty()
returns the same value for both cases (null, empty or Undefined returns true), makes the result ambiguous. Suppose you want to receive a variable. It is mandatory but can be empty. So what? rsrsr It seems strange but there are such situations and they are common. Hence the conception that the use of this form is a bad practice. Actually a bad practice "dirty" allowed by PHP.– Daniel Omine
@Danielomine Ai is a case to use the
isset
. =) I updated the answer and put another alternative as well.– stderr