PHP-the Language

As we mentioned, PHP looks more like a traditional programming language such as C, Pascal, or Visual Basic. It doesn't use the familiar tag structure you may be accustomed to, but it's not that difficult to grasp. PHP is quite powerful and has more in its bag of tricks than we can cover in this chapter. But, like the other language chapters in this book, we'll explore a few of the features that will get you started.

Language Overview

Before we get into a few of the more common PHP commands you use in your web development, you need to be aware of the way PHP handles some of its constructs.

Case Sensitivity

PHP is a forgiving language. It does its best to never generate an error when running on your web server. And, for the most part, PHP isn't case sensitive in its language constructs. However, there is one point that can drive you batty if you're unaware of it. PHP is picky about the case of variables. That is, PHP is case sensitive with variable names. The variable City is not the same as the variable city. UserID is not that same as userid. Keep that in mind when you start developing in PHP and save yourself some time. (Function names and command names can be mixed case, if you'd like-just don't mix-case your variables.)

Variables Start with a Dollar Sign ($)

All variables in PHP are to be prefaced with a dollar sign ($). Using the example in the previous section, our PHP code should have $City or $UserID as the variable names whenever we use them. Without the $, PHP won't know that you're trying to use a variable. Also, the first character after the $ cannot be a number. The variable name $76Trombones would generate an error. If you need to include the dollar sign at the beginning of a text string, escape it using the backslash (\). For example, to print a dollar sign, include something similar to the following:

<?php print "\$sign" ?>

All Statements End with a Semicolon (;)

All statements in PHP must end with a semicolon (;). However, blocks and loops do not. The semicolon tells PHP that the current line has ended and that it should process the command.

Curly Braces Denote Blocks of Code ({ })

To include and execute blocks of code within IF statements or loops, you follow the C and JavaScript conventions of wrapping the block with curly braces ({ }). For example, an IF statement can take the following form:

IF TRUE { print ("Hello world. <br>");  print ("Hello again, from the second line."); }

Common Escape Sequences

We demonstrated a sample escape sequence above with the dollar sign. Escape sequences are those character combinations that escape from the typical processor behavior and allow you to deviate from the standard processing of the code. You'll need to use escape sequences in double-quoted strings (more on that in the next section). Here's a list of common escape sequences you might need in order to display certain characters within PHP:


Causes PHP to print a dollar sign


Causes PHP to print a double quote


Causes PHP to print a backslash

Single-Quoted Versus Double-Quoted Strings

Single-quoted strings are treated differently than double-quoted strings in PHP. Single-quoted strings treat almost everything between them as literal characters, even if you include variable names. For example, the code

<?php print 'The name you entered was $cFirstName' ?>

displays the text shown in Figure 16.2. As you can see, PHP didn't interpret the variable as you might have expected. For PHP to interpret the variable correctly, use double quotes around the text string, as in the following:

Figure 16.2: Single quotes tell PHP to take everything literally.

<?php print "The name you entered was $cFirstName" ?> 

PHP Is Forgiving

As we mentioned, even though PHP is more like a true programming language instead of a tag-based language, it is forgiving when you make an error in your code. PHP typically tries to continue to run when it encounters an error so that your page doesn't bomb out. For example, Listing 16.2 shows that we're trying to print the variable cFirstName, but we haven't defined it. You might expect an error, but depending on how you have the php.ini value set for error_reporting (E_ALL will show you notices), you might see something like Figure 16.3. PHP warned us that it couldn't find the variable, but continued processing the code. Figure 16.4 shows the same code, ran with error_report set to E_ALL & ~E_NOTICES.

Listing 16.2: PHP_NOTICE

start example
<html>  <head>  <title> Variable Display</title>  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">  </head> <body> <?php print "The name you entered was $cFirstName" ?>  <br><br>  <?php print "You should see a notice from PHP above." ?> </body>  </html> 
end example

click to expand
Figure 16.3: PHP continues code processing when it encounters an undefined variable, but displays a notice.

click to expand
Figure 16.4: This is the same code with the php.ini error_reporting option set not to show notices.

PHP's Comparison Operators

