Team LiB
Previous Section Next Section

Basic PHP Syntax

Now that you are aware of how PHP scripts are executed, let's discuss how to actually write your first PHP script. All PHP scripts are written in what are called code blocks. These blocks can be embedded into HTML, if desired, and are generally defined by <?php at the start and ?> at the end. Everything outside of these block identifiers will be ignored by the PHP interpreter and instead passed directly back to the Web server to be displayed to the client. Listing 1.1 is an example of a simple "Hello World" PHP script to get you started:

Listing 1.1. A Simple "Hello World" Script

       echo "Hello, world!"; 


As you would expect, the first three lines of this simple PHP script are ignored and passed directly to the output of the script. The fourth line, however, is executed by PHP, and the string "Hello world!" is printed to the browser, followed by the rest of the ignored HTML text. You have learned your first PHP statementthe echo statement. This statement is the basic method in PHP to display content back to the client, and you'll be using it extensively throughout the book. Also note that, as with other C-style languages, each statement ends with a semicolon.


Although <?php and ?> are generally used, the following are also valid code-block separators:

<? ... ?>

Shorthand version of <?php and ?>

<% ... %>

ASP style





HTML editor compatible syntax

Note that some of these code block separators function only when the associated php.ini configuration directive is enabled. Unless there is a specific reason not to, using the default <?php and ?> tags is strongly recommended.

Although it's PHP, the preceding script does nothing that couldn't already be done with standard HTML. To do anything worthwhile, you'll need to learn how to use PHP variables.

In PHP, variables always start with the $ symbol followed by any combination of characters, provided that the first character following the $ symbol is a valid letter or underscore. Valid letters include uppercase and lowercase az as well as characters whose ASCII-value is between 127 and 255 (non-U.S. letters). In PHP, variables can be defined either by assigning them a value or by using the var statement. Listing 1.2 shows a few examples:

Listing 1.2. PHP Variable Examples

    $myvar = "foo";             /* Assigns the string 'foo' */

    badvar = "test";            /* Invalid, no $ symbol */
    $another(test)var = "bad";  /* Invalid, can't use () */
    $php5 = "is cool";          /* Correct Syntax */
    $5php = "is wrong";         /* Invalid, starts with number */


In PHP, everything between /* and */ is considered a comment used to describe the script and is ignored by the interpreter. For single-line comments, // or # may be used to "comment out" the remainder of a single line:

    $var = "foo";        // This is all ignored
    $var = "bar";        # so is this

Although it is not necessary to destroy variables to free up resources (PHP's garbage collection routines will do so for you when a script terminates), it is sometimes desirable to force the destruction of a variable. To do this, PHP provides the unset() function. This function can be used on any valid PHP variable, including array elements (see Chapter 2, "Arrays," for an in-depth discussion of arrays). Listing 1.3 demonstrates its use to destroy an existing PHP variable:

Listing 1.3. Using the unset() Function
    $myvar = "This is a string";
    unset($myvar);        // Destroy the variable

As far as variable types are concerned, PHP is classified as a loosely typed language. This means that a variable does not have to be defined as a string, an integer, a floating point, and so on. Instead, the variable is assigned a value and PHP will treat it accordingly, depending on the circumstance under which it was used. In PHP, there are three basic variables types (integer, string, and floating point) and two complex types (objects and arrays). This chapter will deal only with the basic types; see Chapter 2, "Arrays," and Chapter 14, "Object-Oriented Programming in PHP," for details regarding the remaining two complex variables.

    Team LiB
    Previous Section Next Section