Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: if !empty & empty

So i have this form.. With 2 fields. "Youtube" and "link" I want to do if you have filled in YouTube, it should do this:

if(!empty($youtube)) {
 if ($pos === false) { 
 echo "Du skal indtaste youtube et URL, som starter med 'http://www.youtube.com/watch?..<br>";
 echo "<br> Har du ikke din video på YouTube, skal du ikke udfylde feltet, men kun 'Link' feltet.<br><br>";
 echo "<a href='javascript:history.back();'>Gå tilbage</a>";
 }

}

This do its job, but i also want to check on the same if(), if nothing in link. So ive did this:

    if(!empty($youtube) && empty($link)) {
     if ($pos === false) { 
     echo "Du skal indtaste youtube et URL, som starter med 'http://www.youtube.com/watch?..<br>";
     echo "<br> Har du ikke din video på YouTube, skal du ikke udfylde feltet, men kun 'Link' feltet.<br><br>";
     echo "<a href='javascript:history.back();'>Gå tilbage</a>";
     }
}

But what if i want to check the opposite, if theres something in LINK and nothing in youtube? And if i want to check if theres nothing at all in those two?

like image 534
Karem Avatar asked Feb 27 '10 10:02

Karem


People also ask

Is empty or NULL PHP?

empty() function in PHP ? The isset() function is an inbuilt function in PHP which checks whether a variable is set and is not NULL. This function also checks if a declared variable, array or array key has null value, if it does, isset() returns false, it returns true in all other possible cases.

How do you check if a parameter is empty?

To find out if a bash variable is empty: Return true if a bash variable is unset or set to the empty string: if [ -z "$var" ]; Another option: [ -z "$var" ] && echo "Empty" Determine if a bash variable is empty: [[ ! -z "$var" ]] && echo "Not empty" || echo "Empty"

Is empty vs isNull?

The isNull operator checks a string and returns a boolean value: true if the string is null, or false if the string is not null. The isEmpty operator checks if a string contains no characters and is only whitespace.

Is empty array empty PHP?

An empty array is falsey in PHP, so you don't even need to use empty() as others have suggested. PHP's empty() determines if a variable doesn't exist or has a falsey value (like array() , 0 , null , false , etc).


2 Answers

if(!empty($youtube) && empty($link)) {

}
else if(empty($youtube) && !empty($link)) {

}
else if(empty($youtube) && empty($link)) {
}
like image 152
Felix Kling Avatar answered Sep 27 '22 20:09

Felix Kling


Here's a compact way to do something different in all four cases:

if(empty($youtube)) {
    if(empty($link)) {
        # both empty
    } else {
        # only $youtube not empty
    }
} else {
    if(empty($link)) {
        # only $link empty
    } else {
        # both not empty
    }
}

If you want to use an expression instead, you can use ?: instead:

echo empty($youtube) ? ( empty($link) ? 'both empty' : 'only $youtube not empty' )
                     : ( empty($link) ? 'only $link empty' : 'both not empty' );
like image 31
bart Avatar answered Sep 27 '22 21:09

bart