[CyberSecurityLearning 47] PHP array

table of Contents

Array

Elements in the array:

The array can also contain an array (multidimensional array/two-dimensional array)

Array classification

Array creation

The first way to create an array

The second way to create an array

Access to array elements

Array traversal

Index array traversal (for loop)

Associative array traversal (foreach)

Pre-defined super-global array variables (important!)

$_GET (used to receive the parameters passed by the get method)★

Practice: write a simple login page

get.php code:

login.php code:

isset function

$_POST(★)

login.php code

post.php code

$_POST upload file (★)


Array

Is a type of variable (but not a basic variable type)
is a composite data type
key-value pair (key name key key value value)


Elements in the array:

(In addition to objects, data of any data type can be stored!) Arrays can also be stored in arrays

<?php
$name="AJEST";
$age=24;
$sex=true;
$grade=78.9;
//把上面四个变量放到同一个变量里面去
$stu[]="AJEST";//[]实际上是一个运算符
$stu[]=24;
$stu[]=true;
$stu[]=78.9;
//echo $stu; echo只能输出简单的数据类型
echo "<pre>";//pre标签格式化输出
//print_r($stu);//print_r是输出一个数组
var_dump($stu);
?>

 

The array can also contain an array (multidimensional array/two-dimensional array)

<?php
$students=array(
	1901 => array("AJEST",24,true,79.9),
	1902 => array("MDM",23,false,61),
	1903 => array("XL",25,true,59.9)
);
print_r($students);
//想取得第一个学生的成绩
echo $students[1901][3];
?>

 

<html>
	<title>学生基本信息表</title>
	<meta charset="utf-8">
</html>
 
<?php
$students=array(
	1901 => array(
	'name' => "GGG",
	'age' => 24,
	'esx' => true,
	'grade' => 79.9
	),
	1902 => array("MDM",23,false,61),
	1903 => array("XL",25,true,59.9)
);
echo "<table border=3>";
echo "<tr>
	<td>姓名</td>
	<td>年龄</td>
	<td>性别</td>
	<td>成绩</td>
</tr>";
 
foreach($students as $v){
	echo "<tr>";
	foreach($v as $vv){
		if($vv === true){
			$vv = "男";
		}
		if($vv === false){
			$vv = "女";
		}
		echo "<td>".$vv."</td>";
	}
	echo "</tr>";
}
echo "</table>";
?>

 

Array classification

Key-value pairs, positive integers, are called index arrays

Key-value pairs with semantic strings are called associative arrays

 

Array creation

The first way to create an array

$stu[]

1. In the case that the key name is not specified, assign a value to the array, and the key name starts counting from the largest and increases in sequence

2. You can manually give the key name

<?php
$stu[10]="AJEST";//[]实际上是一个运算符
$stu[20]=24;
$stu[30]=true;
$stu[]=78.9;
echo "<pre>";//pre标签格式化输出
var_dump($stu);
?>

<?php
$stu['name']="AJEST";//[]实际上是一个运算符
$stu['age']=24;
$stu['sex']=true;
$stu['grade']=78.9;
$stu[]="Content";
echo "<pre>";//pre标签格式化输出
//print_r($stu);//print_r是输出一个数组
var_dump($stu);
?>

The second way to create an array

array()

<pre>
<?php
$stu1=array("AJEST",24,true,78.9);//[]实际上是一个运算符
print_r($stu1);
$stu2=array(
	'name' => "MDM",
	'age' => "23",
	'sex' => false,
	'grade' =>99.9,
	'Something like this!'
);
print_r($stu2);
?>

 

Access to array elements

1. Read

