3

I'm beginner in PHP and was wondering what's the difference between this

$variable = "text"; 
if($variable){
    echo $variable; 
}

and this

$variable = "text"; 
if(isset($variable)){
    echo $variable; 
}
j08691
  • 204,283
  • 31
  • 260
  • 272
Giorgi
  • 383
  • 2
  • 14
  • `isset()` is best for radios/checkboxes. Use `empty()` for strings/integer inputs. when a variable contains a value, using `isset()` will always be true. – Funk Forty Niner Jan 12 '16 at 19:33
  • 5
    you set the variable yourself, so it's not a problem. isset() tests help prevent "undefined variable" or "undefined index" warnings for things that you THINK might be present, but aren't necessarily. e.g. consider `$_POST['foo']`. that will only be present if a POST request was performed, and a `foo` form field was defined. can't just start using it, because it MIGHT not be there, so you isset() test it to be sure. – Marc B Jan 12 '16 at 19:35
  • yes but in these given examples both works fine why should i use isset or empty? – Giorgi Jan 12 '16 at 19:35
  • 1
    still believe isset should be used for radios/checkboxes, not for variables/inputs. This is getting to be a primarily opinion-based discussion/question. – Funk Forty Niner Jan 12 '16 at 19:37
  • 1
    Put `$variable=false;` and try again. In your example the whole if is pointless so the question is pointless. – Sami Kuhmonen Jan 12 '16 at 19:43
  • Determine if a variable is set and is not NULL . If a variable has been unset with unset(), it will no longer be set. isset() will return FALSE if testing a variable that has been set to NULL . output will be : bool(true) – varunkumar Jan 12 '16 at 19:49
  • U cant use isset in bolean result becuase false is also set – devpro Jan 12 '16 at 19:49
  • This is also set " "; with spaces – devpro Jan 12 '16 at 19:50
  • Here's a nice comparison of what `empty($x)`, `is_null($x)`, `isset($x)` and `if($x)` evaluate to for different types of `$x`: http://php.net/manual/en/types.comparisons.php – lafor Jan 12 '16 at 19:51

4 Answers4

2

Sometimes you want to know if a variable is set or not e.g. for security reasons of for debugging reasons.

The function isset() is used to determine if a variable is set and is not NULL and is accordingly returning true or false.

The expression if($variable) {} instead will evaluate to false if $variable is set but is zero or if $variable is set but its current value is false (=counterexample)

Assume:

isset($variable): Evaluates to true if a variable is set and not null

if($variable): Evaluates to true depending on the value of a variable or the result/value of an expression

Blackbam
  • 17,496
  • 26
  • 97
  • 150
2

The most important feature of functions like isset(), empty(), etc. to me is, that they do not throw errors when a variable is not set, like Marc B already mentioned.

PS: I strongly recommend to turn on strict mode and all error reporting during development.

Community
  • 1
  • 1
schmunk
  • 4,708
  • 1
  • 27
  • 50
1

isset is used to check whether input you receive contains what you expect - this is a very, very loose definition and I wrote that trying to explain easily when you'd use it.

When you set a variable yourself, then it doesn't make sense to use isset, you know it's set and that it contains some value.

However, many languages, not just PHP, deal with user input. User in this case could be a human, another computer service etc. You usually store that input inside a variable. A great example is PHP's $_POST, $_GET, $_SERVER, $_SESSION.

When you receive an input that user made via HTML form, you want to check whether certain values have been populated - in that case, you use isset.

An example HTML form:

<form method="POST" action="your_file.php">
    <input type="text" name="first_name" />
    <input type="text" name="last_name" />
    <button type="submit">Send</button>
</form>

Someone can submit that form, they can fill in both inputs. Or only one of them, or even none. You don't know what they've done, but regardless - PHP will provide you with a variable that might contain correct input.

If I submitted that form with nothing filled in, and if you tried to access a value for first_name - you would get a PHP warning - and you don't want PHP warnings.

Therefore, before working with user input (in this case with a $_POST superglobal array), you would check whether I filled in everything properly and you'd do that with:

if(isset($_POST['first_name']) && isset($_POST['last_name']))
{
    echo "Form was filled in correctly";
}
else
{
    echo "Form wasn't filled in correctly! Please fill in both fields";
}

Hopefully that clears it up a bit. Just once more, I used very trivial but real-world examples.

N.B.
  • 13,688
  • 3
  • 45
  • 55
0

Perhaps you example is a bit skewed. However. if you were to ignore the first line of each code segment, perhaps you will see this differently. Consider the code segments

if($variable){
    echo $variable; 
}

If $variable has not been initialised and assigned a value, then the is statement will result in an error.

On the other hand, if you initialise the variable

$variable = "text";
if($variable){
     echo $variable; 
}

will test is the variable is true, or non zero.

The isset() function will ignore the value, and tell you if the variable is initialised.

In the following code segment, the if() statement will return true.

$variable = "text"; 
if(isset($variable)){
    echo $variable; 
}

while, in this code

<?php
if(isset($variable)){
    echo $variable; 
}

the if() statement will return false.

crafter
  • 6,246
  • 1
  • 34
  • 46