Chapter 2. Language BasicsContents:
Lexical Structure This chapter provides a whirlwind tour of the core PHP language, covering such basic topics as data types, variables, operators, and flow control statements. PHP is strongly influenced by other programming languages, such as Perl and C, so if you've had experience with those languages, PHP should be easy to pick up. If PHP is one of your first programming languages, don't panic. We start with the basic units of a PHP program and build up your knowledge from there. 2.1. Lexical StructureThe lexical structure of a programming language is the set of basic rules that governs how you write programs in that language. It is the lowest-level syntax of the language and specifies such things as what variable names look like, what characters are used for comments, and how program statements are separated from each other. 2.1.1. Case SensitivityThe names of user-defined classes and functions, as well as built-in constructs and keywords such as echo, while, class, etc., are case-insensitive. Thus, these three lines are equivalent: echo("hello, world"); ECHO("hello, world"); EcHo("hello, world"); Variables, on the other hand, are case-sensitive. That is, $name, $NAME, and $NaME are three different variables. 2.1.2. Statements and SemicolonsA statement is a collection of PHP code that does something. It can be as simple as a variable assignment or as complicated as a loop with multiple exit points. Here is a small sample of PHP statements, including function calls, assignment, and an if test: echo "Hello, world"; myfunc(42, "O'Reilly"); $a = 1; $name = "Elphaba"; $b = $a / 25.0; if ($a == $b) { echo "Rhyme? And Reason?"; } PHP uses semicolons to separate simple statements. A compound statement that uses curly braces to mark a block of code, such as a conditional test or loop, does not need a semicolon after a closing brace. Unlike in other languages, in PHP the semicolon before the closing brace is not optional: if ($needed) { echo "We must have it!"; // semicolon required here } // no semicolon required here The semicolon is optional before a closing PHP tag: <?php if ($a == $b) { echo "Rhyme? And Reason?"; } echo "Hello, world" // no semicolon required before closing tag ?> It's good programming practice to include optional semicolons, as they make it easier to add code later. 2.1.3. Whitespace and Line BreaksIn general, whitespace doesn't matter in a PHP program. You can spread a statement across any number of lines, or lump a bunch of statements together on a single line. For example, this statement: raise_prices($inventory, $inflation, $cost_of_living, $greed); could just as well be written with more whitespace: raise_prices ( $inventory , $inflation , $cost_of_living , $greed ) ; or with less whitespace: raise_prices($inventory,$inflation,$cost_of_living,$greed); You can take advantage of this flexible formatting to make your code more readable (by lining up assignments, indenting, etc.). Some lazy programmers take advantage of this free-form formatting and create completely unreadable code—this isn't recommended. 2.1.4. CommentsComments give information to people who read your code, but they are ignored by PHP. Even if you think you're the only person who will ever read your code, it's a good idea to include comments in your code—in retrospect, code you wrote months ago can easily look as though a stranger wrote it. Good practice is to make your comments sparse enough not to get in the way of the code itself and plentiful enough that you can use the comments to tell what's happening. Don't comment obvious things, lest you bury the comments that describe tricky things. For example, this is worthless: $x = 17; // store 17 into the variable $x whereas this may well help whoever will maintain your code: // convert &#nnn; entities into characters $text = preg_replace('/&#([0-9])+);/e', "chr('\\1')", $text); PHP provides several ways to include comments within your code, all of which are borrowed from existing languages such as C, C++, and the Unix shell. In general, use C-style comments to comment out code, and C++-style comments to comment on code. 2.1.4.1. Shell-style commentsWhen PHP encounters a hash mark (#) within the code, everything from the hash mark to the end of the line or the end of the section of PHP code (whichever comes first) is considered a comment. This method of commenting is found in Unix shell scripting languages and is useful for annotating single lines of code or making short notes. Because the hash mark is visible on the page, shell-style comments are sometimes used to mark off blocks of code: ####################### ## Cookie functions ####################### Sometimes they're used before a line of code to identify what that code does, in which case they're usually indented to the same level as the code: if ($double_check) { # create an HTML form requesting that the user confirm the action echo confirmation_form( ); } Short comments on a single line of code are often put on the same line as the code: $value = $p * exp($r * $t); # calculate compounded interest When you're tightly mixing HTML and PHP code, it can be useful to have the closing PHP tag terminate the comment: <?php $d = 4 # Set $d to 4. ?> Then another <?php echo $d ?> Then another 4 2.1.4.2. C++ commentsWhen PHP encounters two slash characters (//) within the code, everything from the slashes to the end of the line or the end of the section of code, whichever comes first, is considered a comment. This method of commenting is derived from C++. The result is the same as the shell comment style. Here are the shell-style comment examples, rewritten to use C++ comments: //////////////////////// // Cookie functions //////////////////////// if ($double_check) { // create an HTML form requesting that the user confirm the action echo confirmation_form( ); } $value = $p * exp($r * $t); // calculate compounded interest <?php $d = 4 // Set $d to 4. ?> Then another <?php echo $d ?> Then another 4 2.1.4.3. C commentsWhile shell- and C++-style comments are useful for annotating code or making short notes, longer comments require a different style. As such, PHP supports block comments, whose syntax comes from the C programming language. When PHP encounters a slash followed by an asterisk (/*), everything after that until it encounters an asterisk followed by a slash (*/) is considered a comment. This kind of comment, unlike those shown earlier, can span multiple lines. Here's an example of a C-style multiline comment: /* In this section, we take a bunch of variables and assign numbers to them. There is no real reason to do this, we're just having fun. */ $a = 1; $b = 2; $c = 3; $d = 4; Because C-style comments have specific start and end markers, you can tightly integrate them with code. This tends to make your code harder to read, though, so it is frowned upon: /* These comments can be mixed with code too, see? */ $e = 5; /* This works just fine. */ C-style comments, unlike the other types, continue past end markers. For example: <?php $l = 12; $m = 13; /* A comment begins here ?> <p>Some stuff you want to be HTML.</p> <?= $n = 14; ?> */ echo("l=$l m=$m n=$n\n"); ?> <p>Now <b>this</b> is regular HTML...</p> l=12 m=13 n= <p>Now <b>this</b> is regular HTML...</p> You can indent, or not indent, comments as you like: /* There are no special indenting or spacing rules that have to be followed, either. */ C-style comments can be useful for disabling sections of code. In the following example, we've disabled the second and third statements by including them in a block comment. To enable the code, all we have to do is remove the comment markers: $f = 6; /* $g = 7; # This is a different style of comment $h = 8; */ However, you have to be careful not to attempt to nest block comments: $i = 9; /* $j = 10; /* This is a comment */ $k = 11; Here is some comment text. */ In this case, PHP tries (and fails) to execute the (non-)statement Here is some comment text and returns an error. 2.1.5. LiteralsA literal is a data value that appears directly in a program. The following are all literals in PHP: 2001 0xFE 1.4142 "Hello World" 'Hi' true null 2.1.6. IdentifiersAn identifier is simply a name. In PHP, identifiers are used to name variables, functions, constants, and classes. The first character of an identifier must be either an ASCII letter (uppercase or lowercase), the underscore character (_), or any of the characters between ASCII 0x7F and ASCII 0xFF. After the initial character, these characters and the digits 0-9 are valid. 2.1.6.1. Variable namesVariable names always begin with a dollar sign ($) and are case-sensitive. Here are some valid variable names: $bill $head_count $MaximumForce $I_HEART_PHP $_underscore $_int Here are some illegal variable names: $not valid $| $3wa These variables are all different: $hot_stuff $Hot_stuff $hot_Stuff $HOT_STUFF 2.1.6.2. Function namesFunction names are not case-sensitive (functions are discussed in more detail in Chapter 3). Here are some valid function names: tally list_all_users deleteTclFiles LOWERCASE_IS_FOR_WIMPS _hide These function names refer to the same function: howdy HoWdY HOWDY HOWdy howdy 2.1.6.3. Class namesClass names follow the standard rules for PHP identifiers and are not case-sensitive. Here are some valid class names: Person account The class name stdClass is reserved. 2.1.6.4. ConstantsA constant is an identifier for a simple value; only scalar values—boolean, integer, double, and string—can be constants. Once set, the value of a constant cannot change. Constants are referred to by their identifiers and are set using the define( ) function: define('PUBLISHER', "O'Reilly & Associates"); echo PUBLISHER; 2.1.7. KeywordsA keyword is a word reserved by the language for its core functionality—you cannot give a variable, function, class, or constant the same name as a keyword. Table 2-1 lists the keywords in PHP, which are case-insensitive. Table 2-1. PHP core language keywords
In addition, you cannot use an identifier that is the same as a built-in PHP function. For a complete list of these, see Appendix A. Copyright © 2003 O'Reilly & Associates. All rights reserved. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|