tags:

views:

87

answers:

4

I have the following if statement

if (isset($part->disposition) and ($part->disposition=='attachment'))

Problem is the second part of that statement, i also need to include this;

($part->disposition=='inline')

The statement needs to work if the disposition is attachment or if its inline.

+3  A: 

Hey Patrick

doesn't that work:

if (isset($part->disposition) and (($part->disposition=='attachment') or ($part->disposition=='inline')))
This worked and was first so you get win. Thanks all for the prompt help and the diff methods!
Patrick
+6  A: 

This must help:

if (isset($part->disposition) && ($part->disposition=='attachment' || $part->disposition=='inline'))
Sarfraz
+1 for more efficient use of parens
Kitson
@Kitson it is my habit to write cleaner code. Easy to understand for others, with proper indentation, line spacing, correct use of braces, brackets, etc. :)
Sarfraz
+2  A: 

This try (for efficiency):

if (isset($part->disposition))
{
    if($part->disposition=='attachment' || $part->disposition=='inline')
    {
        // perform task
    }
}
rockacola
Why is this more efficient? Won't the second statement be ignored if the first is false - when they are put together - and hence be equally efficient?
stiank81
It's not more efficient, but more readable and maintainable.
DanDan
+3  A: 

In case you may be going to have more than two options in the future you might also be interested in in_array(needle, haystack)

if (
  isset($part->disposition)
  && in_array($part->disposition, array('attachment', 'inline', 'option3', 'option4'))
)

If you want the equivalent of === (strict comparison, instead of == like in your example) set the third parameter of in_array() to true.

VolkerK
+1 thanks for the tip!
Patrick