Preprocessor (And, Yes, We're Aware PHP Is A "Recursive Acronym"-Probably Meant To Confuse The Masses)
Preprocessor (And, Yes, We're Aware PHP Is A "Recursive Acronym"-Probably Meant To Confuse The Masses)
PHP stands for PHP: Hypertext Preprocessor (and, yes, were aware PHP is a recursive acronymprobably meant to confuse the masses). Its flexibility and relatively small learning curve (especially for programmers who have a background in C, Java, or Perl) make it one of the most popular scripting languages around. PHPs popularity continues to increase as businesses, and individuals everywhere embrace it as an alternative to Microsofts ASP language and realize that PHPs benefits most certainly outweigh the costs (three cheers for open source!). According to Netcraft, PHP code can now be found in more than 16 million Web sites. You can download the PHP software from PHPs Web site at www.php.net.
PHP programs are written using a text editor, such as Notepad or WordPad, just like HTML pages. Unlike HTML, though, PHP pages, for the most part, end in a .php extension. This extension signifies to the server that it needs to parse the PHP code before sending the resulting HTML code to the viewers Web browser. HTML can also be written inside the PHP section of your page; this allows you to format text while keeping blocks of code together. This will also help you write organized, efficient code, and the browser (and, more importantly, the viewer) wont know the difference.
You should always keep in mind these two basic rules of PHP: PHP is denoted in the page with opening and closing tags, as follows: <?php ?> PHP lines end with a semicolon, generally speaking: <?php // First line of code goes here; // Second line of code goes here; // Third line of code goes here; ?>
<?php // First line of code goes here; /* Second line of code goes here; Third line of code goes here;*/ ?> You can add comments in your program, as in the preceding code, through double slashes (//) for oneliners or /* and */ for opening and closing comment tags that may extend over several lines of code. Indents dont matter, and, neither do line returns.
You cant get much simpler than this first program, but try it out to get a feel for what the results look like. The PHP function echo, seen in the material that follows, is one of the most commonly used PHP functions. It is used to send text (or variable values or a variety of other things) to the browser.
1. Enter the following program in your favorite text editor (Notepad, WordPad, or whatever), and save it as firstprog.php. Make sure you save it in a plain text format to avoid parsing problems, and if youre using Notepad, double-check to ensure that the file is not saved as firstprog.php.txt by default.
<html> <head> <title>My First PHP Program</title> </head> <body> <?php echo Im a lumberjack.; ?> </body> </html>
2. Open this program using your browser. 3. Now view the source of the HTML code so you can see what happened with the PHP portions of the code. As you can see, the PHP portion of the code has vanished, leaving only the resulting HTML code.
4. Now add the following highlighted line to your script so you can get a better feel for how your PHP code will be parsed:
<html> <head> <title>My First PHP Program</title> </head> <body> <?php echo Im a lumberjack.; echo And Im okay.; ?> </body> </html>
5. Save the revised file and open it in your browser. As you can see, the line runs together without a line break, even though you had your PHP code on two different lines.
As you can see in the previous example, using only PHP code results in rather bland pages. You can make them look more professional and less utilitarian by adding some HTML to your PHP. HTML can be inserted within your PHP block of code using the echo function. Anything you can code in HTML, from frames, to tables, to font characteristics, can be inserted within a PHP section of code.
In this example, youll use some PHP and HTML together. 1. Modify the highlighted lines of firstprog.php:
<html> <head> <title>My First PHP Program</title> </head> <body> <?php echo <h1>Im a lumberjack.</h1>; echo <h2>And Im okay.</h2>; // pay attention to <h1> and <h2> tags ?> </body> </html>
You have to check for double quotes. As you may have noted when you worked through the previous example, using the echo function may involve the use of double quotation marks. Because HTML also uses double quotes, you can do one of two things to avoid problems: Use single quotes inside your HTML. Escape your HTML double quotes with a backslash, as in the following: echo <font size=\2\>; This is especially useful if you want to display double quotes in your text, such as: echo He was about 65\ tall.;
Example 1: <?php echo <table width=100% border=2 bgcolor=#FFFFFF>; echo <tr>; echo <td width=50%>; echo <font face=Verdana, Arial size=2>; echo First Name:; echo </font></td>; echo <td width=50%>; echo <font face=Verdana, Arial size=2>; echo $_POST[fname] echo </font></td>; echo </tr>; echo </table>;?>
Example 2: <table width=100% border=2 bgcolor=#FFFFFF>; <tr> <td width=50%> <font face=Verdana, Arial size=2> First Name: </font> </td> <td width=50%> <font face=Verdana, Arial size=2> <?php echo $_POST[fname]; ?> </font> </td> </tr> </table>
Although we have not yet discussed variables, you can see in the first example that the only thing PHP was really needed for was to provide the value held in the variable fname and display it on the screen. The rest of the related code was in HTML. In this instance, youre better off just staying in HTML and pulling out the PHP line when you need it, instead of coding the HTML inside the PHP. Although it really doesnt matter to the server, it makes for easier formatting, easier debugging, and less typing (which is always a good thing). In essence, it is up to you to balance your HTML with PHP and discover what works best for your coding style.
Aconstant is a placeholder for a value that you reference within your code. Constants are typically named with capital letters (so you can easily find them within your code), and the values are usually formally defined before using them. Constant names must begin with a letter or an underscore and cannot begin with a number. Names are also case-sensitive. You define a value assigned to a constant with the PHP function define(). Once youve defined a constant,it cant be changed or undefined.
In this exercise, youll see how you can use constants in your program. 1. Open your text editor and type the following program:
<html> <head> <title>My Movie Site</title> </head> <body> <?php define (FAVMOVIE, The Life of Brian); echo My favorite movie is ; echo FAVMOVIE; ?> </body> </html>
Unlike constants, variables are obviously meant to be variablethey are meant to change or be changed at some point in your program. Variables also do not need to be defined or declared and can simply be assigned when needed. Variables can hold either numeric or text values. Variables are denoted with a dollar sign ($) and are case-sensitive (in other words, $dateEntered and $DateEntered are not the same thing). The first letter of the variable name must be an underscore or letter and cannot be a number.
In this exercise, youll add variables to your existing script. 1. Open your text editor and make the following changes to your moviesite.php file (noted in highlighted lines):
<html> <head> <title>My Movie Site</title> </head> <body> <?php define(FAVMOVIE, The Life of Brian); echo My favorite movie is ; echo FAVMOVIE; echo <br>; $movierate = 5; echo My movie rating for this movie is: ; echo $movierate; ?> </body> </html>