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.