PHP for the World Wide Web (Visual QuickStart Guide)

I l @ ve RuBoard

As those who hand code HTML understand, white space (blank lines, tabs, and other extra spaces) in your code can help maintain an uncluttered programming environment while not affecting what the viewer sees in the browser. By inserting blank lines between sections of code, placing nested elements one tab-stop in from their predecessor, and by spacing out code, a script will look more organized, making it easier to initially program and later debug. Thus, judicious use of white space in your work is highly encouraged and can be utilized in both your PHP and the resulting HTML.

Remember that there are three areas of Web development this book considers: the PHP scripts, the data that the PHP sends to the Web browser (mostly in the form of HTML), and how the Web browser interprets or displays that data. I'll briefly address the issue of white space for each of these.

When programming your PHP, you should understand that white space is generally (but not universally ) ignored. Any blank line (whether it be just one or several in a row) is completely irrelevant to the end result. Likewise, tabs and spaces, are normally inconsequential to PHP.

HTML code outside of PHP (Script 1.4, lines 15), can be spaced out just as you would within the PHP, but to address the layout of the HTML actually generated by PHP (Script 1.4, line 7), you will need to use special characters .

To change the spacing of your PHP and the data sent to the browser:

  1. Open first.php in your text editor.

  2. Insert a new line before and after the print command by pressing Return at the appropriate places.

    The new lines within the PHP serve only to add focus and clarity to our script.

  3. At the end of the print command (now on line 8), add \n, within the quotation marks (Script 1.5).

    Script 1.5. When we add blank lines we will not affect the appearance of the page in our browser but it does make our code more readable. For each \n we include in our print statement, another break will be inserted into our HTML source (not to be confused with <BR> which will create a return in how the browser displays the HTML).

    The \n character combination sends a command to the Web browser to start a new line in the HTML source. Think of it as the equivalent of pressing the Return key.

  4. Save your script, upload it to your server, and view it with your browser (Figure 1.5).

    Figure 1.5. Our page still looks the same in the browser with the new line as it did without it, since \n is not an HTML tag and the blank lines in the PHP code (lines 7 and 9) do not get sent to the browser.

While the above series of steps can make your PHP and HTML more readable, remember the additional white space will not affect the appearance of your page in the Web browser (Figure 1.5). To do that you must use HTML tags (the code to create a space in your browser is &nbsp; and the HTML equivalent of pressing Return is <BR> ).

Tip

One exception to the rule of immaterial white space in PHP would be extra spaces within a print statement, resulting in extra spaces being printed to the browser. However, HTML largely ignores white space as well.

Tip

To see what code was sent to the browser, use the "View Source" or "View Page Source" feature built into all browsers. You will quickly be able to tell the difference between using a new line and not using a new line (Figures 1.6 and 1.7). While it may be hard to appreciate the benefits of white space on a twelve-line script, this will become more significant as your scripts become larger and more complicated.

Figure 1.6. Viewing the source of a Web page is a good way to determine where a formatting problem may exist. This is the source code from our script before we added the \n and blank lines.

Figure 1.7. The \n we put into our print statement separates the "Hello, world!" line from the other HTML tags. When sending more complicated HTML code to our browser, using new lines helps simplify the look of the source.

Tip

There is a school of thought that recommends condensing your HTML code into the tightest possible package by eliminating all superfluous spaces. The thinking behind this is that you will increase the download speed of the page once you are no longer transmitting extra spaces. While the notion is not without merit, doing so is not conducive to practicing and learning.

I l @ ve RuBoard

Категории