Deitel & Associates, Inc. Logo

Back to
digg.png delicious.png blinkit.png furl.png
Internet & World Wide Web How to Program, 3/e
Internet & World Wide Web How to Program, 3/e

© 2004
pages: 1420

Amazon logo

This tutorial presents four PHP programs that introduce PHP programming concepts, including comments, variables, operators, keywords, type conversions, scripting delimiters, interpolation, string concatenation, control statements and arrays. The techniques you learn hear are used in our subsequent tutorials:
[Note: This tutorial is an excerpt (Section 26.2) of Chapter 26, PHP, from our textbook Internet & World Wide Web How to Program, 3/e. This tutorial may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., INTERNET & WORLD WIDE WEB HOW TO PROGRAM, 3/E, 2004, pp.900-909. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]
26.2 Introduction to PHP Programming (Continued)
In PHP, code is inserted between the scripting delimiters <?php and ?>. PHP code can be placed anywhere in XHTML markup, as long as the code is enclosed in these scripting delimiters. Line 8 declares variable $name and assigns to it the string "LunaTic". All variables are preceded by a $ and are created the first time they are encountered by the PHP interpreter. PHP statements are terminated with a semicolon (;).
Common Programming Error 26.1
Failing to precede a variable name with a $ is a syntax error.
Common Programming Error 26.2
Variable names in PHP are case sensitive. Failure to use the proper mixture of cases is a syntax error.
Common Programming Error 26.3
Forgetting to terminate a statement with a semicolon (;) is a syntax error.
Line 8 contains a one-line comment, which begins with two forward slashes (//). Text to the right of the slashes is ignored by the interpreter. One-line comments can also begin with the pound sign (#). Multiline comments begin with delimiter /* and end with delimiter */.
Line 21 outputs the value of variable $name by calling function print. The actual value of $name is printed, instead of "$name". When a variable is encountered inside a double-quoted ("") string, PHP interpolates the variable. In other words, PHP inserts the variable's value where the variable name appears in the string. Thus, variable $name is replaced by LunaTic for printing purposes. PHP variables are "multitype," meaning that they can contain different types of data (e.g., integers, doubles or strings) at different times. Figure 26.2 introduces these data types.
Fig. 26.2 PHP data types.
Data type
int, integer
Whole numbers (i.e., numbers without a decimal point).
float, double
Real numbers (i.e., numbers containing a decimal point).
Text enclosed in either single ('') or double ("") quotes.
bool, Boolean
True or false.
Group of elements of the same type.
Group of associated data and methods.
An external data source.
No value.
Good Programming Practice 26.1
Whitespace enhances the readability of PHP code. It also simplifies programming and debugging.
Page 1 | 2 | 3 | 4 | 5 | 6 | 7
Other PHP Tutorials
PHP Tutorial 1: Introduction to PHP
PHP Tutorial 2: Creating Simple PHP Programs (You are here)
PHP Tutorial 3: String Processing and Regular Expressions
PHP Tutorial 4: Form Processing and Business Logic