Re: Variable substitution to replace switch



At Thu, 31 May 2007 06:02:30 -0700, damezumari let h(is|er) monkeys type:

http://www.phpbuilder.net/columns/akent20000610.php3?page=5

[snip]

switch ($type)
{
case 'hours':
$hours = $add + $hours;
break;
case 'minutes':
$minutes = $add + $minutes;
break;
case 'seconds':
$seconds = $add + $seconds;
break;
case 'months':
$months = $add + $months;
break;
case 'days':
$days = $add + $days;
break;
case 'years':
$years = $add + $years;
break;
}
$timestamp = mktime($hours, $minutes, $seconds, $months, $days,
$years);
$newdatetime = date('Y-m-d H:i:s', $timestamp);
return ($newdatetime);
}

It works OK, but I would like to replace the switch statement with one
statement a la:

&$type = &$type + $add;

Can that be done in php?


$$type += $add;

But, why are you reinventing the strtotime function?
$dayaftertomorrow = date('D M j G:i:s T Y',strtotime('+2 days'));

HTH,

Sh.

--
Schraalhans Keukenmeester - schraalhans@xxxxxxxxxxxxxxxxxxxxxx
[Remove the lowercase part of Spamtrap to send me a message]

"strcmp('apples','oranges') < 0"

.



Relevant Pages

  • Re: [Monad] Missing help about_switch
    ... Using a switch to handle multiple if statements. ... A switch statement is, in effect, a series of If statements. ... the automatic variable "$_" and is available during the evaluation of ... command at the MSH command prompt: ...
    (microsoft.public.windows.server.scripting)
  • Re: Switch for floating !!
    ... Why does the switch case statement does not have support ... characters representing elements of text, whose codes are arbitrarily assigned, ... Would you use a giant switch statement to distinguish fibonacci integers from ... You need a better example to actually rationalize switching on floating point ...
    (comp.lang.c)
  • Re: switch vs. if
    ... Any half-decent compiler will reduce a switch statement to a jump ... a switch statement is conceptually much more like a jump table ...
    (comp.lang.cpp)
  • Re: Mulitple IIf statements - Too many
    ... The database I was asking about originally has been put on hold. ... use the SWITCH statement in a different database and I am getting an "#Error" ... I try to run the query, it says the query is too complex. ... the Switch() function is more compact and efficient than multiple ...
    (microsoft.public.access.queries)
  • Re: nested ifs
    ... However, when you can use switch, the ... A PHP switch statement only really works on the valueof a single ... Certainly end brackets of nested blocks should be commented to ... I don't have any inline code other than defines in include files. ...
    (comp.lang.php)