What a PHP Script Looks Like PHP

PHP exists as a tag inside an HTML file. Like all HTML tags, it begins with a less-than symbol, or opening angle bracket (<) and ends with a greater than symbol, or closing angle bracket (>). To distinguish it from other tags, the PHP tag has a question mark (?) following the opening angle bracket and preceding the closing angle bracket. All text outside the PHP tag is simply passed through to the browser. Text inside the tag is expected to be PHP code and is parsed.

To accommodate XML and some picky editors such as Microsoft's Front Page, PHP offers three other ways to mark code. Putting php after the opening question mark makes PHP code friendly to XML parsers. Alternatively, you may use a script tag as if you were writing JavaScript. Lastly, you can use tags that appear like ASP, using <% to start blocks of code.

Listing shows an ordinary HTMLpage with one remarkable difference: the PHP code between the <? and the ?>. When this page is passed through the PHP module, it will replace the PHP code with today's date. It might read something like, Friday May 1, 1999.

Listing Printing Today's Date

Listing Printing Today's Date

Whitespace, that is spaces, tabs, and carriage returns, is ignored by PHP. Used judiciously, it can enhance the readability of your code.is functionally the same as the previous example, though you may notice more easily that it contains PHP code.

Listing Reformatting for Readability

<HTML>
<HEAD>
<TITLE>Listing 1-2</TITLE>
</HEAD>
<BODY>
Today's Date:
<?
/*
** print today's date
*/
print(Date("l F d, Y"));
?>
</BODY>
</HTML>

You may also notice that in Listing there is a line of code that begins with a slash followed by an asterisk. This is a comment. Everything between the /* and the */ is equivalent to hitespace. It is ignored. Comments can be used to document how your code works. Even if you maintain your own code you will find comments necessary for all but simple scripts.

In addition to the opening and closing comment statements, PHP provides two ways to build a single-line comment. Double-slashes or a pound sign will cause everything after them to the end of the line to be ignored by the parser.

After skipping over the whitespace and the comment in Listing , the PHP parser encounters the first word: print. This is one of PHP's functions. A function collects code into a unit you may invoke with its name. The print function sends text to the browser. The contents of the arentheses will be evaluated, and if it produces output, print will pass it along to the browser.

Where does the line end? Unlike BASIC and JavaScript, which use a line break to denote the end of a line, PHP uses a semicolon. On this issue PHP takes inspiration from C. The contents of the line between print and ; is a call to a function named date. The text between the opening and closing parentheses is the parameter passed to date. The parameter tells date in what form you want the date to appear. In this case we've used the codes for the weekday name, the full month name, the day of the month, and the fourdigit year. The current date is formatted and passed back to the print function.

The string of characters beginning and ending with double quotes is called a string constant or string literal. PHP knows that when quotes surround characters you intend them to be treated as text. Without the quotes, PHP will assume you are naming a function or some other part of the language itself. In other words, the first quote is telling PHP to keep hands off until it finds another quote.

Notice that print is typed completely in lowercase letters, yet date has a leading uppercase letter. I did this to illustrate that PHP takes a very lenient attitude toward the names of its built-in functions. Print, PRINT, and PrInT are all valid calls to the same function. However, for the sake of readability, it is customary to write PHP's built-in functions sing lowercase letters only.



Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PHP Topics