![]() 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.7: compare.php -->
|
|
5 <!-- String Comparison -->
|
|
6
|
|
7 <html xmlns = "http://www.w3.org/1999/xhtml">
|
|
8 <head>
|
|
9 <title>String Comparison</title>
|
|
10 </head>
|
|
11
|
|
12 <body>
|
|
13 <?php
|
|
14
|
|
15 // create array fruits
|
|
16 $fruits = array( "apple", "orange", "banana" );
|
|
17
|
|
18 // iterate through each array element
|
|
19 for ( $i = 0; $i < count( $fruits ); $i++ ) {
|
|
20
|
|
21 // call function strcmp to compare the array element
|
|
22 // to string "banana"
|
|
23 if ( strcmp( $fruits[ $i ], "banana" ) < 0 )
|
|
24 print( $fruits[ $i ]." is less than banana " );
|
|
25 elseif ( strcmp( $fruits[ $i ], "banana" ) > 0 )
|
|
26 print( $fruits[ $i ].
|
|
27 " is greater than banana " );
|
|
28 else
|
|
29 print( $fruits[ $i ]." is equal to banana " );
|
|
30
|
|
31 // use relational operators to compare each element
|
|
32 // to string "apple"
|
|
33 if ( $fruits[ $i ] < "apple" )
|
|
34 print( "and less than apple! <br />" );
|
|
35 elseif ( $fruits[ $i ] > "apple" )
|
|
36 print( "and greater than apple! <br />" );
|
|
37 elseif ( $fruits[ $i ] == "apple" )
|
|
38 print( "and equal to apple! <br />" );
|
|
39
|
|
40 }
|
|
41 ?>
|
|
42 </body>
|
|
43 </html>
|
|
|