/    Sign up×
Community /Pin to ProfileBookmark

isset() and about existing variable’s value

i ussually seen, for checking of existing variable value, people use to use isset() function, for checking form value or else.

isset() is always return true if the variable is exists, whether the value is exists or not. so just use variable name to check whether the value of given variable is set or not.

[code=php]
<?
if(isset($_POST[‘var1’])) print “var1 exists, but the value may not exists<br>”;
if($_POST[‘var2’]) print “var2 exists and the value of var is exists, value of var2 = ” . $_POST[‘var2’] . “<br>”;
?>
————–
<br>
<form action=”<?=$PHP_SELF?>” method=post>
var1 (check with isset()) <input type=”text” name=”var1″><br><br>
var2 (check by default) <input type=”text” name=”var2″><br><br>
<input type=”submit” value=”CHECK :.”>
</form>
[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 29.2006 — Your test of...
[code=php]
if($_POST['var2'])
[/code]
...will fail (return FALSE) if, for instance, the user entered 0 (zero) as the value for that input. So it's possible in some situations that you might not want to use such a comparison. Also, depending on your error_reporting level, if $_POST['var2'] is not set for some reason, then that code will output a "notice" type error message, which could potentially be more than a cosmetic problem should it happens before a command that causes headers to be sent is executed.

I find a more robust method to verify that a value has been entered is:
[code=php]
if(isset($_POST['var2'] and trim($_POST['var2']) !== '')
{
// OK
}
else
{
// error
}
[/code]

This will avoid the unwanted notice if $_POST['var2'] is not set, and will not fail if the variable is set with a value of 0, or 0.0, or other non-empty strings that might be interpreted as FALSE.
Copy linkTweet thisAlerts:
@sb_Nov 29.2006 — Also !empty().
Copy linkTweet thisAlerts:
@NogDogNov 29.2006 — Also !empty().[/QUOTE]
Except [b]if(!empty($variable))[/b] has the same problem as [b]if($variable)[/b], as both will return FALSE if it's value is 0 (zero), 0.0, or "0".
Copy linkTweet thisAlerts:
@theRamonesauthorNov 29.2006 — yes, i already know if the input is 0, it returns false.

i just wanted a simple way to check whether the value is set or not, just with one condition : if (con) {}

You're right, your method is robust
×

Success!

Help @theRamones spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...