标签:
>
Greater than<
Less than<=
Less than or equal to>=
Greater than or equal to==
Equal to!=
Not equal to
Now let‘s see how we can use comparisons to ask yes or no questions.
Say we want to write a program that asks whether your name is longer than 7 letters. If the answer is yes, we can respond with "You have a long name!" We can do this with an if
statement:
<?php $age = 17; if( $age > 16 ) { echo "You can drive!"; } ?>
An if statement is made up of the if
keyword, a condition like we‘ve seen before, and a pair of curly braces { }
. If the answer to the condition is yes, the code inside the curly braces will run.
<?php $name = "Edgar"; if ($name == "Simon") { print "I know you!"; } else { print "Who are you?"; } ?>
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title></title> 5 </head> 6 <body> 7 <?php 8 switch (2) { 9 case 0: 10 echo ‘The value is 0‘; 11 break; 12 case 1: 13 echo ‘The value is 1‘; 14 break; 15 case 2: 16 echo ‘The value is 2‘; 17 break; 18 default: 19 echo "The value isn‘t 0, 1 or 2"; 20 } 21 ?> 22 </body> 23 </html>
You have two ways of creating a switch. First, there‘s the way we have made all the past exercises:
switch ($i) { }
But we can also make it this way:
switch ($i): endswitch;
This is called alternative syntax. It exists to provide syntactic sugar
标签:
原文地址:http://www.cnblogs.com/elewei/p/4850163.html