PHPDay12:PHP7

0x00 PHP7版本新增特性

1.新增操作符

(1)<=>  比较两个数大小

-1:前者小于后者

0:前者等于后者

1:前者大于后者

(2)三元操作符改进

$c = $a??3

当$a存在且不为0的时候就将$a赋值给c。否则将3赋值给c

(3)unicode解析

字符的Unicode形式在网页中也可以正常解析

例如:

echo "\u{4f60}"  你

(4)类型声明

5中类型声明中只能是数组和对象

7中可以为string int float bool array object

还新增了返回值的声明

<html>
<body>
    <?php
    function fun(int $a):int{
        return $a;
    }
    ?>
</body>
</html>

(5)可以定义数组为一个常量

define("USER",[1,2,3]);

(6)func_get_arg()和func_get_args()这两个方法返回参数当前的值,而不是传入时的值,当前的值有可能会被修改

<html>
<body>
    <?php
    function fun(int $a,int $b){
        $a = 999;
        var_dump(func_get_args());
    }
    fun(123,324);
    ?>

</body>
</html>

0x01 PHP7废弃的特性

不能使用同名的构造函数

class test{

public function test(){

}

}

实例方法不能用静态方法的方式调用

article::test() test非静态方法

0x02 post表单接受数据的几种方式

$_POST ,$HTTP_RAW_POST_DATA ,php://input 三者之间的区别

以上三者都可以接受post传输来表单数据

微信开发中必须用php://input

第二种方式在php7中已经被移除

1.$_POST

只能接受下面两种格式的数据

  1. application/x-www.form-urlencoded 不写enctype="multipart/form-data"就是这种Content-type
  2. enctype="multipart/form-data"

不能接收Content-type为:application/json 和 application/xml

2.HTTP_RAW_POST_DATA

$GLOBALS['HTTP_RAW_POST_DATA']

需要设置php.ini中的always_poplulate_raw_post_data 为on

且不能用enctype="multipart/form-data"

3.php://input

不能获取 enctype="multipart/form-data"

但是能接收Content-type为:application/json 和 text/xml 或者 application/xml微信开发的时候这两种格式常见。

使用方式:$data = file_get_contents("php://input");

返回值为string格式

实例:

form.php

<html>
<head>
<meta charset="utf-8">
<title>表单</title>
</head>
<body>
    <form action="post.php" method="post">
    <input type="text" name="name"><br>
    <input type="submit" value="提交">
    </form>
</body>
</html>

post.php

<?php
var_dump($_POST);
var_dump($GLOBALS[HTTP_RAW_POST_DATA]);
var_dump(file_get_contents("php://input"));
?>
发布了156 篇原创文章 · 获赞 19 · 访问量 8931

猜你喜欢

转载自blog.csdn.net/weixin_43415644/article/details/104102719