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)
Arithmetic Operators
PHP provides a variety of arithmetic operators, which we demonstrate in Fig. 26.4. Line 14 declares variable $a and assigns it the value 5. Line 18 calls function define to create a named constant. A constant is a value that cannot be modified once it is declared. Function define takes two arguments-the name and value of the constant. An optional third argument accepts a boolean value that specifies whether the constant is case insensitive-constants are case sensitive by default.

Common Programming Error 26.4
Assigning a value to a constant after a constant is declared is a syntax error.
Fig. 26.4 Using PHP's arithmetic operators. (Part 1 of 3.)
1   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3   
4   <!-- Fig. 26.4: operators.php -->
5   <!-- Demonstration of operators -->
6   
7   <html xmlns = "http://www.w3.org/1999/xhtml">
8       <head>
9         <title>Using arithmetic operators</title>
10       </head>
11   
12       <body>
13         <?php
14            $a = 5;
15             print( "The value of variable a is $a <br />" );
16
17            // define constant VALUE
18            define( "VALUE", 5 );
19   
20            // add constant VALUE to variable $a
21            $a = $a + VALUE;
22             print( "Variable a after adding constant VALUE
23               is $a <br />" );
24
25            // multiply variable $a by 2
26            $a *= 2;
27             print( "Multiplying variable a by 2 yields $a <br />" );
28
29            // test if variable $a is less than 50
30             if ( $a < 50 )
31                print( "Variable a is less than 50 <br />" );
32   
33             // add 40 to variable $a
34             $a += 40;
35             print( "Variable a after adding 40 is $a <br />" );
36   
37            // test if variable $a is 50 or less
38             if ( $a < 51 )
39                print( "Variable a is still 50 or less<br />" );
40   
41            // test if variable $a is between 50 and 100, inclusive
42            elseif ( $a < 101 )
43               print( "Variable a is now between 50 and 100,
44                   inclusive<br />" );
45            else
46                print( "Variable a is now greater than 100
47                  <br />" );
48   
49             // print an uninitialized variable
50             print( "Using a variable before initializing:
51               $nothing <br />" );
52   
53            // add constant VALUE to an uninitialized variable
54            $test = $num + VALUE;
55             print( "An uninitialized variable plus constant
56               VALUE yields $test <br />" );
57   
58            // add a string to an integer
59            $str = "3 dollars";
60            $a += $str;
61             print( "Adding a string to variable a yields $a
62               <br />" );
63         ?>
64       </body>
65   </html>

  


 
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