/    Sign up×
Community /Pin to ProfileBookmark

isset vs !empty

Hello,

I am fairly new to PHP and I am creating a basic registration from. I was trying to use isset to check all the variables but without any luck. It seemed all I needed to go was use !empty instead of isset.

Any reason why isset would not work?

if ( (!empty($username)) && (!empty($email)) && (!empty($email2)) && (!empty($pass)) && (!empty($pass2)) )
{
// seems to work
// rest of code to validate
}

else
{
echo ‘please fill in all fields’;
}

Cheers.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@TheAliveWinnerMay 28.2012 — [code=php]
if(isset($username,$email,$email2,$pass,$pass2)) {
// do stuff here
} else {
// do stuff here
}
[/code]
Copy linkTweet thisAlerts:
@TheAliveWinnerMay 28.2012 — [I]isset[/I]([I]$variable[/I] [, ...[I]$variable[/I]...])[/QUOTE]

[COLOR=DarkGreen][I]Accepts multiple $variables separated by [B]commas[/B], but will [B]only[/B] return TRUE if all

variables are set![/I]
[/COLOR]
Copy linkTweet thisAlerts:
@NogDogMay 28.2012 — isset() returns false if the variable is not set at all or is set to NULL. In addition to those two cases, [B]![/B]empty() will return false if the variable is set but has a value of FALSE, 0, 0.0, or "" (empty string). Thus, for example, you need to be careful about using [b]![/b]empty() when testing for a numeric input where zero is a valid value.
[code=php]
<pre><?php
$notEmpty = (!empty($foo));
$isset = (isset($foo));
echo "not set:nnotEmpty:";
var_dump($notEmpty);
echo "isset:";
var_dump($isset);
echo "n";

$foo = null;
$notEmpty = (!empty($foo));
$isset = (isset($foo));
echo "null:nnotEmpty:";
var_dump($notEmpty);
echo "isset:";
var_dump($isset);
echo "n";

$foo = false;
$notEmpty = (!empty($foo));
$isset = (isset($foo));
echo "false:nnotEmpty:";
var_dump($notEmpty);
echo "isset:";
var_dump($isset);
echo "n";

$foo = 0;
$notEmpty = (!empty($foo));
$isset = (isset($foo));
echo "0 (zero):nnotEmpty:";
var_dump($notEmpty);
echo "isset:";
var_dump($isset);
echo "n";

$foo = 1;
$notEmpty = (!empty($foo));
$isset = (isset($foo));
echo "non-zero number:nnotEmpty:";
var_dump($notEmpty);
echo "isset:";
var_dump($isset);
echo "n";

$foo = '';
$notEmpty = (!empty($foo));
$isset = (isset($foo));
echo "empty string:nnotEmpty:";
var_dump($notEmpty);
echo "isset:";
var_dump($isset);
echo "n";

$foo = 'string';
$notEmpty = (!empty($foo));
$isset = (isset($foo));
echo "string:nnotEmpty:";
var_dump($notEmpty);
echo "isset:";
var_dump($isset);
echo "n";
?></pre>
[/code]

Output:
<i>
</i>not set:
notEmpty:bool(false)
isset:bool(false)

null:
notEmpty:bool(false)
isset:bool(false)

false:
notEmpty:bool(false)
isset:bool(true)

0 (zero):
notEmpty:bool(false)
isset:bool(true)

non-zero number:
notEmpty:bool(true)
isset:bool(true)

empty string:
notEmpty:bool(false)
isset:bool(true)

string:
notEmpty:bool(true)
isset:bool(true)
Copy linkTweet thisAlerts:
@kbduvallMay 28.2012 — When you submit a form, say VIA POST, the form sets the value of the form field into the $_POST variable. So if the form field (say, name="username") is left empty, $_POST['username'] will be set, but just to an empty value ("").

When you're validating the form input on the PHP side, using isset($_POST['username']) will make sure that the form field is there, while empty($_POST['username']) will tell you whether or not the user has put anything into that field.

In my personal opinion, it's important to check both when validating form fields as users can alter the form input data using various methods. You can do that by calling each function for each field, or if you're going to be doing that many times, you can create a function.
×

Success!

Help @entropy 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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...