The Basics of PHP Scripts


Let's jump straight in with a PHP script. To begin, open your favorite text editor. Like HTML documents, PHP files are made up of plain text. You can create them with any text editor, and most popular HTML editors provide at least some support for PHP.

Did you Know?

A good website for finding PHP-friendly editors is at www.php-editors.com.


Type in the example in Listing 4.1 and save the file, calling it something like first.php.

Listing 4.1. A Simple PHP Script

 1: <?php 2:        echo "Hello Web!"; 3: ?> 

If you are not working directly on the machine that will be serving your PHP script, you will probably need to use an FTP client such as WS_FTP or SmartFTP for Windows or Fetch for Mac OS to upload your saved document to the server.

When the document is in place on the server, you should be able to access it using your browser. If all has gone well, you should see the script's output. Figure 4.5 shows the output from the first.php script.

Figure 4.5. Success: the output from first.php.


Beginning and Ending a Block of PHP Statements

When writing PHP, you need to inform the PHP engine that you want it to execute your commands. If you don't do this, the code you write will be mistaken for HTML and will be output to the browser. You can designate your code as PHP with special tags that mark the beginning and end of PHP code blocks. Table 4.1 shows four such PHP delimiter tags.

Table 4.1. PHP Start and End Tags Tag

Tag Style

Start Tag

End Tag

Standard tags

<?php

?>

Short tags

<?

?>

ASP tags

<%

%>

Script tags

<script language="php">

</script>


Of the tags in Table 4.1, only the standard and script tags are guaranteed to work on any configuration. The short and ASP-style tags must be explicitly enabled in your php.ini.

To activate recognition for short tags, you must make sure that the short_open_tag switch is set to On in php.ini:

short_open_tag =  On;


To activate recognition for the ASP-style tags, you must enable the asp_tags setting in php.ini:

asp_tags = On;


By the Way

To ensure portable, reusable code, it's best to use the standard tags instead of short tags or ASP-style tags for the simple reason that server configurations are uniqueuse the standard style because you know you can count on it as part of any configuration.


After you have edited php.ini and restarted Apache, you should be able to use any of the four styles in your scripts. This is largely a matter of preference, although if you intend to include XML in your script, you should disable the short tags (<?? >) and work with the standard tags (<?php? >).

Watch Out!

The character sequence <? tells an XML parser to expect a processing instruction and is therefore frequently included in XML documents. If you include XML in your script and have short tags enabled, the PHP engine is likely to confuse XML processing instructions and PHP start tags. Disable short tags if you intend to incorporate XML in your document.


Let's run through some of the ways in which you can legally write the code in Listing 4.1. You can use any of the four PHP start and end tags that you have seen:

<? echo "Hello Web!"; ?> <?php echo "Hello Web!"; ?> <% echo "Hello Web!"; %> <script language="php"> echo "Hello Web!"; </script>


You can also put single lines of code in PHP on the same line as the PHP start and end tags:

<? echo "Hello Web!"; ?>


Now that you know how to define a block of PHP code, let's take a closer look at the code in Listing 4.1 itself.

The echo Statement and print() Function

Simply, the echo statement is used to output data. In most cases, anything output by echo ends up viewable in the browser. Alternatively, you could have used the print() function in place of the echo statement. Using echo or print() is a matter of taste; when you look at other people's scripts, you might see either used.

Whereas echo is a statement, print() is a function. A function is a command that performs an action, usually modified in some way by data provided for it. Data sent to a function is almost always placed in parentheses after the function name. In this case, you could have sent the print() function a collection of characters, or a string. Strings must always be enclosed in quotation marks, either single or double, as in this example:

<? print("Hello Web!"); ?>


The only line of code in Listing 4.1 ended with a semicolon. The semicolon informs the PHP engine that you have completed a statement.

A statement represents an instruction to the PHP engine. Broadly, it is to PHP what a sentence is to written or spoken English. A sentence should usually end with a period; a statement should usually end with a semicolon. Exceptions to this rule include statements that enclose other statements and statements that end a block of code. In most cases, however, failure to end a statement with a semicolon will confuse the PHP engine and result in an error.

Combining HTML and PHP

The script in Listing 4.1 is pure PHP. You can incorporate this into an HTML document by simply adding HTML outside the PHP start and end tags, as shown in Listing 4.2.

Listing 4.2. A PHP Script Incorporated into HTML

  1: <html>  2: <head>  3: <title>A PHP script including HTML</title>  4: </head>  5: <body>  6: <b>  7: <?php  8:     echo "hello world";  9: ?> 10: </b> 11: </body> 12: </html>

As you can see, incorporating PHP code into a predominantly HTML document is simply a matter of typing in the code. The PHP engine ignores everything outside the PHP open and close tags. If you were to save the contents of Listing 4.2 as helloworld.php, place it in your document root, and then view it with a browser, as shown in Figure 4.6, you would see the string hello world in bold. If you were to view the document source, as shown in Figure 4.7, the listing would look exactly like a normal HTML document.

You can include as many blocks of PHP code as you need in a single document, interspersing them with HTML as required. Although you can have multiple blocks of code in a single document, they combine to form a single script. Any variables defined in the first block will usually be available to subsequent blocks.

Figure 4.6. The output of helloworld.php as viewed in a browser.


Figure 4.7. The output of helloworld.php as HTML source code.


Adding Comments to PHP Code

Code that seems clear at the time of writing can seem like a hopeless tangle when you try to amend it six months later. Adding comments to your code as you write can save you time later on and make it easier for other programmers to work with your code.

A comment is text in a script that is ignored by the PHP engine. Comments can be used to make code more readable or to annotate a script.

Single-line comments begin with two forward slashes (//)the preferred styleor a single hash or pound sign (#). The PHP engine ignores all text between these marks and either the end of the line or the PHP close tag:

// this is a comment #  this is another comment


Multiline comments begin with a forward slash followed by an asterisk (/*) and end with an asterisk followed by a forward slash (*/):

/* this is a comment none of this will be parsed by the PHP engine */





Sams Teach Yourself PHP, MySQL And Apache All in One
Sams Teach Yourself PHP, MySQL and Apache All in One (3rd Edition)
ISBN: 0672328739
EAN: 2147483647
Year: 2004
Pages: 327

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net