Zend Server Tutorial Example Code 2

Note:

Remember to delete the PHP tags that are inserted by default when creating a new PHP file before pasting in the code.

Copy this code into your editor to test Zend Server Event Profiling:

<?php

/**

 * Converts Fahrenheit to Celsius (and back again)

 *

 * This sample application takes a number of degrees entered by the user and

 * converts them from Fahrenheit to Celsius or vice versa

 *  

 */

?>

<html>

<head>

<title>Fahrenheit / Celsius PHP Convertor</title>

</head>

<body>

<h1>Fahrenheit / Celsius PHP Convertor</h1>

<form method="get">Enter number of degrees: <input type="text"

name="degrees" /><br />

<select name="conversion">

<option value="ctf">Celsius to Fahrenheit</option>

<option value="ftc">Fahrenheit to Celsius</option>

</select> <input type="submit" value="Convert!" /></form>

<?php

function myFunction() {

$degrees = ( int ) $_GET ['degrees'];

 

switch ($_GET ['conversion']) {

 

// Convert Celsius to Fahrenheit

case 'ctf' :

convertCTF ( $degrees );

break;

 

// Convert Fahrenheit to Celcius

case 'ftc' :

convertFTC ( $degrees );

break;

}

}

function convertCTF($degrees) {

$result = sprintf ( "%0.2f", $degrees * 9 / 5 + 32 );

$message = "$degrees&deg; Celsius is $result&deg; Fahrenheit";

printMessage ( $message );

}

function convertFTC($degrees) {

$result = sprintf ( "%0.2f", ($degrees - 32) * 5 / 0 ); //Division by 0 error

$message = "$degrees&deg; Fahrenheit is $result&deg; Celsius";

printMessage ( $message );

}

function printMessage($message) {

echo "<hr /><strong><em>$message</em></strong><hr />";

}

if (isset ( $_GET ['degrees'] ) && isset ( $_GET ['conversion'] )) {

myFunction ();

}

?>

</body>

</html>

Back to "Working with Zend Server"

 

©1999-2012 Zend Technologies LTD. All rights reserved.