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)
Converting Between PHP Data Types
Conversion between different data types may be necessary when performing arithmetic operations with variables. In PHP, data-type conversion can be performed by passing the data type as an argument to function settype. Figure 26.3 demonstrates type conversion of the PHP data types introduced in Fig. 26.2.
Fig. 26.33 Type conversion. (Part 1 of 2.)
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.3: data.php -->
5   <!-- Demonstration of PHP data types -->
6   ;
7   "http://www.w3.org/1999/xhtml">
8      <head>
9         <title>PHP data types</title>
10      </head>
11   
12      <body>
13   
14         <?php
15   
16            // declare a string, double and integer
17            $testString = "3.5 seconds";;
18            $testDouble = 79.2;
19            $testInteger = 12;
20         ?>
21
22         <!-- print each variable's value -->
23         <?php print( $testString ); ?> is a string.<br />
24         <?php print( $testDouble ); ?> is a double.<br />
25         ;<?php print( $testInteger ); ?> is an integer.<br />
26   
27          <br />
28         Now, converting to other types:<br />
29         <?php
30   
31             // call function settype to convert variable
32             // testString to different data types
33            print( "$testString" );
34            settype( $testString, "double" );
35            print( " as a double is $testString <br />" );
36            print( "$testString" );
37            settype( $testString, "integer" );
38            print( " as an integer is $testString <br />" );
39            settype( $testString, "string" );
40            print( "Converting back to a string results in
41               $testString <br /><br />" );
42   
43            $data = "98.6 degrees";
44   
45            // use type casting to cast variables to a
46            // different type
47            print( "Now using type casting instead: <br />
48               As a string - " . (string) $data .
49               "<br />As a double - " . (double) $data .
50               "<br />As an integer - " . (integer) $data );
51         ?>
52       </body>
53   </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