PHP Tutorial: Using Variables to Store and Display Student Information
This tutorial explains how to define PHP variables for a student's name, birth date, subject and student number, enforce specific formatting rules, and embed the variables in a 4‑row × 2‑column HTML table to display the information on a web page.
This article, contributed by a certified author of php中文网, introduces a PHP exercise that requires storing a student's name, birth date, subject and student number in variables and displaying them on a web page.
Requirement analysis: the birth date must follow the Gregorian format YYYY‑MM‑DD; the student number must start with 0 followed by a two‑digit year, two‑digit month, two‑digit day and a three‑digit sequence (e.g., 0120519001); a 4‑row × 2‑column HTML table should embed PHP code to output the four fields.
Design ideas: define four variables ($name, $birth, $subject, $snum); embed PHP in an HTML table; each table row corresponds to name, birth date, subject, and student number.
Knowledge base: a variable is a temporary container for values such as numbers or strings; PHP is weakly typed and creates a variable on first assignment without prior declaration; naming rules require a leading $, a letter or underscore as the first character, no leading digit, and only letters, digits, or underscores thereafter, while PHP permits some keywords as variable names; common naming conventions include snake_case, camelCase (recommended), and PascalCase.
Code example for simple variable assignment:
<?php
$a=1;
$b='你好';
?>Main student‑information code:
<?php
// define variables to store student data
$name = '王六'; // student name
$birth = '2003-08-07'; // birth date
$subject = 'PHP'; // subject
$snum = '0150427001'; // student number
?>HTML table with embedded PHP to display the data:
<table>
<tr><th colspan="3">展示学生资料</th></tr>
<tr><td>姓 名:</td><td><?php echo $name; ?></td></tr>
<tr><td>出生日期:</td><td><?php echo $birth; ?></td></tr>
<tr><td>学 科:</td><td><?php echo $subject; ?></td></tr>
<tr><td>学 号:</td><td><?php echo $snum; ?></td></tr>
</table>The resulting page renders a table showing the student's name, birth date, subject and student number.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
