Step 1: Download the sample PHP file
- Download and extract the sample PHP file.
- For an explanation of the code used in this file, see Behind the Code.
Step 2: Copy the file to your Zend Server's document root
Step 3: Run your application
Open a Web browser and browse to the URL of your file, located at http://localhost:<port>/f2c_convert.php .
- Replace <port> with the port number on which your Web server listens.
- What is my port number?
Congratulations
You can now use your application!
The Fahrenheit / Celsius PHP Converter will be displayed.
- Enter a number in the "Enter number of degrees" field and use the drop-down list to select whether to convert from Celsius to Fahrenheit or from Fahrenheit to Celsius.
- Click the "Convert!" button to convert your temperature.
The result will be displayed in the browser.
What next?
- See Behind the Code, below, to learn about the PHP code you have just run.
- Access more sample applications which can be run on the Zend Server.
- To learn more about PHP basics, check out the PHP 101 category in the Zend Developer Zone.
Behind The Code
The “Temperature Convertor” sample application is a PHP application which allows you to take user input from a form field, perform mathematical operations on the input, and then print results to the screen.
To view the code behind the application, open the f2c_convert.php file you placed in your Document Root. You can view the code using Zend Studio or any other IDE or text editor.
Let's break up the code in our application to see what it is doing:
- <?php
This is the opening PHP tag.
- /**
- * 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
- *
- */
This is a comment.
Comments can be used anywhere in your PHP script to add information which can be read by developers looking at the code. They are ignored by the PHP interpreter and will not be run as code.
It is good programming practice to add comment to all elements in your code.
- In Zend Studio for Eclipse, code can be commented by selecting the relevant lines and pressing Ctrl + Shift + /.
- ?>
- <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>
This section of code contains mostly HTML code, which displays the input form.
Some interesting things to notice are:
- The first line is a closing PHP tag. This tells the PHP interpreter that the code following it should not be processed as PHP code, and should be printed out directly to the browser.
- Line #18 starts the HTML code for our form. The method="get" attribute sets the HTTP method to be used for submitting the form. The value of this attribute can be either ‘get’ or ‘post’.
- The ‘input’ tag at line #19 creates a text input field in the form. The name="degrees" attribute defines the name of the element. This name will later be used by PHP to perform mathematical operations on the user input.
- Lines #20 - #23 add an additional input field in the form of a drop-down list. Notice that each field in the form has a different ‘name’ attribute.
- Line #24 creates the form’s submit button, which in our case will say ‘Convert!’.
- <?php
- if (isset($_GET['degrees']) && isset($_GET['conversion']))
This block of code is the one which performs the conversion.
The switch statement at line #33 determines whether the degrees are converted from Fahrenheit to Celsius or vice versa, depending on the value of the $_GET['conversion'] variable:
- Lines #36 – 39 will be executed if the Celsius to Fahrenheit option (value=“ctf”) was selected by the user.
- Lines #42 – 45 will be executed if the Fahrenheit to Celsius option (value=“ftc”) was selected by the user.
Mathematical operations are performed on the value of the $degrees parameter in order to convert it into the appropriate result. This is then formatted into a number containing two decimal places (using the sprintf() function) and assigned to the $result variable.
At lines #38 or #44 a message to the user containing this result is constructed and assigned to the $message variable.
- // Print out the results
- echo "<hr /><strong><em>$message</em></strong><hr />";
- }
- ?>
- </body>
- </html>
Line #49 prints out the message assigned to $message. Note that this line is part of the “if” block starting at line #29 and will only be printed if the user has supplied input.
Lines #51 - #54 close the PHP block and the HTML page. These will be executed regardless of whether the user has submitted input.



