码迷,mamicode.com
首页 > 其他好文 > 详细

Conditionals and Control Flow

时间:2015-09-30 21:00:02      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

Part 1: if and else Statements

1. Comparisons operators

  • > Greater than
  • < Less than
  • <= Less than or equal to
  • >= Greater than or equal to
  • == Equal to
  • != Not equal to

 

2. if Statements

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.

 

3. Adding an Else

<?php
  $name = "Edgar";

  if ($name == "Simon") {
    print "I know you!";
  }
  else {
    print "Who are you?";
  }
?>

 

 

Part 2: Control Flow: Switch

 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

 

Conditionals and Control Flow

标签:

原文地址:http://www.cnblogs.com/elewei/p/4850163.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!