74

Possible Duplicate:
Check if a variable is empty

Simple PHP question:

I have this stement:

if (isset($_POST["password"]) && ($_POST["password"]=="$password")) {
...//IF PASSWORD IS CORRECT STUFF WILL HAPPEN HERE
}

Somewhere above this statement I use the following line in my JavaScript to set the username as a variable both in my JavaScript and in my PHP:

uservariable = <?php $user = $_POST['user']; print ("\"" . $user . "\"")?>;

What I want to do is add a condition to make sure $user is not null or an empty string (it doesn't have to be any particular value, I just don't want it to be empty. What is the proper way to do this?

I know this is a sill question but I have no experience with PHP. Please advise, Thank you!

Community
  • 1
  • 1
I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
  • please use the search function before asking question as you have been prompted to in http://stackoverflow.com/questions/ask-advice. This has been asked and answered before: http://stackoverflow.com/search?q=check+for+empty+value+php – Gordon Jun 26 '12 at 17:56

2 Answers2

128

Null OR an empty string?

if (!empty($user)) {}

Use empty().


After realizing that $user ~= $_POST['user'] (thanks matt):

var uservariable='<?php 
    echo ((array_key_exists('user',$_POST)) || (!empty($_POST['user']))) ? $_POST['user'] : 'Empty Username Input';
?>';
John Green
  • 13,241
  • 3
  • 29
  • 51
  • It looks like I read the question differently than everyone else... we're talking about $user, not $_POST['user'], right? – John Green Jun 26 '12 at 17:54
  • 1
    `$user` is assigned to `$_POST['user']` in his code. Might as well validate before assignment but it really doesn't matter – Matt Dodge Jun 26 '12 at 17:56
  • hmm, what's the difference, exactly? if I'm doing something wrong please tell me! – I wrestled a bear once. Jun 26 '12 at 17:57
  • @mattedgod - Good point. I missed that. – John Green Jun 26 '12 at 18:02
  • I like this way. I want this to be right. But I'm still getting errors. Unexpected ";" ... and I don't understand this part: 'Empty Username Input' – I wrestled a bear once. Jun 26 '12 at 18:35
  • I had a typo. Missed a paren. Try the code again. As far as 'Empty Username Input', that's just a string I chose to represent an empty user variable. If you want to use program flow to do something other than echo it out, we'd have to shift the code around a bit. – John Green Jun 26 '12 at 22:48
34

Use empty(). It checks for both empty strings and null.

if (!empty($_POST['user'])) {
  // do stuff
}

From the manual:

The following things are considered to be empty:

"" (an empty string)  
0 (0 as an integer)  
0.0 (0 as a float)  
"0" (0 as a string)    
NULL  
FALSE  
array() (an empty array)  
var $var; (a variable declared, but without a value in a class)  
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • Note that if 'user' is not set on $_POST, you'll get a warning. It'll still work, but it will fill up your logs quickly. I usually mitigate this with: `if ((array_key_exists('user',$_POST)) || (!empty($_POST['user']))` – John Green Jun 26 '12 at 17:58
  • all three of these are giving me error messages: – I wrestled a bear once. Jun 26 '12 at 18:12
  • if (isset($_POST["password"]) && ($_POST["password"]=="$password") && (array_key_exists('user',$_POST)) and if (isset($_POST["password"]) && ($_POST["password"]=="$password") && (!empty($_POST['user'])) and (isset($_POST["password"]) && ($_POST["password"]=="$password") && (!empty('$user')) – I wrestled a bear once. Jun 26 '12 at 18:14
  • i didn't check the logs, when i go to the page i see "php sytax error on line 173" – I wrestled a bear once. Jun 26 '12 at 18:15