| 
	
 | 
 Posted by Robert Cummings on 09/21/05 02:35 
On Tue, 2005-09-20 at 19:20, adriano ghezzi wrote: 
> hy guys this night I'm quite tired  
>  
> I need a little tip 
>  
>  ok var n could be from 0 to 12 
>  
> var f must be set  
> f=1 if    0<n<=4 
> f=2 if   5<n<=7 
> f=3 if  8<n<=12 
> f=4 if n>12 
>  
> due to my fatigue I coded four if-if else statement, 
>  
> in other languages it is possible use conditional epression in switch case 
> like  
> switch $n 
>  
> case (0<n<=4): 
> ........ 
>  
> but no in php 
>  
> any suggestion for more professional coding then 4 if/else statement 
 
For such a small set I'd say that's as good as it gets. However if there 
were 100 intervals you could try the following: 
 
function f( $n ) 
{ 
    $map = array 
    ( 
        //     min,   max,    f 
        array(   0,     4,    1 ), 
        array(   5,     7,    2 ), 
        array(   8,    12,    3 ), 
        array(  17,  null,    4 ), 
    ); 
 
    foreach( $map as $criteria ) 
    { 
        if( $criteria[0] === null || $n > $criteria[0] 
            && 
            $criteria[1] === null || $n <= $criteria[1] ) 
        { 
            return $criteria[2]; 
        } 
    } 
 
    return null; 
} 
 
I think your code is flawed by the way, the lower endpoints are only 
compared for greater than value and not equal to value meaning $n with 
value 4 would not match any f value. 
 
Cheers, 
Rob. 
--  
..------------------------------------------------------------. 
| InterJinn Application Framework - http://www.interjinn.com | 
:------------------------------------------------------------: 
| An application and templating framework for PHP. Boasting  | 
| a powerful, scalable system for accessing system services  | 
| such as forms, properties, sessions, and caches. InterJinn | 
| also provides an extremely flexible architecture for       | 
| creating re-usable components quickly and easily.          | 
`------------------------------------------------------------'
 
  
Navigation:
[Reply to this message] 
 |