2. Add (don't give the original key name in the array, if the given key name already exists, modify it)

3. Modify

<pre>
<?php
$stu1=array("AJEST",24,true,78.9);//[]实际上是一个运算符
print_r($stu1);
$stu2=array(
	'name' => "MDM",
	'age' => "23",
	'sex' => false,
	'grade' =>99.9,
	'Something like this!'
);

echo $stu1[0];//AJEST  查询名字
$stu1[3]=100;//成绩改成100
print_r($stu1);

?>

Array traversal

The for loop is only suitable for regularly indexed arrays

foreach language structure

Index array traversal (for loop)

 

<?php
$stu1=array("AJEST",24,true,78.9);
//count()--计算数组中的单元数目,或对象中属性的个数
for($i=0;$i<count($stu1);$i++)
{
	echo $stu1[$i]."<br/>";  //中括号是运算符
}
?>

Associative array traversal (foreach)

<?php
$stu1=array(
	'name'=>"MDM",
	'age'=>"23",
	'sex'=>false,
	'grade'=>99.9,
	'something like this!'
);
//foreach就是用来遍历数组的,foreach循环每执行一次就访问键值对(foreash有个自己的计数器,编程者不可见)智能
//数组中有多少个键值对,它就会循环几次
foreach($stu1 as $key=>$value){  //as就相当于把数组作拆分,$key这个变量名是自己定的,这是一种语言结构
	echo $key." => ".$value."<br/>";
}
?>

Foreach is our most important language structure, the most important!

Pre-defined super-global array variables (important!)

php defined, you can use it directly The
function can be used internally or externally

    $GLOBALS

    Refer to global variables that can be used in the global scope

    $_SERVER   

    An array containing information such as header, path, and script locations

    $_GET (used to receive the parameters passed by the url)

    An array of variables passed to the current script via URL parameters

    $_POST   

    When the Content-Type of the HTTP POST request is application/x-www-form-urlencoded or multipart/form-data, the variables will be passed to the current script as an associative array.

    $_FILES

    Array of items uploaded to the current script via HTTP POST

    $_COOKIE

    An array of variables passed to the current script via HTTP Cookies

    $_SESSION

    Array of SESSION variables available for the current script

    $_REQUEST

    By default, an array of $_GET, $_POST and $_COOKIE is included

    $_ENV

    An array of variables passed to the current script through the environment

$_GET (used to receive the parameters passed by the get method)★

<?php
var_dump($_GET);
?>

Accept the passed parameters from the URL

http://localhost/PHP/array/get.php?name=GGG (How do we pass parameters through get? We need to add a question mark after this script )

http://192.168.1.132/PHP/array/get.php?name=GGG&passwd=123456 (to pass two parameters, you need to use & to connect)

The name of the parameter becomes the key name

The value of the parameter will become the key value

Practice: write a simple login page

get.php code:

<meta charset="utf-8">
<?php
//是否点击登录按钮
if(isset($_GET['userSubmit'])){
	if(isset($_GET['userName']) && $_GET['userName']=="AJEST"
	&& isset($_GET['userPass']) && $_GET['userPass']=="123456"
	){
		echo "welcome,".$_GET['userName'];
	}else{
		echo "用户名或者密码错误<a href='./login.html'>请重新登录</a>";
	}
}else{
	echo "登录错误,请重新登录<a href='./login.html'>请通过表单重新登录</a>";
}
?>

login.php code:

<html>
<meta charset="utf-8">
<h1>用户登录</h1>
# action是提交到哪个页面(提交到当前路径下的get.php),提交方式是GET,target="_blank"就是在新标签页打开
<form action="./get.php" method="get" target="_blank">  
用户名:<input type="text" name="userName"><br />
密码:<input type="password" name="userPass"><br />
<input type="submit" name="userSubmit" value="登录">
</form>
</html>

When the GET array is used to receive the form submission, it will use the name attribute value of our tag as the key of the get array, and the value of the tag will be the key of the get array

isset function

<?php
var_dump(isset($_GET));//isset是判断变量是否被定义isset,并且非 null
var_dump(isset($_GET['name']));

?>

$_POST(★)

When the Content-Type of the HTTP POST request is application/x-www-form-urlencoded or multipart/form-data (file), the variables will be passed to the current script as an associative array.

The GET data is in the URL, and the POST data is in the request body of the HTTP request message

Receive the parameters passed by post

<?php
var_dump($_POST);
?>

login.php code

If enctype is not written, the default is application/x-www-form-urlencoded
and multipart/form-data is a file

<html>
<meta charset="utf-8">
<h1>用户登录</h1>
<form action="./post.php" method="post" target="_blank" enctype="">  
用户名:<input type="text" name="userName"><br />
密码:<input type="password" name="userPass"><br />
<input type="submit" name="userSubmit" value="登录">
</form>
</html>

post.php code

<pre>
<meta charset="utf-8">
<?php
//var_dump($_POST);
if(isset($_POST['userSubmit'])){
	if(isset($_POST['userName']) && $_POST['userName']=="GGG"
	&& isset($_POST['userPass']) && $_POST['userPass']=="123456"
	){
		echo "Welcome,".$_POST['userName'];
	}else{
		echo "用户名或密码错误<a href='./login.html'>请通过重新登录</a>";
	}
}else{
	echo "Error!<a href='./login.html'>请通过表单登录</a>";
}
?>

$_POST upload file (★)

$_FILES     is an array of items uploaded to the current script via HTTP POST

Regarding the upper limit of the upload file size, we need to modify the php.ini configuration file (skip here)

Modify the php.ini configuration file

Change the cache location after uploading files to C:\phpStudy\tmp\tmp

C:\phpStudy\tmp\tmp is to change the path of the php upload file

Restart phpStudy (make the configuration file take effect)

upfile.php :

<html>
<meta charset="utf-8">
<h1>
	文件上传测试
</h1>
<form
	action=""
	method="post"
	enctype="multipart/form-data"
>
	<input type="file" name="userUpFile">
	<input type="submit" name="userSubmit" value="上传">
</form>
</html>
<hr />
<?php
echo "<pre>";
if(isset($_POST['userSubmit'])){  //用户是否点击提交按钮
	var_dump($_FILES);   //$_FILES捕获文件上传信息
	$tmp_path=$_FILES['userUpFile']['tmp_name'];
	$path=__DIR__."\\".$_FILES['userUpFile']['name'];//__DIR__获取当前php脚本所在目录
	//echo $path;
	if(move_uploaded_file($tmp_path,$path)){ //把缓存文件移动到目标文件(缓存文件如果不移动会瞬间消失,要利用sleep函数才能看到)
	//move_uploaded_file(参数1,参数2);将上传上来的缓存文件的目录(参数1)保存到参数2目录下
		echo "upfile success!";
		echo "<br />".$_FILES['userUpFile']['name'];
	}else{
		echo "upfile failed";
	}
}
?>

 

 

Guess you like

Origin blog.csdn.net/Waffle666/article/details/115030506