Home > Backend Development > PHP Tutorial > PHP basic case one: display student information card

PHP basic case one: display student information card

善始善终
Release: 2023-04-09 16:14:02
Original
4963 people have browsed it

1. Requirements analysis:

# Please use PHP variables to save the student’s name, date of birth, subject and student number, and finally The student's information is output to the web page for display. Among them, when defining the student's date of birth and student number, the following two conditions must be met.

1. The date of birth is in the Gregorian calendar, and the format is YYYY-MM-DD, for example, 2003-09-08, which means you were born on September 8, 2003 in the Gregorian calendar.

2. The student number is composed of 0 plus a two-digit year, a two-digit month and date, and then a three-digit student serial number, for example, May 2012 The serial number of the first student in a certain class on the 19th is 0120519001.

3. Use the feature that PHP code can be embedded into HTML pages, write a table with 4 rows and 2 columns, embed PHP code in the table, and output the student's name, date of birth, Subject and student number.

2. Design ideas

1.Define variables to save student information and need to be defined Several variables, what are these variables?

2. To embed code into an HTML page, do you need to write a table with several rows and columns? What information do these tables output about students?

3. Knowledge Reserve

1. What is a variable?

#Variables in programs originate from mathematics and can store results or represent abstract concepts in programming languages. A simple understanding of a variable is a container that temporarily stores values. It can store numbers, text, or some complex data.

2. How to declare variables?

Because PHP is a weakly typed language, there is no need to declare variables in advance before using them. The variables will be automatically created when they are assigned a value for the first time. This reason makes the syntax of PHP and C Languages, strongly typed languages ​​such as Java are very different.

Declaring a PHP variable must use a dollar sign "$" followed by the variable name, and then use "=" to assign a value to the variable.

<span style="color: rgb(0, 0, 0);"><?php<br/>$a=1;<br/>$b=&#39;你好&#39;;<br/>?></span>
Copy after login

3. Variable naming rules

Variable name It cannot be defined arbitrarily. A valid variable name should meet the following requirements: (1) The variable must start with a $ symbol, followed by the name of the variable. $ is not part of the variable name;

(2) The variable name must start with a letter or underscore;

(3) The variable name cannot start with a number;

(4) Variable names can only contain letters (A~z), numbers (0~9) and underscores (_);

(5) What is incomprehensible with other languages ​​is , some keywords in PHP can also be used as variable names (such as $true, $for).

4、几点提示

当使用多个单词构成变量名时,可以使用下面的命名规范:

(1)下划线命名法:将构成变量名的单词以下划线分割,例如 $get_user_name、$set_user_name;

(2)驼峰式命名法(推荐使用):第一个单词全小写,后面的单词首字母小写,例如 $getUserName、$getDbInstance;

(3)帕斯卡命名法:将构成变量名的所有单词首字母大写,例如 $Name、$MyName、$GetName。

四、代码实现

<span style="color: rgb(0, 0, 0);"><?php<br/>//定义变量保存学生资料<br/>$name = &#39;王六&#39;;//保存学生的姓名<br/>$birth = &#39;2003-08-07&#39;;//保存学生的出生日期<br/>$subject = &#39;PHP&#39;;//保存学生的所属学科<br/>$snum = &#39;0150427001&#39;;//保存学生的学号<br/>?></span>
Copy after login

定义好PHP代码,编写一个4行2列的表格,在表格中嵌入PHP代码,分别输出学生的姓名、出生日期、学科以及学号。

<span style="color: rgb(0, 0, 0);"><table><br/><tr><br/><th colspan="3">展示学生资料</th><br/></tr><br/><tr><br/><td>姓  名:</td><br/><td><?php echo $name;?></td><br/></tr><br/><tr><br/><td>出生日期:</td><br/><td><?php echo $birth;?></td><br/></tr><br/><tr><br/><td>学  科:</td><br/><td><?php echo $subject;?></td><br/></tr><br/><tr><br/><td>学  号:</td><br/><td><?php echo $snum;?></td><br/></tr><br/></table></span>
Copy after login

五、效果展示

PHP basic case one: display student information card

The above is the detailed content of PHP basic case one: display student information card. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template