开发者

php number problem

$num = 9;
   switch ($num){
    case $num > 0 && $num < 5:
        echo 5;
    break;
    case $num > 5 && $num < 10:
        echo 10;
    break;
    case $num > 10 && $num < 15:
        echo 15;
    开发者_如何学Pythonbreak;
     ....
}
result: 10

stupid code.

Do you have any good idea for this? Thank you.


$result = ceil($num/5)*5;

Is that what you're looking for?


You could convert it to ASCII art:

  _                                    ___    
  | |                         ______   / _ \ _ 
 / __)_ __  _   _ _ __ ___   |______| | (_) (_)
 \__ \ '_ \| | | | '_ ` _ \   ______   \__, |  
 (   / | | | |_| | | | | | | |______|    / / _ 
  |_||_| |_|\__,_|_| |_| |_|            /_/ ( )
                                            |/ 


                  _ _        _        ___                      __    __
                 (_) |      | |      / / |                     \ \  / /
     _____      ___| |_  ___| |__   | / __)_ __  _   _ _ __ ___ | || | 
    / __\ \ /\ / / | __|/ __| '_ \  | \__ \ '_ \| | | | '_ ` _ \| / /  
    \__ \\ V  V /| | |_| (__| | | | | (   / | | | |_| | | | | | | \ \  
    |___/ \_/\_/ |_|\__|\___|_| |_| | ||_||_| |_|\__,_|_| |_| |_| || | 
                                     \_\                       /_/  \_\


                             _                         __      ___                     _                            __  _____   
                            | |                        \ \    / _ \    ___    ___     | |                          / / | ____|_ 
       ___  __ _ ___  ___  / __)_ __  _   _ _ __ ___    \ \  | | | |  ( _ )  ( _ )   / __)_ __  _   _ _ __ ___    / /  | |__ (_)
      / __|/ _` / __|/ _ \ \__ \ '_ \| | | | '_ ` _ \    > > | | | |  / _ \/\/ _ \/\ \__ \ '_ \| | | | '_ ` _ \  < <   |___ \   
     | (__| (_| \__ \  __/ (   / | | | |_| | | | | | |  / /  | |_| | | (_>  < (_>  < (   / | | | |_| | | | | | |  \ \   ___) |_ 
      \___|\__,_|___/\___|  |_||_| |_|\__,_|_| |_| |_| /_/    \___/   \___/\/\___/\/  |_||_| |_|\__,_|_| |_| |_|   \_\ |____/(_)



                    _             _____   
                   | |           | ____|_ 
           ___  ___| |__   ___   | |__ (_)
          / _ \/ __| '_ \ / _ \  |___ \   
         |  __/ (__| | | | (_) |  ___) |_ 
          \___|\___|_| |_|\___/  |____/( )
                                       |/ 


      _                    _       
     | |                  | |    _ 
     | |__  _ __ ___  __ _| | __(_)
     | '_ \| '__/ _ \/ _` | |/ /   
     | |_) | | |  __/ (_| |   <  _ 
     |_.__/|_|  \___|\__,_|_|\_\( )
                                |/ 


                             _                         __     _____                    _                            __  __  ___    
                            | |                        \ \   | ____|   ___    ___     | |                          / / /_ |/ _ \ _ 
       ___  __ _ ___  ___  / __)_ __  _   _ _ __ ___    \ \  | |__    ( _ )  ( _ )   / __)_ __  _   _ _ __ ___    / /   | | | | (_)
      / __|/ _` / __|/ _ \ \__ \ '_ \| | | | '_ ` _ \    > > |___ \   / _ \/\/ _ \/\ \__ \ '_ \| | | | '_ ` _ \  < <    | | | | |  
     | (__| (_| \__ \  __/ (   / | | | |_| | | | | | |  / /   ___) | | (_>  < (_>  < (   / | | | |_| | | | | | |  \ \   | | |_| |_ 
      \___|\__,_|___/\___|  |_||_| |_|\__,_|_| |_| |_| /_/   |____/   \___/\/\___/\/  |_||_| |_|\__,_|_| |_| |_|   \_\  |_|\___/(_)



                    _             __  ___    
                   | |           /_ |/ _ \ _ 
           ___  ___| |__   ___    | | | | (_)
          / _ \/ __| '_ \ / _ \   | | | | |  
         |  __/ (__| | | | (_) |  | | |_| |_ 
          \___|\___|_| |_|\___/   |_|\___/( )
                                          |/ 


      _                    _       
     | |                  | |    _ 
     | |__  _ __ ___  __ _| | __(_)
     | '_ \| '__/ _ \/ _` | |/ /   
     | |_) | | |  __/ (_| |   <  _ 
     |_.__/|_|  \___|\__,_|_|\_\( )
                                |/ 


                             _                         __     __  ___                     _                            __  __ _____   
                            | |                        \ \   /_ |/ _ \    ___    ___     | |                          / / /_ | ____|_ 
       ___  __ _ ___  ___  / __)_ __  _   _ _ __ ___    \ \   | | | | |  ( _ )  ( _ )   / __)_ __  _   _ _ __ ___    / /   | | |__ (_)
      / __|/ _` / __|/ _ \ \__ \ '_ \| | | | '_ ` _ \    > >  | | | | |  / _ \/\/ _ \/\ \__ \ '_ \| | | | '_ ` _ \  < <    | |___ \   
     | (__| (_| \__ \  __/ (   / | | | |_| | | | | | |  / /   | | |_| | | (_>  < (_>  < (   / | | | |_| | | | | | |  \ \   | |___) |_ 
      \___|\__,_|___/\___|  |_||_| |_|\__,_|_| |_| |_| /_/    |_|\___/   \___/\/\___/\/  |_||_| |_|\__,_|_| |_| |_|   \_\  |_|____/(_)



                    _             __ _____   
                   | |           /_ | ____|_ 
           ___  ___| |__   ___    | | |__ (_)
          / _ \/ __| '_ \ / _ \   | |___ \   
         |  __/ (__| | | | (_) |  | |___) |_ 
          \___|\___|_| |_|\___/   |_|____/( )
                                          |/ 


      _                    _       
     | |                  | |    _ 
     | |__  _ __ ___  __ _| | __(_)
     | '_ \| '__/ _ \/ _` | |/ /   
     | |_) | | |  __/ (_| |   <  _ 
     |_.__/|_|  \___|\__,_|_|\_\( )
                                |/ 






       _ _ _ _ 
      (_|_|_|_)



 __   
 \ \  
  | | 
   \ \
   / /
  | | 
 /_/  


                     _ _       __  ___  
                    | | |  _  /_ |/ _ \ 
  _ __ ___ ___ _   _| | |_(_)  | | | | |
 | '__/ _ | __| | | | | __|    | | | | |
 | | |  __|__ \ |_| | | |_ _   | | |_| |
 |_|  \___|___/\__,_|_|\__(_)  |_|\___/ 


switch does not work for this scenario, it only evaluate once

$num = 9;
$div = ceil($num/5);

switch ($div)
{
  case 1:
    echo 5;
    break;
  case 2:
    echo 10;
    break;
  case 3:
    echo 15;
    break;
  ...
}


That's not how switch works, this will create comparisons like 9 == (9 > 0 && 9 < 5), which is not what you want. The easiest way is probably this:

if ($num < 5) {
    ...
} else if ($num < 10) {
    ...
} ...

("Easiest way" as in "easiest way to express logic like this." The easiest way for this particular operation is the application of basic math.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