Home  >  Article  >  Backend Development  >  PHP Basic Case 3: Determining Student Constellations

PHP Basic Case 3: Determining Student Constellations

善始善终
善始善终Original
2020-11-13 10:55:574176browse

1. Demand Analysis

The zodiac sign is matched based on the birth month and day (11th to 14th digit); for example, when the birth date is between March 21st and April 19th, it is Aries, and other Click here to continue writing the horoscope.

2. Design ideas

1. The division of constellations is the interval between two dates. When the date is less than the 10th of the student's birth date, how to prevent comparison errors?

2. How to judge the constellation?

3. Knowledge Reserve

1. In PHP, the following conditional statements are provided:

·                                                                                                                                                                                                                                                             but can execute code when the condition is true. ·             : -Ternary operator

                                                             if...else statement - executes a piece of code when the condition is true, and executes another piece of code when the condition is not true.                                    ....else statement - executes a code block when one of several conditions is true

·    switch statement - executes a code block when one of several conditions is true

2, if statement

is used to execute code only when the specified condition is true.

Grammar

if (条件)
 {
 条件成立时要执行的代码;
 }

3, if...else statement

Execute a block of code when the condition is true, and execute it when the condition is not true Another piece of code.

Grammar

if (条件)
 {
 条件成立时执行的代码;
 }
 else
 {
 条件不成立时执行的代码;
 }

4. if...else if...else statement

Between several conditions Execute a block of code when established.

.

Syntax

if (条件)
 {
 if 条件成立时执行的代码;
 }
 else if (条件)
 {
 elseif 条件成立时执行的代码;
 }
 else
 {
 条件不成立时执行的代码;
 }

5. Switch statement

I hope to selectively execute one of several code blocks.

Grammar

switch (n) { 
case label1: 如果 n=label1,此处代码将执行; break; 
case label2: 如果 n=label2,此处代码将执行; break; 
default: 如果 n 既不等于 label1 也不等于 label2,此处代码将执行; 
}

4. Code implementation1. Define variables to save student information

$name = '王六';//保存学生的姓名
$birth = '2003-08-07'; //保存学生的出生日期

2. Segmentation String, get the year, month, and day of the student's birth

$temp = explode('-',$birth);  
$stu_by = $temp[0];
$stu_bm = $temp[1];
$stu_bd = $temp[2];

3. Get the year, month, and date of the current time

$cur_y = date('Y'); //4位数字完整表示的年份
$cur_m = date('n'); //数字表示的月份,没有前导零,1~12
$cur_d = date('j'); //月份中的第几天,没有前导零,1~31

4. Determine whether the student's date is a two-digit number

if($stu_bd < 10){
  $stu_bd = &#39;0&#39;.$stu_bd;
}
$date = "$stu_bm.$stu_bd";

5. Determine the constellation

  if($date >=1.21 && $date <= 2.19){
              $const = &#39;水瓶座&#39;;
       }elseif($date >=2.20 && $date <= 3.20){
              $const = &#39;双鱼座
       }elseif($date >=3.21 && $date <= 4.20){
              $const = &#39;白羊座&#39;;
       }elseif($date >=4.21 && $date <= 5.21){
              $const = &#39;金牛座&#39;;
       }elseif($date >=5.22 && $date <= 6.21){
              $const = &#39;双子座&#39;;
       }elseif($date >=6.22 && $date <= 7.22){
              $const = &#39;巨蟹座&#39;;
       }elseif($date >=7.23 && $date <= 8.23){
              $const = &#39;狮子座&#39;;
       }elseif($date >=8.24 && $date <= 9.23){
              $const = &#39;处女座&#39;;
       }elseif($date >=9.24 && $date <= 10.23){
              $const = &#39;天秤座&#39;;
       }elseif($date >=10.24 && $date <= 11.22){
              $const = &#39;天蝎座&#39;;
       }elseif($date >=11.23 && $date <= 12.21){
              $const = &#39;射手座&#39;;
       }else{
              $const = &#39;魔羯座&#39;;
       }

5. Result display

The above is the detailed content of PHP Basic Case 3: Determining Student Constellations. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn