PHP-2

1、PHP数组

·数组能够在单独的变量名中存储一个或多个值

·在PHP中,array()函数用于创建数组,有三种类型:
·索引数组-带有数字索引的数组
·关联数组-带有指定键的数组
·多维数组-包含一个或多个数组的数组

·索引数组的创建方式:
·自动分配(索引从0开始):$cars=array("vs","bm","ad");
·手动分配:$cars[0]="vs"; $cars[1]="bm"; $cars[2]="ad";

·获得数组长度-count()函数

·关联数组的创建方式:
·$age=array("P"=>"35","B"=>"37","J"=>"43");
·$age['P']="35";$age['B']="37";$age['J']="43";

·遍历关联函数foreach
<?php
$age=array("Bill"=>"35","Steve"=>"37","Peter"=>"43");

foreach($age as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>

运行结果:
Key=Bill, Value=35
Key=Steve, Value=37
Key=Peter, Value=43

2、数组排序

·升序排序 - sort()
·降序排序 - rsort()
·根据值,以升序对关联数组进行排序 - asort()
·根据键,以升序对关联数组进行排序 - ksort()
·根据值,以降序对关联数组进行排序 - arsort()
·根据键,以降序对关联数组进行排序 - krsort()

3、PHP表单处理

·PHP超全局变量$_get和$_post用于手机表单数据

<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

·填写完表单提交后,表单数据会发送到.php文件中处理

<html>
<body>

Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>

</body>
</html>

输出结果:

Welcome John
Your email address is john.doe@example.com

·使用get也能得到相同的结果

·get和post都创建数组,此数组包含键/值对,其中的键是表单控件的名称,而值是来自用户的输入数据。
$_get是通过URL参数传递到当前脚本的变量数组
$_post是通过HTTP POST传递到当前脚本的变量数组

·通过get方法发送的信息对任何人都是可见的(所有变量名和值都显示在URL中)。对所发送的信息数量
大于2000个字符。可用于发送非敏感的数据。绝不能用get来发送密码或其他敏感信息!

·通过post方法发送的信息对其他人是不可见的(所有名称或值会被嵌入到HTTP请求的主体中),并对发
送信息的数量无限制。

·表单验证

Name: <input type="text" name="name">
E-mail: <input type="text" name="email">
Website: <input type="text" name="website">
Comment: <textarea name="comment" rows="5" cols="40"></textarea>

Gender:
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
·当提交此表单时,通过method="post"发送
·$_SERVER["PHP_SELF"]是一种超全局变量,它返回当前执行脚本的文件名。
将表单数据发送到页面本身,用户就能够获得错误提示。
·htmlspecialchars()函数 把特殊字符转换为HTML实体

4、PHP日期和时间
·PHP date()函数语法:date("格式","时间")

<?php
date_default_timezone_set("Asia/Shanghai"); 设置时区
echo date("Y/m/d/l/h:i:sa"); ?> 输出年月日时分秒上午下午

·mktime()函数创建日期和时间
<?php
$d=mktime(9, 12, 31, 6, 10, 2015);
echo date("Y-m-d h:i:sa", $d);
?>

·strtotime()用字符串创建日期,字符串可使用tomorrow,next saturday等
<?php
$d=strtotime("10:38pm April 15 2015");
echo date("Y-m-d h:i:sa", $d);
?>

5、PHP include文件
·include/require 语句会获取指定文件中存在的所有文本、代码、标记,并复制到使用
include语句的文件中。即可以将PHP文件的内容插入到另一个PHP文件。

·include和require的区别:
require会生成致命错误(error)并停止脚本
include只生成警告(warning)并且脚本会继续

原文地址:https://www.cnblogs.com/xcc2016/p/5546322.html