php初识

php可以和html、js代码混写,但前提是文件后缀为.php

1、编写一个html页面,要求如下

title标签以自己的姓名为值,img标签加载自己任选的一张图片,编写一个form表单,分别使用get和post向服务器发送名称为name和pass的数据

<!--HTML代码-->
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="UTF-8" />
<title>王雨晗</title>
</head>
<body>
<img src="./lib/mali.jpg" alt="这是一个图片,为什么没加载呢" title="图片(假)">

<form method="get" action="test2.php"> <!--post表单修改method为post即可-->
<p>
<label for="name">who are you?</label>
<input name="name" type="text" size="20px" maxlength="20" />
</p>
<p>
please enter your password<input name="password" type="password" size="20px" >
</p>
<p>
<input type="submit" name="button" value="提交" >
<input type="reset" name="reset" value="清除" >
</p>
</form>

</body>
</html>

html页面

image-20250812163458514

get提交表单

image-20250812163731062

post提交表单

image-20250812164348878

2、使用php定义一个以自己姓名缩写为名称的变量,存储自己的姓名全拼

<?php
$WYH = "wangyuhan";
echo $WYH;
?>

image-20250812160206549

3、尝试修改第二题创建的变量值为int类型并打印

<?php
$WYH = 123123123;
echo $WYH;
?>

image-20250812161205357

4、演示可变变量的作用(演示两个$符号的作用并且解释)

<?php
$marisa = "aya";
$aya = "ayaaaaaa";
$$marisa = "koi";
echo $aya;
?>

image-20250812161856819

为什么最后输出变量 aya 的值为 koi 而不是 ayaaaaaa

  • $marisa = “aya”;赋予变量 $marisa 的值为字符串 aya
  • $aya = “ayaaaaaa”;赋予变量 $aya 的值为字符串 ayaaaaaa
  • $$marisa = “koi”;由于变量 $marisa 已经被赋值为 aya,这段代码可以理解为$aya = “koi”; 给变量 $aya 进行了一次重新赋值,值为 “koi”
  • 最后输出变量 $aya 的值就为 koi

5、接受第一题get方法传输的name和pass变量,输出到页面上

<?php
$inf_get_name = $_GET['name']; // 获取get方法传输过来的名为name的参数
$inf_get_passwd = $_GET['password']; // 获取get方法传输过来的名为password的参数
echo "This is name: " . $inf_get_name; // 输出参数
echo "<br>";
echo "This is password: " . $inf_get_passwd;
?>

6、使用burp或者yakit抓取自己form表单发送的get和post数据,观察区别

get方法

get方法传输数据会将数据拼接在url后,在浏览器地址栏可见,并且长度受协议限制。

image-20250812165314125

post方法

post方法传输数据,数据放在http请求体中,在浏览器地址栏不可见,长度不受协议限制。

image-20250812165131878

7、将当天笔记所有的代码编写一遍,熟悉概念

扩展

1、接收post方法传递的数据并且输出到页面上

<?php
$inf_post_name = $_POST['name']; // 获取post方法传输过来的名为name的参数
$inf_post_passwd = $_POST['password']; // 获取post方法传输过来的名为password的参数
echo "This is name: " . $inf_get_name; // 输出参数
echo "<br>";
echo "This is password: " . $inf_get_passwd;
?>

2、尝试使用form表单上传文件存储到服务器上

Author: wickt42
Link: http://example.com/2025/08/12/php初识/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.