PHP With Example Code
PHP With Example Code
Preprocessing
Chapter 08
Overview and Objectives
• Present a brief history of the PHP language
• Discuss how PHP fits into the overall web picture
• Discuss PHP script development and testing
• Incorporate into our home page a PHP-generated welcome message
• Show how to cause our home page to be refreshed (reloaded) every so often
• Implement the server-side functionality of our feedback form:
– sending the user’s feedback to the business via email,
– sending a copy of the e-mail to the client,
– storing a copy of the feedback on the server
• Implement an alternate version of our BMI calculator
• Show how to send HTML-encoded e-mail
• Use the POST value of the method attribute of the form element to submit form data
• Discuss several features of the PHP language, as required for our purposes:
– Comments
– variables (including superglobals),
– numerical and string data types,
– expressions and operators, arrays,
– built-in and programmer defined functions,
– file output,
– server-side inclusion of files
<?php
//processFeedback.php
//Send the e-mail feedback message to the business (but here, to the webbook site)
$headerToBusiness = "From: $_POST[email]\r\n";
mail("[email protected]", $_POST['subject'], $messageToBusiness, $headerToBusiness);
//Transform the confirmation message to the client into XHTML format and display it
$display = str_replace("\r\n", "<br />\r\n", $messageToClient);
$display =
"<html><head><title>Your Message</title></head><body><tt>".
$display.
"</tt></body></html>";
echo $display;
<?php
function simpleMessage($height, $heightUnit, $weight,
$weightUnit)
{
$bmi = sprintf("%.2lf", calculateBMI($height,
$heightUnit, $weight, $weightUnit));
$text =
"<h1>BMI Report</h1>".
"<h3>Your BMI is ".$bmi.".</h3>";
return $text;
}