![]() Back to www.deitel.com |
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>
|
|
|