Advertisement
  • Add a Comment
  • Print
  • Share

How to Program a Basic WordPress PHP File

To make sure you understand the basics of PHP, including how to start and stop PHP within a file, try your hand at a little sample of 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) and then 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. All HTML tags need to be opened and then closed, just like PHP tags. 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> and then press Enter.

    This closes the <head> tag from the previous step.

  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 and then press Enter.

    This tag tells the web browser to start a PHP function.

  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 website.

  8. Type ?> and then press Enter.

    This tag tells the web browser to end the PHP function.

  9. Type </body> and then press Enter.

    This tells the web browser to close the <body> HTML tag from and that you’re done with the body of the web page.

  10. Type </html> and then press Enter.

    This tage closes the <html> tag from the first step and tells the web browser that you’re at the end of the HTML document. 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>
  11. Save the file to your local computer.

    The file must be saved as a .php file.

  12. Upload the file via File Transfer Protocol (FTP).

    The new file must be located in the root directory of your web server.

  13. Open a web browser and type the address http://yourdomain.com/xxxxxx.php in the web browser’s address bar.

    In this example, yourdomain is your actual domain name and xxxxxx.php is the previously saved filename. If the file displays correctly in your browser, congratulations! You programmed PHP to work in a web browser.

    image0.jpg

If the file does not display correctly in your browser, you see some common PHP error messages that indicate what errors exist in your code, usually it gives the error message plus the line number where the error exists in the file.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement
Advertisement

Inside Dummies.com