spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / php / corephp / 1 To page 1To page 2To page 3To page 4current page
[previous]

An Introduction to PHP

Subject Matter Expert - Managed Services (PA)
Next Step Systems
US-PA-Wayne

Justtechjobs.com Post A Job | Post A Resume
Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs


Editing Scripts

PHP scripts are just text files, and you can edit and create them just as you would HTML files. Certainly, you can telnet into your Web server and start creating files with vi. Or you can create files with Notepad and use FTP to upload them one by one. But these aren't ideal experiences. One handy feature of newer editors is built-in FTP. These editors can open files on a remote Web server as if they were on a local drive. A single click saves them back to the remote Web server. Another fea-ture you may enjoy is syntax highlighting. This causes PHP keywords to be colored in order to help you read the code faster.

Everyone has a favorite editor for PHP scripts. I use UltraEdit <http://www.ultraedit.com/>. I know many Windows users prefer Macromedia's Dreamweaver <http://www.macromedia.com/software/dreamweaver/> or HomeSite <http://www.macromedia.com/software/homesite/> to edit PHP scripts. The Macintosh users I know prefer BBedit <http://www.barebones.com/products/bbedit/bbedit.html>.

On a UNIX operating system, you may prefer emacs or vi, of course. You might also consider nEdit <http://nedit.org/>.A module for PHP is available in the contrib directory. The topic of which editor is best appears frequently on the PHP mailing list. Reading the archives can be amusing and informative <http://www.progressive-comp.com/Lists/?l=php3-general>.

Although I continue to use a text editor for building PHP applications, many people prefer an integrated development environment, otherwise known as an IDE. There are several IDEs designed specifically for PHP. PHPEdit <http://www.phpedit.net/> is one example. The Zend Studio <http://www.zend.com/store/products/end-studio.php> is another very popular choice.

Algorithms

Whenever we interact with a computer, we are instructing it to perform some action. When you drag an icon into the wastebasket on your desktop, you are asking the computer to remove the file from your hard disk. When you write an HTML file, you are instructing the computer in the proper way to display some information. There are usually many incremental steps to any process the computer performs. It may first clear the screen with the color you specified in the body tag. Then it may begin writing some text in a particular color and typeface. As you use a computer, you may not be entirely aware of each tiny step it takes, but you are giving it a list of ordered instructions that you expect it to follow.

Instructions for baking a cake are called a recipe. Instructions for making a movie are called a screenplay. Instructions for a computer are called a program. Each of these is written in its own language, a concrete realization of an abstract set of instructions. Borrowing from mathematics, computer science calls the abstract instructions an algorithm.

You may at this moment have in mind an algorithm that you'd like to implement. Perhaps you wish to display information in a Web browser that changes frequently. Imagine something simple, such as displaying today's date. You could edit a plain HTML file once a day. You could even write out a set of instructions to help remind you of each step. But you cannot perform the task with HTML alone. There's no tag that stands for the current date.

PHP is a language that allows you to express algorithms for creating HTML files. With PHP, you can write instructions for displaying the current date inside an HTML document. You write your instructions in a file called a script. The language of the script is PHP, a language that both you and the computer can understand.

1.6 What a PHP Script Looks Like

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 ques-tion 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. Finally, you can use tags that appear like ASP, using <% to start blocks of code. Appendix D explains how these alternatives work. In my own coding, I frequently use the simple <? and ?> method because I can be sure I can configure PHP to accept them. For code you share with others, it's best to use <?php for the opening tag, as I have in the examples.

Listing 1. 6 shows an ordinary HTML page with one remarkable difference: the PHP code between the <?php 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 (see Figure 1. 1).

Listing 1.6 Printing today's date

<html>
<head>
<title>Listing 1-6</ title>
</head>
<body>
Today's date: <?php print( Date(" l F d, Y")); ?>
</body>
</html>

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

home / programming / php / corephp / 1 To page 1To page 2To page 3To page 4current page
[previous]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint

Created: March 27 2003
Revised: Sept. 1, 2003

URL: http://webreference.com/programming/php/corephp/1