Experiment 2-3 PHP basic syntax programming (3)
1. Experimental hours
2 hours
2. Experimental purpose
(1) Become more familiar with the basic syntax of PHP;
(2) Master the selection structure of PHP;
(3) Master the loop structure of PHP;
3. Experimental equipment
PC computer, equipped with Win10 operating system, Word2019, PHPStudy+eclipse for php
4.Experimental content and procedures
( 1 ) Experimental steps
① Create a new Project File—new—Local PHP Project and name it PHP2_3
②Create two new php files, right-click the project name—new—PHP File
③Enter the code and save it
④Verify the correctness of the code in the browser and output the running results
(2) Experimental content
1) Web calculator
To implement addition, subtraction, multiplication and division operations, input two operands, select an operator, and output the calculation result.
Source code: 2_3_1.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>网页计算器</title>
</head>
<body>
<?php
if (isset($_POST['ok']) && is_numeric($_POST['num1']) && is_numeric($_POST['num2'])) {
$a = $_POST['num1'];
$b = $_POST['num2'];
$c = $_POST['cal'];
if ($c == '+') {
$result = $a + $b;
} else if ($c == '-') {
$result = $a - $b;
} else if ($c == '*') {
$result = $a * $b;
} else if ($c == '/') {
$result = $a / $b;
}
}
?>
<form name="form" method="post" action="">
<table>
<tr>
<td>操作符1</td>
<td>运算符</td>
<td>操作数2</td>
<td></td>
<td>计算结果</td>
</tr>
<tr>
<td><input type="text" name="num1" value="<?php echo $a; ?>"> </td>
<td>
<select name="cal">
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
</select>
</td>
<td><input type="text" name="num2" value="<?php echo $b; ?>"></td>
<td><input type="submit" value="计算" name="ok"></td>
<td><input type="text" name="num3 " value="<?php echo $result; ?>"></td>
</tr>
</table>
</form>
</body>
</html>
Result screenshot:
2) Login verification form
Prompt boxes will appear after successful and failed logins.
Source code: 2_3_2.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录验证表单</title>
<style type="text/css">
#tr1{
text-align: center;
background-color: darkgray;
}
#tr4{
text-align: center;
}
table{
border: groove;
border-color: black;
}
</style>
</head>
<body>
<form name="form" method="post" action="">
<table align="center" border="all" cellspacing="0">
<tr>
<td colspan="2" id="tr1">用户登录表单</td>
</tr>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan="2" id="tr4"><input type="submit" name="submit" value="登录">
<input type="reset" value="重置"></td>
</tr>
</table>
</form>
</body>
</html>
<?php
if (isset($_POST['submit'])){
$username = $_POST['username'];
$password = $_POST['password'];
if ($username == "admin" && $password == "123456")
echo "<script>alert('success')</script>";
else
echo "<script>alert('failed')</script>";
}
?>
Result screenshot:
5. Experimental results
Through this experiment I learned:
- How to transfer the value in the form input box to the php program, and how to transfer the result processed by the program back to the form text box, value = " <?php echo $result; ?> "
- Why is the value in the text box cleared after clicking the Calculate button for the last entered value? Write the php program before the form.