Deitel & Associates, Inc. Logo

Back to www.deitel.com
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

ISBN:
0-13-145091-3
© 2004
pages: 1420

Order
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)
Line 21 adds constant VALUE to variable $a, which is a typical use of arithmetic operators. Line 26 uses the assignment operator *= to yield an expression equivalent to $a = $a * 2 (thus assigning $a the value 20). These assignment operators (i.e., +=, -=, *= and /=) are syntactical shortcuts. Line 34 adds 40 to the value of variable $a.
In PHP, uninitialized variables have the value undef, which evaluates to different values, depending on its context. For example, when undef is used in a numeric context (e.g., $num in line 54), it evaluates to 0. In contrast, when undef is interpreted in a string context (e.g., $nothing in line 51), it evaluates to an empty string ("").

Error-Prevention Tip 26.1
Always initialize variables before using them. Doing so helps avoid subtle errors.
Strings are converted to integers when they are used in arithmetic operations (lines 59-60). In line 60, the string value "3 dollars" is converted to the integer 3 before being added to integer variable $a.
Error-Prevention Tip 26.2
Function print can be used to display the value of a variable at a particular point during a program's execution. This is often helpful in debugging a script.
Common Programming Error 26.5
Using an uninitialized variable might result in an incorrect numerical calculation. For example, multiplying a number by an uninitialized variable results in 0.
PHP keywords (such as if...elseif...else) are reserved for implementing language features. Figure 26.5 contains a list of frequently used PHP keywords.
Initializing and Manipulating Arrays
PHP provides the capability to store data in arrays. Arrays are divided into elements that behave as individual variables. Array names, like other variables, begin with the $ symbol. Script arrays.php (Fig. 26.6) demonstrates initializing and manipulating arrays.
 
Fig. 26.5 PHP keywords.
PHP keywords
and
break
case
class
continue
default
do
else
elseif
extends
false
for
foreach
function
global
if

include
list
new
not
or
require
return
static
switch
this
true
var
virtual
xor
while
Individual array elements are accessed by following the array-variable name with an index enclosed in braces ([]). If a value is assigned to an array that does not exist, then the array is created (line 18). Likewise, assigning a value to an element where the index is omitted appends a new element to the end of the array (line 21). The for loop (lines 24-25) prints each element's value. Function count returns the total number of elements in the array. Since array indices start at 0, the index of the last element is one less than the total number of elements. In this example, the for loop terminates once the counter ($i) is equal to the number of elements in the array.
Line 31 demonstrates a second method of initializing arrays. Function array returns an array that contains the arguments passed to it. The first item in the list is stored as the first array element, the second item is stored as the second array element and so on. Lines 32-33 use another for loop to print out each array element's value.
 
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