Home Backend Development PHP Tutorial PHP Basic Case 3: Determining Student Constellations

PHP Basic Case 3: Determining Student Constellations

Nov 13, 2020 am 10:55 AM
php

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 (条件)
 {
 条件成立时要执行的代码;
 }
Copy after login

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
 {
 条件不成立时执行的代码;
 }
Copy after login

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

Between several conditions Execute a block of code when established.

.

Syntax

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

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,此处代码将执行; 
}
Copy after login

4. Code implementation1. Define variables to save student information

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

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];
Copy after login

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
Copy after login

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

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

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;;
       }
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

See all articles