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)
Lines 17-19 of Fig. 26.3 assign a string to variable $testString, a floating-point number to variable $testDouble and an integer to variable $testInteger. Variables are converted to the data type of the value they are assigned. For example, variable $testString becomes a string when assigned the value "3.5 seconds". Lines 23-25 print the value of each variable. Note that enclosing a variable name in double quotes in a print statement is optional. Lines 34-39 call function settype to modify the data type of each variable. Function settype takes two arguments-the variable whose data type is to be changed and the variable's new data type. Calling function settype can result in loss of data. For example, doubles are truncated when they are converted to integers. When converting between a string and a number, PHP uses the value of the number that appears at the beginning of the string. If no number appears at the beginning of the string, the string evaluates to 0. In line 34, the string "3.5 seconds" is converted to a double, resulting in the value 3.5 being stored in variable $testString. In line 37, double 3.55 is converted to integer 3. When we convert this variable to a string (line 39), the variable's value becomes "3".
Another option for conversion between types is casting (or type castingsettype, casting does not change a variable's content. Rather, type casting creates a temporary copy of a variable's value in memory. Lines 47-50 cast variable $dataa's value to a string, a double and an integer. Casting is necessary when a specific data type is required for an arithmetic operation.
The concatenation operator (.) concatenates strings. This combines multiple strings in the same print statement (lines 47-50). A print statement may be split over multiple lines; everything that is enclosed in the parentheses, terminated by a semicolon, is sent to the client.
 
Page 1 | 2 | 3 | 4 | 5 | 6 | 7
 

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