Forum Moderators: coopster

Message Too Old, No Replies

If either variable is empty, do this.

Small syntax problem

         

TheBrandon

5:39 pm on Nov 6, 2009 (gmt 0)

10+ Year Member



Hello everyone,

I am working on a theater project. Some members are allowed free tickets, or tickets to be picked up at will call.

I'm trying to get both variables to test as empty so I can customize a report.

I essentially want to say:

if($var1 is not empty) OR ($var2 is not empty)

$var1 and $var2 will always be set so I don't think I can use isset. They will just contain the value 0 if they aren't allowed free tickets.

I have tried the following:

if(($a == 0) ¦¦ ($b == 0)){
if(isset($a) ¦¦ isset($b)){
if(empty($a) ¦¦ (empty($b))){
if(!empty($a) ¦¦ (!empty($b))){
if(!($a) ¦¦ (!($b))){
if(($a) ¦¦ (($b))){

None of them seem to be working properly.

If either $a or $b contain anything other than 0, I need the program to echo 1 set of data. If they both contain 0, I basically want it to do nothing.

Can anyone help with the syntax for this? It would be greatly appreciated.

rocknbil

6:50 pm on Nov 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



IMO it's **always** good to set a value, as you have done. You limit what you have to test against (isset(), null) and also squelches "variable not set" warnings.

If it's always "something" or "0" why can't you do this?

if (($a>0) or ($b > 0))

You didn't say if $a or $b can be text values, in which case that won't work. If this is the case, I would change the initial/default value to '', a blank string,

$a=$b='';

if (($a!= '') or ($b != ''))

TheBrandon

7:00 pm on Nov 6, 2009 (gmt 0)

10+ Year Member



They can't be text values. They will always be 0 or a higher number. I didn't think of doing less than or greater than. I sometimes get stuck in the variable itself rather than the data it contains.

Thank you for the suggestion. I will try it and post an update.

TheBrandon

7:50 pm on Nov 6, 2009 (gmt 0)

10+ Year Member



You're right. That worked perfectly. Thank you very much.

$a = '1';
$b = '1';

if(($a>0) ¦¦ ($b>0)){
echo 'Reaction 1';
}else{
echo 'Reaction 2';
}