Section 3.1. PHP and HTML Text


3.1. PHP and HTML Text

It'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 Output

You'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

 <html>     <head>         <title>Hello World</title>     </head>     <body>         <p>I sure wish I had something to say.</p>     </body> </html> 

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

 <html>     <head>         <title>Hello World</title>     </head>     <body>         echo("<p>Now I have something to say.</p>");     </body> </html> 

3.1.1.1. Separating PHP from HTML

This 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

 <html>     <head>         <title>Hello World</title>     </head>     <body>         <?php         echo ("Hello world!<br />");         print ('Goodbye.<br />');         print 'Over and out.';         ?>     </body> </html> 

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

 <html> <head>         <title>Hello World</title>     </head>     <body>         Hello world!<br />Goodbye.<br />Over and out.     </body> </html> 

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 browser


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

Comments are retained in the PHP file, but the interpreter doesn't output the PHP comments. The interpreter outputs only the HTML comments.


Example 3-5. Using comments to make your code easier to read

 <html>     <head>         <title>Hello World</title>     </head>     <body>         <?php         // A single line comment could say that we are going to         // print hello world.         /* This is how to do a            multi-line comment and could be used comment out a block            of code */         echo("Hello world!<br />");         print('Goodbye.<br />');         ?>     </body> </html> 

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.

Since PHP files tend to switch back and forth between PHP code and HTML markup, using an HTML comment in the middle of PHP or a PHP comment in the middle of HTML makes a mess of your page, so be extra vigilant in not doing this!


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.



Learning PHP and MySQL
Learning PHP and MySQL
ISBN: 0596101104
EAN: 2147483647
Year: N/A
Pages: 135

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net