Connecting Tech Pros Worldwide Help | Site Map

Is it inefficient to use includes and requires compared to functions ?

Familiar Sight
 
Join Date: Jan 2009
Posts: 165
#1: Oct 9 '09
Hello,

I have been writing procedural php for a couple of years
and I have quite lot of little scrips that I use in different
large scripts.

I'll give and example
I display the category menu on onmany pages so I have this
little php file:

Expand|Select|Wrap|Line Numbers
  1. switch ($N_cat_cd)
  2.      { 
  3.      case "ex": $Dcat = " Expert-World";  break;
  4.      case "se": $Dcat = " Self Improvement";  break;
  5.      case "s.": $Dcat = " S.E.O.";  break;
  6.      case "tr": $Dcat = " Traffic"; break;
  7.      case "ni": $Dcat = " Niche-Affiliate"; break;
  8.      case "co": $Dcat = " Copy writing"; break;
  9.      case "sa": $Dcat = " Sales systems"; break;
  10.      case "pr": $Dcat = " Product creation"; break;
  11.      case "we": $Dcat = " Website Design"; break;
  12.      case "gr": $Dcat = " Graphics"; break;
  13.      }
  14.      $SEOcat = strtolower(trim($Dcat));
  15.  
  16. $cat_menu ="
  17.   <div class='menuCAT'>
  18.     <ul>
  19.     <li> <a href=\"/im/menu1.htm" > $SEOcat</a></li>
  20.     <li> <a href=\"/im/menu1.htm" > $SEOcat</a></li>
  21.     <li> <a href=\"/im/menu2.htm" > $SEOcat</a></li>
  22.     <li> <a href=\"/im/menu3.htm" > $SEOcat</a></li>
  23.     <li> <a href=\"/im/menu4.htm" > $SEOcat</a></li>
  24.     <li> <a href=\"/im/menu5.htm" > $SEOcat</a></li>
  25.     <li> <a href=\"/im/menu6.htm" > $SEOcat</a></li>
  26.    </ul>
  27.    </div>";
  28.  

So when ever I need this code, I require inside my main php script

For example
Expand|Select|Wrap|Line Numbers
  1. $page="pr";
  2. $title1 = " products and resources";  
  3. $desc=" internet marketing products";
  4.  
  5. if (@$_SESSION['auth'] == "yes" ){
  6.      require_once("mem_head.php");
  7.      }  // end if
  8. else {
  9.      require_once("a_head.php");
  10.      }  // end if
  11.  
  12. $dir = "products";
  13. $pub = "prod";
  14. require_once("a_cat_menu.php");
  15. ?>     
  16.  
  17. <div class="page_name">
  18. <h1>Resources - Expert IM Products</h1>
  19. </div> <!-- End div: page_name --> 
  20.  
In fact there are three sets of require command, I have bolded them


Now I was quite happy with this until I saw this
thread in a forum about an email class called swift.


Quote:
The best way to work is usually by making a function in a file, and calling
that function instead of including / requiring in loop

Because the php works this way:

Read File,
Tokenize,
Compilation
Execution

If you include at each iteration, the whole process is repeated, if you use
functions, it will only execute the portion of code needed to send the mail

From a functionality point of view, it works the same

From a performance point of view, it will work way slower.
So - and here is my question:

Should I be re-writing my includes and requests as functions?

I assumed that the whole code would be read by the php engine before compilation and execution - so why the suggested slow down ?

Maybe this is just inside loops ?

The actual bit of code that was under discussion with this:

Here is the script.

