检查PHP变量是否为NULL

UPDATE:

The following code

function findNowPlayingCondition () {
if (isset($nowplayingname) && isset($nowplayingartist)) {$nowplayingcond = '1'; };
if (isset($nowplayingname) && !$nowplayingartist) {$nowplayingcond = '2'; };
if (!$nowplayingname && isset($nowplayingartist)) {$nowplayingcond = '3'; };
if (!$nowplayingname && !$nowplayingartist) {$nowplayingcond = '4'; };
echo "$nowplayingcond";
}

always comes back with '4', again, I am stumped.

====================

I am trying to create a PHP if/then statement that if $nowplayingname has a valid string in it, and $nowplayingartist is not set, is '', or is NULL, it will set $nowplayingcond as '2'.

if (var_dump(isset($nowplayingname)) && !$nowplayingartist) {$nowplayingcond = '2'};

I am getting a parse error when this executes, I suspect it has something to do with var_dump(isset(, but I am not certain.

You are right, var_dump should not be there. var_dump() is used for debugging.

Following code would be enough:

if(isset($nowplayingname)) { ... }

This will do.

if(!empty($nowplayingname) && !isset($nowplayingartist))
{
$nowplayingcond = 2;
}

To fix the syntax error:

if (var_dump(isset($nowplayingname)) && !$nowplayingartist) {$nowplayingcond = '2';};
                                                                            // ---^

Further suggestions:

  • Indent your code properly and don't use one-liners that way:

    if (var_dump(isset($nowplayingname)) && !$nowplayingartist) {
      $nowplayingcond = '2';
    }
    
  • Why do you use var_dump() in an IF statement? Apart from the fact that var_dump()'s return value will always evaluate to a falsy boolean, the line doesn't make sense (logic error).

  • Use more readable variable names:

    $nowPlayingName
    $nowPlayingArtist