SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist
    Join Date
    May 2002
    Location
    Barcelona, Spain
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question string into integers to add them

    This is a quick one, I have an string that looks like this:
    PHP Code:
    11.6+34.8+11.6+69.6
    (grabbing it from values off a db). What is the quickest way to add them together?

    I tried using some of the string to integer functions but it didn't do the trick. I can do it placing them into an array with explode then adding them, but I am sure there is a better way to do this. Even doing it as I grab them from the db:
    PHP Code:
                     $price number_format($row['price'], 2);    // 2 decimals
                     
    $tax = ($price number_format($row['tax'], 2) ) / 100;
                     
    $unitPrice $tax $price;
                     
    $totalPrice = ($tax $price) * $row['quantity'];
                     
    $subTotal .= "$totalPrice+";
                     echo 
    number_format($unitPrice2',''.') ; 
    Maybe do something here?
    Used to prefer PHP.
    Now I prefer Maya but stuck with PHP
    bruji.com

  2. #2
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    This seem to be a task for eval().

    PHP Code:
    $numbers '11.6+34.8+11.6+69.6';
    echo eval(
    'return '$numbers); 
    Let me know if I have misunderstood you, or if you need more help.

    Yours, Erik.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  3. #3
    SitePoint Evangelist
    Join Date
    May 2002
    Location
    Barcelona, Spain
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I'll try it. I ended up doing this:

    $subTotal[] = $totalPrice;
    // after while loop
    array_sum($subTotal);

    I was just curious if there was a more elegant way to do it.

  4. #4
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Using that method might be better than using eval(), since eval() takes down performance a bit.

    Yours, Erik.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  5. #5
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    could these 3 lines
    PHP Code:
     $unitPrice $tax $price;
     
    $totalPrice = ($tax $price) * $row['quantity'];
     
    $subTotal .= "$totalPrice+"
    be replaced by
    PHP Code:
     $unitPrice $tax $price;
     
    $totalPrice $unitPrice $row['quantity'];
     
    $subTotal += $totalPrice
    or am I missing something?

  6. #6
    SitePoint Evangelist
    Join Date
    May 2002
    Location
    Barcelona, Spain
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by swdev
    could these 3 lines
    PHP Code:
     $unitPrice $tax $price;
     
    $totalPrice = ($tax $price) * $row['quantity'];
     
    $subTotal .= "$totalPrice+"
    be replaced by
    PHP Code:
     $unitPrice $tax $price;
     
    $totalPrice $unitPrice $row['quantity'];
     
    $subTotal += $totalPrice
    or am I missing something?
    Yes, I changed them right after posting this initially
    Used to prefer PHP.
    Now I prefer Maya but stuck with PHP
    bruji.com

  7. #7
    SitePoint Evangelist
    Join Date
    May 2002
    Location
    Barcelona, Spain
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lilleman
    Hi,

    Using that method might be better than using eval(), since eval() takes down performance a bit.
    Ok, I'll keep it like that, thanks for your help.
    Used to prefer PHP.
    Now I prefer Maya but stuck with PHP
    bruji.com


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
  •