473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

290 100+
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.


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
Oct 9 '09 #1
5 1896
Dormilich
8,658 Expert Mod 8TB
@jeddiki
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…
Oct 9 '09 #2
jeddiki
290 100+
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 ???
Oct 10 '09 #3
Dormilich
8,658 Expert Mod 8TB
@jeddiki
if you’re sending to multiple recepients (I think you are), you can use Swiftmailer’s batchSend() method
Oct 10 '09 #4
jeddiki
290 100+
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 :)
Oct 10 '09 #5
Dormilich
8,658 Expert Mod 8TB
@jeddiki
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.
Oct 10 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Joe Mowry | last post by:
First off, I'm posting this question for a friend that doesn't have access to the news groups. Problem: Using PHP as the base and has several PHP < includes > for various functions, all this...
0
by: Martin Walke | last post by:
Hi guys, For those who responded before here's an update from hell!!! I think I confused people before when I said on the 'page load' and Chris suggested using the body onload. I was using the...
9
by: John Doe | last post by:
Hi all, Regarding those cyclic dependencies of classes (like cases in which class A refers to class B and class B to class A): can they ALL be resolved by forward defining classes, and by...
8
by: Jim | last post by:
Hi: Do we have some common style for includes when working on a project with lots of c and h files. Wat I mean is do we have a rule in C when a file includes several files and those file in turn...
10
by: Ben Taylor | last post by:
Hi, Coming from VB, I've still not really grasped the way how in C++ if function A wants to call function B, then function B has to be before function A in the compilation, and #includes that use...
12
by: tshad | last post by:
I am not sure why I am getting this error: I have the following code I want to run from another include file that holds all my functions. functions.inc...
11
by: Eigenvector | last post by:
I apologize if this is a trivial question, but it's always made me wonder when I have to compile my code. There are some #includes that you don't really need to reference in your library and...
5
by: pengypenguin | last post by:
Hello all, I am trying to create a user authentication system, and I would like to separate the authentication code into include files. As I see it, the basic flow would go something like this:...
6
by: Richard | last post by:
I am new to php and am looking for some quick "get up and go" help. What would the arguments for and against be for function declarations v simple include? e.g <?php include("myfunc.php");...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.