...

View Full Version : Function vs require please help me!



jeddi
10-09-2009, 07:29 AM
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:


switch ($N_cat_cd)
{
case "ex": $Dcat = " Expert-World"; break;
case "se": $Dcat = " Self Improvement"; break;
case "s.": $Dcat = " S.E.O."; break;
case "tr": $Dcat = " Traffic"; break;
case "ni": $Dcat = " Niche-Affiliate"; break;
case "co": $Dcat = " Copy writing"; break;
case "sa": $Dcat = " Sales systems"; break;
case "pr": $Dcat = " Product creation"; break;
case "we": $Dcat = " Website Design"; break;
case "gr": $Dcat = " Graphics"; break;
}
$SEOcat = strtolower(trim($Dcat));

$cat_menu ="
<div class='menuCAT'>
<ul>
<li> <a href=\"/im/menu1.htm" > $SEOcat</a></li>
<li> <a href=\"/im/menu1.htm" > $SEOcat</a></li>
<li> <a href=\"/im/menu2.htm" > $SEOcat</a></li>
<li> <a href=\"/im/menu3.htm" > $SEOcat</a></li>
<li> <a href=\"/im/menu4.htm" > $SEOcat</a></li>
<li> <a href=\"/im/menu5.htm" > $SEOcat</a></li>
<li> <a href=\"/im/menu6.htm" > $SEOcat</a></li>
</ul>
</div>";



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

For example


$page="pr";
$title1 = " products and resources";
$desc=" internet marketing products";

if (@$_SESSION['auth'] == "yes" ){
require_once("mem_head.php");
} // end if
else {
require_once("a_head.php");
} // end if

$dir = "products";
$pub = "prod";
require_once("a_cat_menu.php");
?>

<div class="page_name">
<h1>Resources - Expert IM Products</h1>
</div> <!-- End div: page_name -->


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.


START
$swift_path = "/home/focus7/public_html/lib/swift_required.php";
$func_path = "/home/focus7/public_html/my_functions.php";
require_once("$swift_path");
require_once("$func_path");

$transport = Swift_SmtpTransport::newInstance('mail.MY-SITE.com', 25)
->setUsername('MY-NAME')
->setPassword('MY-PASS')
;

$mailer = Swift_Mailer::newInstance($transport);

$sql_client = "SELECT * FROM clients "; // OPEN
DATABASE & GET CLIENTS
$result_client = mysql_query($sql_client) or die("could not execute
FIND CLIENT query.". mysql_error());
while($row_client = mysql_fetch_assoc($result_client))
{ // START THE LOOP
extract($row_client);

$message = $message.$ek.$ek1.$ek2; // CREATE THE MESSAGE FROM
DATA

$simdi = date('H:m:s, l, j F Y',$today);

$to = $email; // CREATE THE
HEADERS
$contact = $contact;
$fm_email="Account-Mana...@support-focus.com";
$fm_name="Support Center";
$subject="Your Weekly Report";
$message_html = "Dear $contact,<br><br><b>Weekly Report: $simdi</
b><br><br>".$message”;

require("send_auto.php"); // SEE BELOW

} // end while // LOOPS THROUGH DATABASE ( ONLY 25

RECORDS )

THIS IS THE send_auto.php

START
$message = Swift_Message::newInstance();
send_auto.php

// set the sender
$message->setFrom(array($fm_email => $fm_name));

// set the recipient
$message->setTo(array($to => $contact));

// set the subject
$message->setSubject($subject);

// set the html body
$message->setBody($message_html, 'text/html');

// set the text body
$message->addPart($message_text, 'text/plain');

//Send the message
if (!$mailer->send($message, $failures)) {
// update failure database
$sql = "INSERT INTO `email_fail` (type,email,send_dt) VALUES
( '$send_type', '$to','$today')";
mysql_query($sql) or die("could not execute email_fail update
query". mysql_error());
} // end if
END


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

Thanks

V@no
10-09-2009, 03:59 PM
They were referring to use include in a loop, like include the same file several times.

Also, it makes sense that include 10 little files would be slower then include one larger file with 10 functions in it.

jeddi
10-10-2009, 07:18 AM
Thanks V@no for your comment.

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:



START
$swift_path = "/home/focus7/public_html/lib/swift_required.php";
$func_path = "/home/focus7/public_html/my_functions.php";
require_once("$swift_path");
require_once("$func_path");

$transport = Swift_SmtpTransport::newInstance('mail.MY-SITE.com', 25)
->setUsername('MY-NAME')
->setPassword('MY-PASS')
;

$mailer = Swift_Mailer::newInstance($transport);

$sql_client = "SELECT * FROM clients "; // OPEN
DATABASE & GET CLIENTS
$result_client = mysql_query($sql_client) or die("could not execute
FIND CLIENT query.". mysql_error());
while($row_client = mysql_fetch_assoc($result_client)) { // START THE LOOP
extract($row_client);

$message = $message.$ek.$ek1.$ek2; // CREATE THE MESSAGE FROM
DATA

$simdi = date('H:m:s, l, j F Y',$today);

$to = $email; // CREATE THE
HEADERS
$contact = $contact;
$fm_email="Account-Mana...@support-focus.com";
$fm_name="Support Center";
$subject="Your Weekly Report";
$message_html = "Dear $contact,<br><br><b>Weekly Report: $simdi</
b><br><br>".$message”;

$message = Swift_Message::newInstance();

// set the sender
$message->setFrom(array($fm_email => $fm_name));

// set the recipient
$message->setTo(array($to => $contact));

// set the subject
$message->setSubject($subject);

// set the html body
$message->setBody($message_html, 'text/html');

// set the text body
$message->addPart($message_text, 'text/plain');

//Send the message
if (!$mailer->send($message, $failures)) {
// update failure database
$sql = "INSERT INTO `email_fail` (type,email,send_dt) VALUES ( '$send_type', '$to','$today')";
mysql_query($sql) or die("could not execute email_fail update query". mysql_error());
} // end if

$content = "Sent Weekly Report to $contact at $email \r\n";
fwrite($handle, $content);
} // end while // END OF LOOP
fclose($handle);
?>


I don't see any point in creating a function
( or there ) ??



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum