473,699 Members | 2,302 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

290 Contributor
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 1906
Dormilich
8,658 Recognized Expert Moderator Expert
@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 Contributor
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 Recognized Expert Moderator Expert
@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 Contributor
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 Recognized Expert Moderator Expert
@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
1606
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 by its self works just fine. Failure occurs on adding the XML include. Here's the wierd: If all the PHP includes are removed/commented out,
0
1186
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 onLoad of the body tag anyway to start my JS function. Anyway, I changed the order of my includes such that the one that calls all the rest was loaded last and that worked.....or so I thought.
9
1614
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 implementing the actual functions outside the class definition, OR there are STILL cases which cannot be solved in this way and could require rewriting your code / rewriting your class hierarchy?
8
1924
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 can call back it. So do we keep the includes in the source file or in its header file and just call the header from the c file. Are there any more rules when we go for includes. Thanx to yu all who bother to share.... su
10
1349
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 the classes in others rely on others have to be after them accordingly. I've got three questions about this if I may, 1) When I create an MFC application by having a dialog
12
2988
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 ************************************************************************************ <Script runat="Server"> Sub fnHeader(client As String) response.write("<!-- #include file = ../includes/staffingHeaders.inc -->")
11
1937
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 header references in the compilation string. For instance the standard hello.c #include <stdio.h> int main()
5
1396
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: if (not authentic) { display login } else { display content
6
1424
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"); // declares a function "myfunc". myfunc("hello");
0
8689
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7752
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5875
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4376
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3058
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2010
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.