SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Thread: Simplify code?

  1. #1
    SitePoint Evangelist
    Join Date
    Jul 2002
    Location
    Italy
    Posts
    429
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simplify code?

    I have only just touched the surface of PHP's functionality. There are just so many ways to do things.

    I wondered if there was a simpler/more efficient way to express the following:
    PHP Code:
    $fees 0;
    if     ( 
    $price 500000 $fees 750;
    elseif ( 
    $price 350000 $fees 550;
    elseif ( 
    $price 250000 $fees 500;
    elseif ( 
    $price 200000 $fees 460;
    elseif ( 
    $price 150000 $fees 420;
    elseif ( 
    $price 100000 $fees 385;
    elseif ( 
    $price )      $fees 350
    Thx G
    blessed are the cracked for they let in the light

  2. #2
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    You could use a switch-case statement instead of a chain of if-statements.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  3. #3
    SitePoint Evangelist
    Join Date
    Jul 2002
    Location
    Italy
    Posts
    429
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks squire. I had thought of the switch-case statement - I've never used it yet! I'm not sure it would be simpler...
    blessed are the cracked for they let in the light

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,121
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure a switch-case would work, as you are needing to do a comparison to determine which fee amount to apply. switch statements accept a value and you can then create case statements for the values you expect and perform different operations.

    Another technique you could utilize is using an array to store your price threshold and the fee associated when that threshold is met, then you can loop through the array to determine which fee to apply. However, that is really just hiding the underlying process you are trying to do, and the if/else statements are much clearer to their intent from a readability stand point.

    So I guess, my recommendation is to leave it as is, or possibly place it in a table that you can run a query against (again, really is somewhat hiding the process, but the query will be just as readable as to its intent).

  5. #5
    SitePoint Evangelist
    Join Date
    Jul 2002
    Location
    Italy
    Posts
    429
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Cpradio. G
    blessed are the cracked for they let in the light

  6. #6
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    A good way to achieve this in object oriented code would be to use a design pattern called the strategy design.

  7. #7
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,263
    Mentioned
    115 Post(s)
    Tagged
    1 Thread(s)
    How often are these fees going to change? You might want to consider putting them into a database table. If you would setup the table with a minimum and maximum dollar amount, you could do it in one query ($result = mysql_query("SELECT fee FROM AddtionalFee WHERE " . $price . " IS BETWEEN LowPrice and HighPrice");).

    Then if you want to change the fees or add a range, you just have to make a database change.
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style
    Learn how to be ready for The Forums' Move to Discourse

  8. #8
    SitePoint Evangelist
    Join Date
    Jul 2002
    Location
    Italy
    Posts
    429
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys.
    blessed are the cracked for they let in the light

  9. #9
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,061
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Just for the heck of it:

    PHP Code:
    $fees 350;
    foreach(array(
    10000 => 38515000 => 42020000 => 46025000 => 50035000 => 55050000 => 750) as $p => $fee)
    {
        if (
    $p >= $price)
        {
           
    $fees $f;
           continue;
        }
        break;

    I started $fees on 350 assuming that price will always be more than 0. If it's not you should add it to the array (at the front).
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  10. #10
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    I don't think this is cleaner code though. It's shorter, but I actually find it more difficult to read...

    Quote Originally Posted by ScallioXTX View Post
    Just for the heck of it:

    PHP Code:
    $fees 350;
    foreach(array(
    10000 => 38515000 => 42020000 => 46025000 => 50035000 => 55050000 => 750) as $p => $fee)
    {
        if (
    $p >= $price)
        {
           
    $fees $f;
           continue;
        }
        break;

    I started $fees on 350 assuming that price will always be more than 0. If it's not you should add it to the array (at the front).

  11. #11
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,061
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by aaarrrggh View Post
    I don't think this is cleaner code though. It's shorter, but I actually find it more difficult to read...
    I agree. The best (cleanest) way to solve this is to do away with the current fee structure and get a formula instead, but I don't think that's an option.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  12. #12
    SitePoint Evangelist
    Join Date
    Jul 2002
    Location
    Italy
    Posts
    429
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Aaarrrggh. I think that might have been more what I had in mind but, yes, whether it's any more readable... Anyway, thanks for an alternative way to code it...
    blessed are the cracked for they let in the light


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •