Search code examples
phpvariablesnullconditional-statements

What is the PHP syntax to check "is not null" or an empty string?


Possible Duplicate:
Check if a variable is empty

I have the following line in my JavaScript which sets a 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?


Solution

  • 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';
    ?>';