PHP's comparison operators are used to compare variables within expressions. Each language generally has a couple of operators that are different from other languages. PHP is no exception, so here's the list:


Equal To (not to be confused with the assignment operator '=')


Not Equal To


Greater Than


Less Than


Greater Than or Equal To


Less Than or Equal To


Identical (only returns true if the variables are the same datatype and the same value)

Language Specifics

Now that you have an overview of the PHP language and understand some of its quirks, let's explore the commands and features that you will use in web development. Naturally, we're not going to cover all, but this discussion should give you a good idea where to start in PHP web development.

Writing to the Page-print and echo

Output-you've certainly got to have it in web development. Otherwise, there's not much sense in having a website, right? PHP has two primary commands for printing to a web page-print and echo.

These two commands perform the same function, but operate a bit differently: echo can take multiple arguments, and print can return a value to the calling routine if it's successful in actually displaying information to the screen. You won't need these differences in most instances, so it's best to settle on one or the other-echo, for instance-for all your printing needs. Here's an example of each of these tags in action that yields the output in Figure 16.5.

<?php  $cCityName = "Louisville";  print "PRINT: The city name is $cCityName" ?>  <br><br>  <?php echo "ECHO: The city name is $cCityName" ?>

Figure 16.5: Both echo and print do basically the same thing.

Passing Variables between Pages with Get

PHP, like other web-development languages, has its own method for retrieving values passed between pages. In ASP, in order to grab a value from an HTTP Get operation, you use request.querystring("varname"). In PHP, you use the command $http_get_vars to extract variables from the query string. The following code creates the form shown in Figure 16.6.

<html>  <head>  <title>Passing Variables</title>  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">  </head> <body>  <form action="Name_get_Action.php" method="get" name="GetName"> What is your name? <input name="Name" type="text" maxlength="50">  <br> <input name="Submit" type="submit" value="Submit"> </form> </body>  </html> 

click to expand
Figure 16.6: This simple form uses the Get method.

Notice that the form is submitted with a method of Get. The following code extracts the variable from the query string that the Get method creates. You can see the value passed in the query string by checking the URL in Figure 16.7. The following code retrieves the variable:

<html>  <head>  <title>Untitled Document</title>  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">  </head> <body> <h2>Using Get</h2>  <br> You entered: <?php print $HTTP_GET_VARS['Name']; ?> </body>  </html> 

click to expand
Figure 16.7: The HTTP Get method passes form variables in the URL query string.

Unlike most functions and commands in other web-development languages, $http_get_vars uses brackets instead of parentheses, as you can see here. That may take you a few syntax errors to get used to typing.

Passing Variables between Pages with Post

The preferred way to pass form variables to other pages is through the use of HTTP Post. This method passes the variables in the HTTP header so that the user doesn't see a query string. Plus, it's cleaner, doesn't have a size restriction, and is a bit more secure than Get. To retrieve variables passed using Post, you use the $http_post_vars command. For example, the following code is almost identical to the form code earlier, but notice that we're using Post as the method:

<html> <head>  <title>Untitled Document</title>  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">  </head> <body>  <form action="Name_Action.php" method="post" name="GetName"> What is your name? <input name="Name" type="text" maxlength="50">  <br> <input name="Submit" type="submit" value="Submit"> </form> </body>  </html>

We retrieve the variable sent via the Post method using the following code that creates the same output, shown in Figure 16.8, as the Get. However, notice that there is no query string in the URL.

<html>  <head>  <title>Using Post</title>  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">  </head> <body>  <h2>Using Post</h2>  <br> You entered: <?php print $HTTP_POST_VARS['Name']; ?> </body>  </html>

click to expand
Figure 16.8: Post doesn't send the query string, so we use $http_post_vars to retrieve the variable.

There are many, many more functions and commands than we can cover here, so to learn PHP in depth, grab a book such as Mastering PHP 4.1 from Sybex at your local book store or visit www.php.netfor more information.

Mastering Dreamweaver MX Databases
Mastering Dreamweaver MX Databases
ISBN: 078214148X
EAN: 2147483647
Year: 2002
Pages: 214 © 2008-2017.
If you may any questions please contact us: