Multiple if empty

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Subliminal
Forum Commoner
Posts: 40
Joined: Thu Oct 23, 2003 8:12 pm

Multiple if empty

Post by Subliminal »

need a quick little help... how would i write

if $var1 or $var2 or $var3 is empty do this

???

Code: Select all

<?php if(empty($var1 || $var2 || $var3)){echo"at least one is empty";}else {echo "none are empty";}



?>
Thanks againa guys!
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

use OR instead of the double |.
Subliminal
Forum Commoner
Posts: 40
Joined: Thu Oct 23, 2003 8:12 pm

Post by Subliminal »

hehehehehe slightly embarrassed
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

No worries. You'd laugh at some of the things I did when I was learning. ;)
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

That makes me remind mIRC scripting ;-)
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

err...correct me if i am wrong, but...

Code: Select all

if(empty($var1 OR $var2 OR $var3)){
	echo "at least one is empty";
} else {
	echo "none are empty";
}
Will not work...

Would need to be like this

Code: Select all

if(empty($var1) || empty($var2) || empty($var3)){
	echo "at least one is empty";
} else {
	echo "none are empty";
}
Mark
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Correct.

empty() ONLY takes a variable, it's also why things like if(empty(trim($var)) produces a parse error. Also OR and || are exactly the same apart from precedence :o
Subliminal
Forum Commoner
Posts: 40
Joined: Thu Oct 23, 2003 8:12 pm

Post by Subliminal »

Wow...
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Note to self: avoid offering help before morning coffee :wink:

Anyway, yes. The if statement would need multiple parenthesis around each variable you are checking and either || or OR between the parenthesis.

The example Bech posted will suffice.

Regards.
Subliminal
Forum Commoner
Posts: 40
Joined: Thu Oct 23, 2003 8:12 pm

Post by Subliminal »

Thanks A Million Guys...
Post Reply