Re: Boolean Parameter to 3 Options?
| From: | Matt Neimeyer | Date: | Wed, 29 Apr 2009 18:15:56 +0000 |
| Subject: | Re: Boolean Parameter to 3 Options? | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-292131@lists.php.net to get a copy of this message | ||
On Wed, Apr 29, 2009 at 1:30 PM, Shawn McKenzie <nospam@mckenzies.net> wrote:
> Philip Thompson wrote:
>> On Apr 29, 2009, at 11:42 AM, Matt Neimeyer wrote:
>>
>>> I have a function that currently takes a boolean value as a parameter.
>>> But now I want to expand it to 3 options... So if I have...
>>>
>>> function doFooBar($doFoo = false)
>>> {
>>> if($doFoo)
>>> { echo "Did Foo"; }
>>> else
>>> { echo "Did Bar"; }
>>> }
>>>
>>> Is it as "simple" as changing it like follows to avoid having to
>>> change existing code that won't use the new values.
>>>
>>> function doFooBar($doFoo = 0)
>>> {
>>> if($doFoo == 2)
>>> { echo "Did Baz"; }
>>> else if($doFoo == 1)
>>> { echo "Did Foo"; }
>>> else
>>> { echo "Did Bar"; }
>>> }
>>>
>>> Something about that disturbs me. Perhaps because any time I think "Oh
>>> it will be as simple as..." it usually isn't.
>>>
>>> Thanks in advance!
>>>
>>> Matt
>>
>> Unless you're doing a strict comparison (===), it probably won't make a
>> lot of difference. However, if you sent "true" to the function, I
>> believe it will reach the last else condition. You may revisit all the
>> locations you call it and update them appropriately.
>>
>> Those are my initial thoughts....
>>
>> ~Philip
>>
>
> No, true will match the first condition. If you're using true and false
> now and just want to add a second option then continue using true/false.
> Don't mix and match. 1 == true and 2 == true.
>
> function doFooBar($doFoo = false)
> {
> if($doFoo === 2) {
> //something
> }
> elseif($doFoo === true) {
> //something true
> }
> elseif($doFoo === false) {
> //something false
> }
> }
>
Ah. I somehow missed the direction of the typecasting. Not that the
documentation isn't completely explicit on the matter but for some
reason I was thinking that the bool got cast to 1 or 0... not that the
string/number got cast to a bool (following the standards there).
Good to know.
Thanks
Matt