Re: Boolean Parameter to 3 Options?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Apr 29, 2009 at 1:30 PM, Shawn McKenzie <nospam@xxxxxxxxxxxxx> 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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux