Entry Level PHP Web Application Development/Our First Programs: Hello, World!
The Tradition of “Hello, World!”Edit
“Hello, World!” is often the first program any student of a new language learns. It just so happens that since we are covering multiple languages and approaches in this book, you are going to see different versions of “Hello, World!” for each way of tackling the problem. Not all examples in this book will be presented form all the different methods possible. However, it is important to realize that any time you are working with development on a web application, there are always different ways to solve a single problem.
“Hello, World!” in plain HTML 4.01Edit
The first way that “Hello, World!” can be written is as a static HTML 4.01 page. HTML itself will form the basis for nearly every single web application you will ever write, and so it is imperative to not underestimate the importance of correct HTML.
This will simply display “Hello, World!” in your browser window, with “Hello, World!” as the page title.
What is the DOCTYPE?Edit
The first line in the HTML document above is the DOCTYPE—the Document Type. The Document Type is what tells the web browser how to interpret the document that follows it. There are several document types that are valid for web applications written today. It is important that you choose the correct one for your application, and then stick with it.
The document type above tells the browser to use HTML 4.01 Strict—which is the document type that we will use throughout this entire book. There are other document types available for use:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
This is HTML 4.01 Strict. If you have used HTML in the past, such as HTML 3.2 or HTML 4 Transitional, this is probably the hardest to get used to. If you are new to HTML, it is the best thing for you to learn to start with. Either way, future versions of HTML (known as XHTML) will be most like HTML 4.01 Strict, and it is recommended for non-XHTML documents.
We use HTML 4.01 Strict in this book because as of this time (June, 2006) XHTML 1.1 is still unsupported in Microsoft Internet Explorer, which holds the plurality (if not the majority) on the browser market. Once Microsoft Internet Explorer contains support for the XHTML 1.1 standards, it would be prudent (and logical) to use XHTML 1.1 as the default document type for new web pages and applications.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
This is HTML 4.01 Transitional, which permits “old-style” HTML to be written with HTML 4. Most notably, HTML 4.01 transitional adds presentational and depreciated elements to the HTML 4.01 Strict standard. It is recommended for use by those who need to make use of elements that are included. There is a list of elements that are depreciated available on the Internet.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
This is used for frameset pages. HTML permits frameset pages, which define frames within a browser window, and then load content into those frames. The use of frames on a page is debated in general, and generally not encouraged. However, like all rules, there are exceptions.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
These are the XHTML document types. XHTML is an XML-ified version of HTML. It is better then HTML, however, full support from Microsoft Internet Explorer is still lacking (as of June 2006), which makes it nonviable at present for use outside of controlled environments.
The Content-Type of the DocumentEdit
Every document served on the Internet contains a “Content-Type” header. For HTML based web applications, the correct Content-Type header that has to be passed is:
It is also recommended that you specify the character set that your application is using. UTF-8 is a common choice, since it contains most of the characters that anybody would use in many various languages. For an application using UTF-8, the proper Content-Type header is:
Content-Type text/html; charset=utf-8
What is “correct” HTML?Edit
There are some basic rules that you can follow to try to make sure that you are consistently writing proper HTML:
- If an element has a close tag, use it.
- Do not make tag-soup—that is, do not start a paragraph with <p>, and then start a new paragraph with <p> without finishing the first one.
- Use lowercase HTML tags when using HTML 4.01 or any version of XHTML. Versions of HTML prior to 4.0 use upper-case tags, and are depreciated.
- Avoid in-line style attributes. Use classes and id attributes with cascading style sheets to render the effect, instead, if possible. (Sometimes, one must use inline style information; however, the behavior is technically depreciated—content and presentation should never be mixed with each other in that way.)
- DO NOT use XHTML-style empty tags in HTML 4.x documents. Some browsers (e.g., Microsoft Internet Explorer) choke on these tags when they are in HTML 4.x documents, and do not know how to parse them. Results can be unpredictable.
- Use the W3C Validation Service to validate your work as you go. If you are working on a project that is internal to an intranet, as opposed to on the public Internet, still take care to validate using the service, though you will need to paste the markup in for validation.
- Some larger corporations (such as AT&T) may have their own internal copy of a document validator. When you have such a validator available, USE IT—it allows you to verify confidential content that cannot be transmitted across the open Internet.
- If necessary, mirror the W3C validation service locally somehow.
What is there to learn from this example?Edit
This is essentially a small program that instructs a web browser to display information, with a title that is useful to the user. It contains all of the proper elements of valid HTML 4.01 Strict, which means that it should render properly in any web browser that supports HTML 4.01 Strict. This is the smallest document that can be used, and contains no style information (which we will learn about later).
In this example, you have learned about document types (which are required for valid HTML documents), setting the Content-Type header with a character set (which is important to ensure that it is displayed properly, especially if it contains non-ASCII characters such as ß, ¥, ¶, ©, or ®), and you see the basic structure of an HTML document. We will build on this information as we go, including the three other examples to come below.
“Hello, World!” with PHPEdit
In the previous example, you witnessed how HTML 4.01 can stand on its own with regular, static content. This means that when an HTML document is saved on the web server, as the one above, it will never change without human interaction. Somebody has to edit and save the file, and then upload it to the server again, for anybody else to witness the change.
In this example, you can see that some of the file has changed. We have saved the file with a new extension - .php, which tells the server that it has to be handled on the server. Also, there are new items within the file. We will take a look at the components that we find in this program.
“Opening” and “Closing” PHPEdit
PHP files are somewhat strange in the computer world—the file is actually usually only part PHP. The rest of the file is whatever the output language is; in our case, this would be HTML 4.01 Strict. For this to be possible, there has to be a way for the application writer to tell PHP where to start and stop looking for PHP code.
For this, we use the PHP open tag (<?php) to tell PHP to start reading instructions, and the PHP close tag (?>) to tell PHP to stop reading instructions. Everything between the start and end tags is read and acted on by PHP. Everything that is outside of the tags is passed, unobstructed. On line 1, you see the first PHP open tag, and on line 11, you see the first closing tag. Everything between them is run by PHP.
Comments in PHPEdit
The next things that you come across in the Hello, World program above are comments. There are two different types of comments generally used in PHP. The first comment we come across is the multi-line comment, which starts on line 2, and stops on line 5. This comment starts with /* and ends with */. These types of comments are borrowed from the C computer language.
Multi-line comments are good for remarks that span multiple lines. In the example above, we used the multi-line comment for demonstration purposes. It is generally useful at the header of a file, to give a brief overview of what the file's purpose is. Some people use the header for information on copyright, license information, and other metadata that pertains to the file. Over time, you will develop your own style for how you write code, including how you structure your files within projects.
An example of a header with metadata for a file that manages database connections might look like:
<?php /* * Database Library for MySQL * Copyright © 2006 Michael B. Trausch, <email@example.com> * Released under the terms of the GNU General Public License v3 */
The second comment that we see, immediately after the first one, is a single line comment. This comment starts with the // characters, and ends at the end of the line. These types of comments are good for giving short descriptions of the next few lines of code, or a reason for doing something.
The biggest thing to remember about comments is that if they were unnecessary, they would not be included in the language. It is important to understand that code that is lacking in comments, as well as code that is over-commented, can become hard to read and understand by other people—or even yourself, later on. Many programmers understand the code that they are writing at the time, however, when they return to it six months later, do not have a clue why they wrote things the way that they did. It is important to know when to comment, and when not to. Over time, you will learn when it is appropriate to place comments in your code.
PHP has a large library of functions that you can use to help your code actually do things. In the example above, the PHP header() function is used to send an HTTP header to the web browser. This is equivalent (and sometimes, with older browsers, more effective) then the HTML <meta> tag that was used in the first example.
PHP has an extremely large number of functions that you as a programmer can call upon. Fortunately, the PHP Manual is an extremely easy-to-use resource which classifies the available functions, as well as allowing you to search them. As a PHP application developer, the PHP Manual can be one of the most important resources you have. As you go through this book, you will find more PHP functions that we will use.
Part of the big deal with web application programming, and PHP in particular, is that you want content to be dynamic—you do not want a static set of pages that will never change. Variables are the cornerstone of dynamic pages. If PHP did not have variables of any type, it would be extremely hard to make use of other features of the language to help process, store, and retrieve data.
Variables are probably the most used items in PHP, and arguably, any programming or scripting language. It is important to make sure that you understand what they are, and how they are used. Variables are containers of data, put bluntly. Each one of them has a name. In our example Hello, World script above, our first variable that we create is:
$hello_text = 'Hello, World!';
All we are doing in this line is assigning the text “Hello, World!” to the variable that we are calling $hello_text. Now, it is possible to change this variable at any time. We can make the $hello_text variable hold the text “Hello, Betsy!” or even the number 42!
We will learn more about variables a little bit later in the book. For now, it is important to realize that when you are assigning the variable, the name of the variable must be on the left, and the new contents of it must be on the right. An equal sign goes in-between them.
One more thing that we need to know immediately, is that there are two types of text strings in PHP code. You may notice that on line 9, we use the single quote, and on line 10 we use the double-quote. There is a difference between them. The single quote character (') specifies an absolute literal string. Anything that is between single-quote marks will be stored exactly as it appears.
The double-quote, however, is a little bit different. There are some neat tricks that you can perform with double-quoted strings. We see one of these tricks on line 10: we include the contents of one variable, into another. This is called variable interpolation, and is a common practice in some types of languages. This is a feature of PHP that you will grow to use quite often in your programs, and you will see it very often in other programmer’s code. Taking a look at line 10:
$page_title = "$hello_text (PHP Version)";
We can see that there is a double-quote there. Since we know (in this case) what $hello_text contains, we can look at this line, and “see” what PHP gets from it:
$page_title = 'Hello, World! (PHP Version)';
PHP replaces the reference to $hello_text with what the contents of $hello_text were at the time. We will take a closer look at this later on the book.
On Variables: camelCase or underscores?Edit
Coding styles differ from project to project, and person to person. One major issue in coding style is how to name the variables. You have seen that we have chosen to use underscores—for example,
$hello_text—instead of “camelCasing” (e.g.,
$helloText). The reason for this is experience; the author has found that using camelCasing tends to create situations in many programming languages where it is very easy to type the name of a variable incorrectly, and this can cause problems that are hard to debug and fix. One example would be if
$helloText were to accidentally be typed as
$helloTExt—a common occurrence when the <SHIFT> key is hard to press or sticks—and is not easy to spot when quickly glancing at code to see what the problem might be.
Due to the way that PHP operates, it’s easier to create correct code if camelCasing is avoided. It may take some time to initially get used to the idea of using underscores to separate words in variable names, but it is a very useful way of ensuring that there is consistency in the way that things are named. Keep in mind, though, that if you use other people’s code—say someone else’s class libraries—you may find that they have chosen to use camelCasing, and using at least a little bit of it is unavoidable.
Whatever style of casing you use, if you are creating a new Web application with PHP, it is best to describe the coding style in a file contained within the project as an easy point of reference, and remain consistent with it.
Other Variations of “Hello, World!”Edit
Answer the following questions. Writing the answers down (or typing them out) after thinking about them and reviewing for the answers can help you to better remember the information for use later. After you have answered the questions, compare your answers to the ones here in the book.
- What is a variable, and what is the purpose of using one? Answer
- What is the purpose of a single-quoted string? Answer
- What is the purpose of a double-quoted string? Answer
- Re-write the Hello, World program in PHP to use another variable called $user_name. Store your name in that variable, and make the program say "Hello yourname, welcome to the world!" Answer
We have gone over a great deal of information here. You are encouraged to do some practicing to help use some of the ideas presented within this page.
- Check out the W3 HTML validation program available on the Internet. View your examples in the web browser, and paste the output from your browser’s “View Source” option into the validation service. Remember this tool—it will be important to you in the future!
- View the PHP Manual and get a feel for how to get around it. This is another important resource that you will use often in development.
- ^ XHTML 1.0 Transitional may be used with a text/html Content-Type header, however, there are restrictions on how it can be done. More information on XHTML is available in the XHTML Appendix.
- ^ UTF-8 is an eight bit character set which represents Unicode characters among computer systems. Unicode is used as standard in many installations of operating systems, and is capable of representing the entire Unicode character set standard. More information on UTF-8 is available on Wikipedia.
- ^ Sometimes, the new changes will not be picked up immediately by a web browser that has previously viewed the document. The example used in Section 2 may be changed to read “Hello, User!” and it may not be picked up right away. We will see later in this book how to combat that problem, as it is a problem that all web applications have to deal with.
- ^ There is also a short PHP open tag (<? ... ?>). However, it is not guaranteed to be enabled on all systems. Furthermore, it is possible that if enabled, it can interfere with XHTML documents. (XHTML Proper has to start with <?xml...?>, and PHP sees the <? as the short start tag.) There more information listed in the XHTML Appendix.
- ^ The PHP header function is used to send any HTTP header to the web browser. This function must be used before any other output in your program. It is an extremely popular function, and we will see more on it later in this book.