Good Morning All,

I have a general question that I'm sure someone has an opinion on...

I have several little snippets of code that will be used in various places in my programs operation and to ensure that they are identical in their operation wherever they are used from, I have made each of them a seperate little script and am including them when needed...

My question is This:

Would it be more efficient to make each of them a function and call the function with the required variables, or to do it the way that I am, with a list of Includes to bring in each one in the order that they are needed?

Here is what one of them looks like:

<?php  //  use  include "all_inc/inc_update_mem.php";
      // vars required for prod_id 6 - $payor - to update mem_status to D
      // and increase existing subs_internal payment by $amount
      if ($prod_id == '6'){// IR Upgrade - Must be a Customer
      $sql = "
        UPDATE members
        SET mem_status='D', last_update='".$created."'
        WHERE mem_id = '".$payor."'
        LIMIT 1
      ";
      mysql_query($sql);
      $sql = "
        UPDATE subs_internal
        SET next_pmt_amt=next_pmt_amt+$amount, last_update='".$created."'
        WHERE mem_id = '".$payor."'
        LIMIT 1
      ";
      mysql_query($sql);

      }else{
  // vars required  $up1_id/$new_mem_status/$mstr_gen/$created/$payor/$prod_id
  // query to update the members record with new data
      $sql = "
        UPDATE members
        SET up1_id='".$up1_id."', mem_status='".$new_mem_status."', mstr_gen='".$mstr_gen."', last_update='".$created."'
        WHERE mem_id = '".$payor."'
        LIMIT 1
      ";
      mysql_query($sql);
        // This should only be done if NOT PTL purchase
      if ($prod_id=='1' || $prod_id=='3'){ // set plan to single
        $sql = "UPDATE members SET plan='S' WHERE mem_id = '".$payor."' LIMIT 1";
        mysql_query($sql);
      }elseif ($prod_id=='2' || $prod_id=='4'){ // set plan to Family
        $sql = "UPDATE members SET plan='F' WHERE mem_id = '".$payor."' LIMIT 1";
        mysql_query($sql);
      }

      }

?>

I have about a dozen like this (most only about 6-8 lines)

and this is how I utilize them now...

      // Get up1_id - 1st avail pos under referrer
      // returns $up1_id and $mstr_gen of new position
      //  ???  which prod_ids does this apply to ?
      include "all_inc/inc_get_up_one.php"; // ????

      // update members record up1/status/mstr_gen/plan/last_update
      include "all_inc/inc_update_mem.php"; // 1-4 and 6

      //UPDATE account_track increase ytd_purch  and decrease cur_bal
      include "all_inc/inc_track_purch.php";// ALL

      // Payment for product from members eWallet ??  IR pmt too???
      // returns $eW_trans_id
      include "all_inc/inc_trans_pmt.php"; // 1-4 and

Would it be more efficient to do them as functions?

Thanks for your response.

Douglas

Recommended Answers

All 5 Replies

Personally I'd prefer functions because you can easily pass parameters to them, instead of having values hard-coded.

Would it be more efficient to do them as functions?

Time it and find out. I think for small scripts the difference is negligible.

Member Avatar for diafol

I second p.

Using functions is easier - more reusable and it's on the way to creating more mature OOP, if you need to go down that route (if the functions lend themselves to being methods of an object).

It looks a bit CRUDdy, so you probably could oop it sometime.

LOL... OK, I had an idea that would have been the choice of those with more experience than I have...

I guess all in all the functions just make more sense.

And as for OOP... For some reason, I just haven't had / found / taken the time to study that to put it to use...

I'm probably still programming in an almost archaic fashion and need to work at coming up to date with my scripting.

Thanks for your feedback
Douglas

Member Avatar for diafol

I came to oop far too late and I'm paying the price for it. Therefore I now urge anybody who's prepared to listen to start learning oop before procedural coding ruins your mind. :)

OK, just thought I would update... took your advice and converted everything to function calls... will be fine and cleaner looking

thanks
Marking this solved, but have another question that i'll pose in a seperate thread.

Douglas

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.