Re: Boolean Parameter to 3 Options?

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

 



2009/4/29 Matt Neimeyer <matt@xxxxxxxxxxxx>:
> 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
>
>

Another way ...

<?php
define ('OPTION_NONE', 0);
define ('OPTION_ALL', -1);

// Options are defined with the value of 1, 2, 4, 8, 16, 32, etc.
define ('OPTION_1', 1);
define ('OPTION_2', 2);
define ('OPTION_3', 4);

function foo($i_Options = OPTION_NONE)
	{
	if ($i_Options & OPTION_1) { echo 'Option 1 chosen', PHP_EOL; } //
Note single &
	if ($i_Options & OPTION_2) { echo 'Option 2 chosen', PHP_EOL; } //
Note single &
	if ($i_Options & OPTION_3) { echo 'Option 3 chosen', PHP_EOL; } //
Note single &
	}

foo();
foo(OPTION_ALL);
foo(OPTION_1 | OPTION_3);
?>

outputs ...

Option 1 chosen
Option 2 chosen
Option 3 chosen
Option 1 chosen
Option 3 chosen


-- 
-----
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
"Standing on the shoulders of some very clever giants!"

-- 
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