Expand|Select|Wrap|Line Numbers
  1. START
  2. $swift_path =  "/home/focus7/public_html/lib/swift_required.php";
  3. $func_path =  "/home/focus7/public_html/my_functions.php";
  4. require_once("$swift_path");
  5. require_once("$func_path");
  6.  
  7. $transport = Swift_SmtpTransport::newInstance('mail.MY-SITE.com', 25)
  8.   ->setUsername('MY-NAME')
  9.   ->setPassword('MY-PASS')
  10.   ;
  11.  
  12. $mailer = Swift_Mailer::newInstance($transport);
  13.  
  14. $sql_client = "SELECT * FROM clients ";                     // OPEN
  15. DATABASE & GET CLIENTS
  16. $result_client = mysql_query($sql_client)       or die("could not execute
  17. FIND CLIENT query.". mysql_error());
  18. while($row_client = mysql_fetch_assoc($result_client))
  19. {                            // START THE LOOP
  20.   extract($row_client);
  21.  
  22.   $message = $message.$ek.$ek1.$ek2;     // CREATE THE MESSAGE FROM
  23. DATA
  24.  
  25.   $simdi = date('H:m:s, l, j F Y',$today);
  26.  
  27.   $to = $email;                                         // CREATE THE
  28. HEADERS
  29.   $contact = $contact;
  30.   $fm_email="Account-Mana...@support-focus.com";
  31.   $fm_name="Support Center";
  32.   $subject="Your Weekly Report";
  33.   $message_html = "Dear $contact,<br><br><b>Weekly Report: $simdi</
  34. b><br><br>".$message”;
  35.  
  36.    require("send_auto.php");                      // SEE BELOW
  37.  
  38. } // end while         //    LOOPS THROUGH DATABASE  ( ONLY 25
  39.  
  40. RECORDS )
  41.  
  42. THIS IS THE send_auto.php
  43.  
  44. START
  45. $message = Swift_Message::newInstance();
  46. send_auto.php
  47.  
  48. // set the sender
  49. $message->setFrom(array($fm_email => $fm_name));
  50.  
  51. // set the recipient
  52. $message->setTo(array($to => $contact));
  53.  
  54. // set the subject
  55. $message->setSubject($subject);
  56.  
  57. // set the html body
  58. $message->setBody($message_html, 'text/html');
  59.  
  60. // set the text body
  61. $message->addPart($message_text, 'text/plain');
  62.  
  63. //Send the message
  64. if (!$mailer->send($message, $failures)) {
  65.    // update failure database
  66.      $sql = "INSERT INTO `email_fail` (type,email,send_dt) VALUES
  67. ( '$send_type', '$to','$today')";
  68.                  mysql_query($sql) or die("could not execute email_fail update
  69. query". mysql_error());
  70.    } // end if
  71. END 

I would really appreciate you inputs as it may fundementally change the way I structure my code.

Thanks
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#2: Oct 9 '09

re: Is it inefficient to use includes and requires compared to functions ?


Quote:

Originally Posted by jeddiki View Post

I assumed that the whole code would be read by the php engine before compilation and execution - so why the suggested slow down ?

this is a case where you have to carefully read. the paragraph is about include()/require() in a loop structure:
Expand|Select|Wrap|Line Numbers
  1. while ($condition)
  2. {
  3.     include 'include_file.php';
  4. }
then the meaning becomes obvious…
Familiar Sight
 
Join Date: Jan 2009
Posts: 165
#3: Oct 10 '09

re: Is it inefficient to use includes and requires compared to functions ?


Thanks Dormilich for pointing that ,

I had only used the require because I have four scripts that
use the same sending code.

Apart from that, there is no need to put the code outside the
script.

So I guess the loop will perform better like this:

Expand|Select|Wrap|Line Numbers
  1. START
  2. $swift_path =  "/home/focus7/public_html/lib/swift_required.php";
  3. $func_path =  "/home/focus7/public_html/my_functions.php";
  4. require_once("$swift_path");
  5. require_once("$func_path");
  6.  
  7. $transport = Swift_SmtpTransport::newInstance('mail.MY-SITE.com', 25)
  8.   ->setUsername('MY-NAME')
  9.   ->setPassword('MY-PASS')
  10.   ;
  11.  
  12. $mailer = Swift_Mailer::newInstance($transport);
  13.  
  14. $sql_client = "SELECT * FROM clients ";                     // OPEN
  15. DATABASE & GET CLIENTS
  16. $result_client = mysql_query($sql_client)       or die("could not
  17. execute
  18. FIND CLIENT query.". mysql_error());
  19. while($row_client = mysql_fetch_assoc($result_client)) {        //
  20. START THE LOOP
  21.   extract($row_client);
  22.  
  23.   $message = $message.$ek.$ek1.$ek2;     // CREATE THE MESSAGE FROM
  24. DATA
  25.  
  26.   $simdi = date('H:m:s, l, j F Y',$today);
  27.  
  28.   $to = $email;                                         // CREATE THE
  29. HEADERS
  30.   $contact = $contact;
  31.   $fm_email="Account-Mana...@support-focus.com";
  32.   $fm_name="Support Center";
  33.   $subject="Your Weekly Report";
  34.   $message_html = "Dear $contact,<br><br><b>Weekly Report: $simdi</
  35. b><br><br>".$message”;
  36.  
  37. $message = Swift_Message::newInstance();
  38.  
  39. // set the sender
  40. $message->setFrom(array($fm_email => $fm_name));
  41.  
  42. // set the recipient
  43. $message->setTo(array($to => $contact));
  44.  
  45. // set the subject
  46. $message->setSubject($subject);
  47.  
  48. // set the html body
  49. $message->setBody($message_html, 'text/html');
  50.  
  51. // set the text body
  52. $message->addPart($message_text, 'text/plain');
  53.  
  54. //Send the message
  55. if (!$mailer->send($message, $failures)) {
  56.    // update failure database
  57.      $sql = "INSERT INTO `email_fail` (type,email,send_dt) VALUES
  58. ( '$send_type', '$to','$today')";
  59.                  mysql_query($sql) or die("could not execute email_fail update
  60. query". mysql_error());
  61.                  } // end if
  62.  
  63. $content = "Sent Weekly Report to $contact at $email \r\n";
  64. fwrite($handle, $content);
  65. } // end while                                    //  END OF LOOP
  66.  
  67. fclose($handle);
  68. ?> 
  69.  
There seems no point in creating a function out of that code
... or is there ???
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#4: Oct 10 '09

re: Is it inefficient to use includes and requires compared to functions ?


Quote:

Originally Posted by jeddiki View Post

There seems no point in creating a function out of that code
... or is there ???

if you’re sending to multiple recepients (I think you are), you can use Swiftmailer’s batchSend() method
Familiar Sight
 
Join Date: Jan 2009
Posts: 165
#5: Oct 10 '09

re: Is it inefficient to use includes and requires compared to functions ?


Yes,
I did read about that,
but I also saw in their forum that a lot of people have trouble with it.

It uses the same sending code in the end so I
think I will stick to the basic version and control
it with my while loop.

But thanks anyway :)
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#6: Oct 10 '09

re: Is it inefficient to use includes and requires compared to functions ?


Quote:

Originally Posted by jeddiki View Post

but I also saw in their forum that a lot of people have trouble with it.

I use it and I don’t have any trouble. (I even use the Decorator Plugin). Nevertheless, batch sending is more effective, than using an external loop.
Reply