Why does isset not work well with $_POST?

Asked

Viewed 862 times

0

I need the title of the page to vary according to the form, for this I used the code within the <head>:

<?php
    $titulo = isset($_POST['titulo']) ? $_POST['titulo'] : 'Sem titulo';
?>
<title><?php echo $titulo." - Minimalist";?></title>

It works well if I add the title in the form, but does not return the 'Untitled' when it is empty. This worked perfectly with the $_GET. Actually, by using the $_POST, he returns a string empty?

  • exact.. to take the test do print_r($_POST)

  • http://answall.com/a/90026/4793

  • 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 return false, but if you send a form, both in get and post, even if you leave the field blank, they will have the variables defined as NULL, in this case the isset will return true...

  • 1

    @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.

  • @Jadera.Wagner yes, friend, it worked by omitting from the URL.

  • 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.

  • 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 the empty() does not perform the function of isset(). And that’s the problem here. It may even work but it’s bad practice.

  • @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. The empty in this case is valid as AP wants to check if the variable is empty.

  • If you want to feel safer you can do something like this: isset($_POST['titulo']) && !empty($_POST['titulo']) but it’s kind of redundant. ;-)

  • 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.

  • @Danielomine Ai is a case to use the isset. =) I updated the answer and put another alternative as well.

Show 6 more comments

1 answer

2


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:

  • 1

    It worked, but correct Empty’s order, it’s changed.

  • 1

    @Danielbonifácio Corrected!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.