View Single Post
  #6 (permalink)  
Old 08-07-2007, 01:48 PM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Admin
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 440
iTrader: (0)
TeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to behold
PHP functions are very much like functions in other languages with the exception that you don't have to declare the return type.

Let's say that I have a function called getSubject that does the processing to determine the subject of an email and I want to 1) use it in the subject line of an email and 2) return to the end user the results. This code could look like this:
PHP Code:
<?php
function getSubject() {
  
//process the subject and store it in a variable, $subject;
  
return $subject;
}

//Send the email.  notice that since $subject is returned by getSubject(), it's value is being passed directly to the mail function
mail ($to,getSubject(),$body);

//Confirm to user:

echo "Your email to $to has been sent.  The subject was <u>".getSubject()."</u> and the body was <textarea style=\"width:100%;height:8em;\">$body</textarea>";
?>
Now, that calls the function twice which may be undesirable, so you could also do this:
PHP Code:
<?php
function getSubject() {
  
//process the subject and store it in a variable, $subject;
  
return $subject;
}

$calculated_subject getSubject();

//Send the email.
mail ($to,$calculated_subject,$body);

//Confirm to user:

echo "Your email to $to has been sent.  The subject was <u>".$calculated_subject."</u> and the body was <textarea style=\"width:100%;height:8em;\">$body</textarea>";
?>
There are a few other ways of achieving the same task, but I think this addresses your question.

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote