isset() evaluates to true even when the textfields are empty. Why is that?

isset, php

Solution

Because they both are set - the variables exist in the `$_GET` array. Even if their values are empty strings.

Try to check for emtpiness as well

 if( isset($_GET['text_first']) && $_GET['text_first'] !== '' ) 

or

if ( ! empty( $_GET['text_first'] ) ) {

Note that you don't need to use `isset()` because `empty()` does not generate a warning if the variable does not exist.

Problem

The first snippet takes data from the two text fields and sends to `action script.php`. The problem is both the `if` statements evaluate to true even if I do not enter anything in the text fields. Why is that ? ``` try.php <form method='get' action='./action_script.php'> <input type="text" id="text_first" name="text_first" /> <br /> <input type="text" id="text_second" name="text_second"/> <br /> <input type="submit" id="submit" /> </form> ``` ``` action_script.php <?php if(isset($_GET['text_first'])) { echo "Data from the first text field : {$_GET['text_first']} <br>"; } if(isset($_GET['text_second'])) { echo "Data from the second text field : {$_GET['text_second']} <br>"; } echo "After the if statement <br />"; ```

Original source