3.1. PHP and HTML TextIt's simple to output text using PHP; in fact, handling text is one of PHP's specialties. We'll begin with detailing where PHP is processed, some of the basic functions to output text, and from there go right into printing text based on a certain condition being true. 3.1.1. Text OutputYou'll want to be able to spit out text easily and often. PHP will let you do that, though you'll need to use proper PHP syntax when creating the code. Otherwise, your browser assumes that everything is HTML and outputs the PHP code directly to the browser, and then everything looks like text and code mixed up. This will certainly confuse your users! You can use whatever text editor you like to write your PHP code, including Notepad or DevPHP (http://sourceforge.net/projects/devphp/). Our examples will demonstrate how similar HTML markup and PHP code look and what you can do to start noticing the differences between them. Example 3-1 is a simple HTML file that we'll use for an example. Example 3-1. All you need to start with PHP is a simple HTML document
Nothing is special here; just your plain vanilla HTML file. However, you can enter PHP right into this file; for example, let's use PHP's echo command to output some text in Example 3-2. Example 3-2. Adding some PHP code to the HTML file
3.1.1.1. Separating PHP from HTMLThis looks pretty simple, but there are some problems. There's no way to tell in this file which part is standard HTML and which part is PHP and therefore must be handled differently. To fix this, surround your PHP code in <?php and ?> tags. When you start writing PHP code, you'll be working with plain vanilla text files that contain PHP and HTML code. HTML is a simple markup language that designates how your page looks in a browser, but it is simply that: text only. The server doesn't have to process HTML files before sending them to the user's browser. Unlike HTML code, PHP code must be interpreted before the resulting page is sent to the browser. Otherwise, it will be one big mess on the user's screen. To set apart the PHP code to inform the web server what needs to be processed, the PHP code is placed between formal or informal tags mixed with HTML. Example 3-3 uses echo and print operators to achieve this. Example 3-3. Calling echo and print
When a browser requests this file, PHP interprets it and produces HTML markup. Example 3-4 is the HTML that is produced from the code in Example 3-3. Example 3-4. The HTML markup produced by the PHP code
Save your HTML document to C:\Program Files\Apache Group\Apache2\htdocs, as we discussed in Chapter 2. Open the file in a web browser, and you see something like Figure 3-1. The code in Example 3-4 is the same code that you see if you select View Page Source from your browsers menu. Figure 3-1. The output as it appears in the web browserWhile writing PHP code, it's crucial to add comments so that your code is easier to read and support. Most people don't remember exactly what they were thinking when they look at the code a year or more later, so let comments permeate your code and you'll be a happier PHPer in the future. PHP supports two styles of comments. We suggest using single-line comments for quick notes about a tricky part while using multiple-line comments when you need to describe something in greater depth; both are shown in Example 3-5.
Example 3-5. Using comments to make your code easier to read
In Example 3-5, two comment styles are used: // for single-line comments and /* ... */ for multiline comments. Keep in mind that if you want to place a comment in HTML markup, you need to use the open comment <! and close comment > tags. A semicolon (;) ends all code statements in PHP. (Because of this, semicolons can't be used in names.) It's good style as well as practical to also start a new line after your semicolon so the code is easier to read.
The PHP files get to your web site just like any other file. To try the PHP code in Example 3-6, save the file in the document root that you selected when you installed Apache in Chapter 2. Once you have your PHP filesay, example.phpin your web accessible directory, you can view it by browsing to http://yourdomain.com/your_directory/example.php. Now that you know how to include PHP code within your HTML markup and not let your user see a bunch of gobbledygook, we'll explore basic PHP programming. |