Trying Out a Little PHP

To test some PHP code, follow these steps to create a simple HTML Web page with an embedded PHP function:

  1. Open a new, blank file in your default text editor — Notepad (Windows) or TextMate (Mac) — type <html>, and then press Enter.

    The <html> tag tells the Web browser that this is an HTML document and should be read as a Web page.

  2. Type <head> and then press Enter.

    The <head> HTML tag contains elements that tell the Web browser about the document; this information is read by the browser but hidden from the Web page visitor.

  3. Type <title>This is a Simple PHP Page</title> and then press Enter.

    The <title> HTML tag tells the browser to display the text between two tags as the title of the document in the browser title bar. (Note: All HTML tags need to be opened and then closed, just like PHP tags that we describe in the preceding section. In this case the <title> tag opens the command, and the </title> tag closes it and tells the Web browser that you're finished dealing with the title).

  4. Type </head> to close the <head> tag from Step 2 and then press Enter.
  5. Type <body> to define the body of the Web page and then press Enter.

    Anything that appears after this tag displays in the Web browser window.

  6. Type <?php to tell the Web browser to start a PHP function and then press Enter.

    See the preceding section on starting and stopping PHP functions.

  7. Type echo ‘<p>Testing my new PHP function</p>’; and then press Enter.

    This is the function that you want PHP to execute on your Web page. This particular function echoes the text “Testing my new PHP function” and displays it on your Web site.

  8. Type ?> to tell the Web browser to end the PHP function and then press Enter.
  9. Type </body> to close the <body> HTML tag from Step 5 and then press Enter.

    This tells the Web browser that you're done with the body of the Web page.

  10. Type </html> to close the <html> tag from Step 1 and then press Enter.

    This tells the Web browser that you're at the end of the HTML document.

When you're done with Steps 1–10, double-check that the code in your text editor looks like this:

<html>
<head>
<title>This is a Simple PHP Page</title>
   </head>
   <body>
   <?php echo '<p>Testing my new PHP function</p>'; ?>
   </body>
   </html>

After you write your code, follow these steps to save and upload your file:

  1. Save the file to your local computer as testing.php.
  2. Upload the testing.php file.

    Via File Transfer Protocol, upload testing.php to the root directory of your Web server. If you need a review on how to use FTP to transfer files to your web server, review the information presented in Book II, Chapter 2.

  3. Open a Web browser and type the address (http://yourdomain.com/testing.php) in the Web browser's address bar (where yourdomain is your actual domain name).

    A single line of text displays: Testing my new PHP function, as shown in Figure 3-1.

If the testing.php file displays correctly in your browser, congratulations! You programmed PHP to work in a Web browser!

image If the testing.php file doesn't display correctly in your browser, a PHP error message will give you an indication of the errors in your code. (Usually included with the error message is the line number where the error exists in the file.)

Figure 3-1: A basic PHP page in a browser window.

image

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.123.238