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:
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.
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.
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.
Type </head> and then press Enter.
This closes the <head> tag from the previous step.
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.
Type <?php and then press Enter.
This tag tells the web browser to start a PHP function.
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.
Type ?> and then press Enter.
This tag tells the web browser to end the PHP function.
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.
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>
Save the file to your local computer.
The file must be saved as a .php file.
Upload the file via File Transfer Protocol (FTP).
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.
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.