文章目录
- PHP基本知识
- PHP基本语法
- 1.php变量、常量
- 2.php打印输出
- 3.php运算符
- if、switch语句
- array数组
- 数组声明
- 数组键名定义的规则
- 数组的赋值操作
- php常见数组的分类
- 1.数值数组
- 2.关联数组
- 3.多维数组
- 循环结构与数组遍历
- 数组遍历--for
- break && continue
- foreach语句
- 1.遍历一维数组
- 2.遍历二维数组
- < form >元素
- 概念
- < input >元素
- type属性
- name属性和value属性
- 拓展属性
- < form >表单两个核心属性
- action属性
- method属性(重点)
- get请求
- post请求
- PHP与form表单综合运用
- 普通打印
- 判断后表单打印
PHP基本知识
- php注释快捷键:Ctrl + ?
- php文件名不能出现中文名,出现则无法输出
PHP基本语法
1.php变量、常量
- 变量以$符号开头,其后是变量名称
<?php
$int = 22;
$float = 15.2;
$str = "22";
$bool = false;
$arr = [1,15.2,"22","true"];
2.php打印输出
- echo
显示输出内容到浏览器(不能打印数组)
- print_r
显示输出内容到浏览器(能打印数组)
- var_dump()
返回变量的完整信息
<?php$int = 22;
$float = 15.2;
$str = "22";
$bool = false;
$arr = [1,15.2,"22","true"];echo $int,$float;
print_r($arr);
var_dump($arr);
3.php运算符
if、switch语句
语法格式同C/C++
array数组
数组声明
1.未定义键名
小括号、中括号效果一致
<?php$as = array(1,2,3);
var_dump($as);$arr = [1,2,3];
var_dump($arr);
2.定义键名
<?php// $as = array(1,2,3);
// var_dump($as);// $arr = [1,2,3];
// var_dump($arr);$as = array(1,2,3);
var_dump($as);$arr = ["1" => 1,"2" => 2,"3" => 3];
var_dump($arr);
数组键名定义的规则
等价于:a[8] = a; a[1] = c; a[0] = D
数组的赋值操作
声明: $arr[键名] = ‘e’(此处双引号也可以~)
<?php
$arr = [8 =>'a',"08" => 'a',1 =>'a',"1" =>'b',1.5 =>'c',false =>'D',
];var_dump($arr);
// $arr[false] = "e";
$arr[false] = 'e';
var_dump($arr);
php常见数组的分类
1.数值数组
上面的都是
2.关联数组
3.多维数组
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><?php$class = ["项目部" => ["name" => "张三","gemder" => "man"],"市场部" => ["name" =>"李四","gender" => "man"]];var_dump($class)?>
</body>
</html>
注意:在html中写php时,要记得以?>结尾,否则网页报错如下:
效果:
循环结构与数组遍历
数组遍历–for
<?php
$number = ['1','2','3','4','5','6'];
for($i = 0 ; $i < count($number) ; $i++){echo $number[$i];
}
count()函数功能:统计数组长度
break && continue
功能同C/C++
foreach语句
1.遍历一维数组
不加键
<?php$number = [1,2,3,4,5,6];
foreach($number as $v){echo $v;
}
加键
<?php// $number = [1,2,3,4,5,6];
// foreach($number as $v){
// echo
// }$number = [1,2,3,4,5,6];
foreach($number as $k=>$v){echo $k . '=>' . $v;// echo "<br>";echo '<br>';
}
echo ‘< br >’; 换行
2.遍历二维数组
1.不加键
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><?php$class = ["项目部" => ["name" => "张三","gender" => "man"],"市场部" => ["name" =>"李四","gender" => "man"]];foreach($class as $v){echo $v['name'];echo $v['gender'];}?>
</body>
</html>
2.加键
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><?php$class = ["项目部" => ["name" => "张三","gender" => "man"],"市场部" => ["name" =>"李四","gender" => "man"]];foreach($class as $k => $v){echo $k . '=>' .$v['name'];echo $k . '=>' .$v['gender'];echo "<br>";}?>
</body>
</html>
< form >元素
概念
登录框、搜索框、填写信息的文本框,本质上都是form表单制作的。form表单是HTML前端和后端语言交互的一个重要元素。
< input >元素
是最重要的表单元素,存在于< form >元素的内部
type属性
reset | 重置按钮,所有数据清零 |
---|
submit:检测输入格式是否正确
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><form>用户:<input type="text"><br>密码:<input type="password"><br>邮箱:<input type="email"><br>男<input type="radio" name="gender">女<input type="radio" name="gender"><br><input type="submit"><input type="reset"><br></form>
</body>
</html>
name属性和value属性
value
radio和submit的值不可以输入,所以要手动输入value值
name
拓展属性
属性 | 功能 |
---|---|
required | 输入框内容不能为空 |
disabled | 输入框禁止输入 |
autofocus | 输入框自动获取焦点 |
required
disable
autofocus
< form >表单两个核心属性
action属性
功能:表单提交地址
demo.php
check.php
method属性(重点)
get请求
demo中显示为get请求
check中显示为get请求
post请求
demo中显示为get请求
check中显示为post请求
demo.php
check.php
PHP与form表单综合运用
普通打印
代码:
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><center><form action="" method="post"><table><tr><td>学号:</td><td><input type="text" name="stu_no"></td></tr><tr><td>姓名:</td><td><input type="text" name="stu_name" value="ludan"></td></tr><tr><td>姓别:</td><td>男:<input type="radio" name="gender" value="男">女:<input type="radio" name="gender" value="女"></td></tr><tr><td>电话:</td><td><input type="text" name="telephone"></td></tr><tr><td>年龄:</td><td><input type="text" name="age"></td></tr><tr><td>学院:</td><td><input type="text" name="college"></td></tr><tr><td colspan="2" align="center"><input type="submit" name="submit" value="保存"><input type="reset" name="reset" value="重置"></td></tr></table></form><?phpprint_r($_POST);?></center>
</body>
</html>
输出:
判断后表单打印
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><center><form action="" method="post"><table><tr><td>学号:</td><td><input type="text" name="stu_no"></td></tr><tr><td>姓名:</td><td><input type="text" name="stu_name" value="ludan"></td></tr><tr><td>姓别:</td><td>男:<input type="radio" name="gender" value="男">女:<input type="radio" name="gender" value="女"></td></tr><tr><td>电话:</td><td><input type="text" name="telephone"></td></tr><tr><td>年龄:</td><td><input type="text" name="age"></td></tr><tr><td>学院:</td><td><input type="text" name="college"></td></tr><tr><td colspan="2" align="center"><input type="submit" name="submit" value="保存"><input type="reset" name="reset" value="重置"></td></tr></table></form><?php// print_r($_POST);// 判断发出的是否post请求才输出,且按表格输出if($_SERVER["REQUEST_METHOD"] === 'POST'){echo '<p style="color:red">print_r($_POST)打印出来的结果</p>';echo '<pre>';print_r($_POST);#两个print_r???echo '</pre>';echo "<table border='1'>";echo '<tr>';echo '<th>学号</th>';echo '<th>姓名</th>';echo '<th>性别</th>';echo '<th>电话</th>';echo '<th>年龄</th>';echo '<th>学院</th>';echo '</tr>';//数组访问,中括号访问echo '<tr>';echo "<td>{$_POST['stu_no']}</td>";echo "<td>{$_POST['stu_name']}</td>";echo "<td>{$_POST['gender']}</td>";echo "<td>{$_POST['telephone']}</td>";echo "<td>{$_POST['age']}</td>";echo "<td>{$_POST['college']}</td>";echo '</tr>';echo "</table>";}?></center>
</body>
</html>