-7

I have some questions about isset() function.

isset() function checks if variable is set or not set or in other words it checks if value of variable is not NULL.

But what if I do something like this:

<?php 
  isset($var);
?>

What happens when I use isset() function on a variable that doesnt exist/isnt declared/isnt defined or whatever you call that?

I am asking because I am writing some code

<?php
function renderForm($firstName = '', $lastName = '' , $error = '', $id = ''){
?>
    <div id='recordsForm'>
        <h1><?php if($id != '' ){ echo "Edit Record"; } else { echo 'Create New Record'; } ?></h1>
        <?php if($error != ''){ echo $error; } ?>
        <form action='records.php' method='POST'>
            <?php 
                if($id != ''){
                    ?>
                        <input type='hidden' name='id' value='<?php echo $id; ?>'> 
                    <?php
                    echo "<h3>Record ID: {$id}</h3>";
                }
                echo "First Name: <input type='text' name='firstname' value='".$firstName."' />";
                echo "<br>";
                echo "Last Name: <input type='text' name='lastname' value='".$lastName."' /> ";
                echo "<br>";
                echo "<input type='submit' name='submit' value='submit' />";
            ?>
        </form>
    </div>

<?php
}
if(isset($_GET['id']) && is_numeric($_GET['id'])){
    // edit record
    renderForm(NULL,NULL,NULL,$_GET['id']);
} else {
    // add new record
    if(isset($_POST['submit'])){
        // Do some form processing stuff
    } else {
        renderform();
    }
}
?>
 ?>

As you can see I wrote isset($_POST['submit']) even if $_POST['submit'] doesnt exist since i didnt called renderForm() function.

So does this means that isset() only checks if variable is not null even if variable doesnt exist like in my case?

I hope i didnt confused you :D

  • no idea what you are asking –  Jan 22 '18 at 21:20
  • 1
    "What happens when I use isset() function on a variable that doesnt exist/isnt declared/isnt defined or whatever you call that?" It will return `false`. – ceejayoz Jan 22 '18 at 21:20
  • 1
    This didn't explain it??? http://php.net/manual/en/function.isset.php – AbraCadaver Jan 22 '18 at 21:25
  • 1
    Possible duplicate of [Best way to test for a variable's existence in PHP; isset() is clearly broken](https://stackoverflow.com/questions/418066/best-way-to-test-for-a-variables-existence-in-php-isset-is-clearly-broken) – IncredibleHat Jan 22 '18 at 21:31

3 Answers3

2

isset($var) checks that the variable is defined in the current scope and its value is not null. Some examples:

<?php 
  isset($var); //false
?>

<?php 
  $var = null;
  isset($var); //false
?>

<?php 
  $var = "some string";
  isset($var); //true
?>


<?php 
  $var = "";
  isset($var); //true
?>


<?php 
  $var = false;
  isset($var); //true
?>
SrThompson
  • 5,568
  • 2
  • 17
  • 25
1

isset() will return true for any variable that is set and not identical to NULL.

Example:

$a = null;
$b = '';
$c = 0;
$d = false;
$e = true;

var_dump(isset($a), isset($b), isset($c), isset($d), isset($e), isset($f));

should display:

bool(false)
bool(true)
bool(true)
bool(true)
bool(true)
bool(false)
Max
  • 913
  • 1
  • 7
  • 18
0

In your case since you are not defining $var, isset($var) will return false.