php 5.6
1、可以使用表达式定义常量 https://php.net/manual/zh/migration56.new-features.php
在之前的 php 版本中,必须使用静态值来定义常量,声明属性以及指定函数参数默认值。 现在你可以使用包括数值、字符串字面量以及其他常量在内的数值表达式来 定义常量、声明属性以及设置函数参数默认值。
f().\n;echo c::sentence;?>
可以通过 const 关键字来定义类型为 array 的常量。
2、使用 ... 运算符定义变长参数函数 现在可以不依赖 func_get_args(), 使用 ... 运算符 来实现 变长参数函数。
1 [1] => 2 [2] => 3)?>
3、使用 ** 进行幂运算 加入右连接运算符 ** 来进行幂运算。 同时还支持简写的 **= 运算符,表示进行幂运算并赋值。
printf(2 ** 3); // 8$a = 2;$a **= 3;printf($a); // 8
4、use function 以及 use const use 运算符可以在类中导入外部的函数和常量了。 对应的结构为 use function 和 use const。
5、加入 hash_equals() 函数,以恒定的时间消耗来进行字符串比较,以避免时序攻击
6、加入 __debuginfo() 当使用 var_dump() 输出对象的时候,可以用来控制要输出的属性和值。
prop = $val; } public function __debuginfo() { return $this->prop; }}var_dump(new c(42));?>
php 5.5 1、新增 generators yield关键字用于当函数需要返回一个迭代器的时候, 逐个返回值。
function number10(){ for($i = 1; $i 'john'), array('id' => 3245,'name' => 'smith'), array('id' => 5342,'name' => 'peter'));//从结果集中取出 name 列$names = array_column($records, 'name');print_r($names);//从结果集中总取出 name 列,用相应的 id 作为键值$names = array_column($records, 'name', 'id');print_r($names);```#php 5.4##1、新增 traitshttps://php.net/manual/zh/language.oop5.traits.php```// traits不能被单独实例化,只能被类所包含trait sayworld{ public function sayhello() { echo 'world!'; }}class myhelloworld{ // 将sayworld中的成员包含进来 use sayworld;}$xxoo = new myhelloworld();// sayhello() 函数是来自 sayworld 构件的$xxoo->sayhello();```##2、新增短数组语法```// 原来的数组写法$arr = array(key => value, key2 => value2);$arr = array(1,2,3,4);// 简写形式$arr = [key => value, key2 => value2];$arr = [1,2,3,4];```##3、新增支持对函数返回数组的成员访问解析```print func()[0];```##4、无论 php.ini 中是否设置 short_open_tag, 格式总是可用。这种简写形式被称为 short open tag, 在 php5.3 起被默认开启,在 php5.4 起总是可用。 使用这种简写形式在 html 中嵌入 php 变量将会非常方便。##5、内置用于开发的 cli 模式的 web server
//启动web服务器
php -s localhost:8000
//启动时指定根目录
php -s localhost:8000 -t /home/me/public_html/foo
//使用路由(router)脚本
php -s localhost:8000 index.php //所有的请求都会由index.php来处理。
##6、新增在实例化时访问类成员
(new foo)->bar();
##7、新增了动态访问静态方法的方式
$func = funcxxoo;
a::{$func}();
##8、闭包支持 $this##9、新增二进制直接量
$bin = bindec(110011); //之前需要这样写
$bin = 0b110011;
echo $bin; //51
##10、session提供了上传进度支持通过 ```$_session[upload_progress_name]``` 就可以获得当前文件上传的进度信息,结合 ajax 就能很容易的实现上传进度条。##11、默认使用 mysqlnd现在mysql, mysqli, pdo_mysql默认使用mysqlnd本地库,在php5.4以前需要:```./configure --with-mysqli=mysqlnd```现在:```./configure --with-mysqli ```##12、让 json 更懂中文
echo json_encode(中文, json_unescaped_unicode);
//中文
##13、default_charset从iso-8859-1已经变为utf-8默认发送“content-type: text/html; charset=utf-8”#php 5.3##1、支持命名空间https://php.net/manual/zh/language.namespaces.php
#2、增加后期静态绑定https://php.net/manual/zh/language.oop5.late-static-bindings.php在php中,我们可以在类中通过self关键字或者__class__来判断或调用当前类。但有一个问题,如果我们是在子类中调用,得到的结果将是父类。因为在继承父类的时候,静态成员就已经被绑定了。
class a
{
static public function callfuncxxoo()
{
print self::funcxxoo();
}
static public function funcxxoo(){ return a::funcxxoo();}
}
class b extends a
{
static public function funcxxoo()
{
return b::funcxxoo;
}
}
$b = new b;
$b->callfuncxxoo();
输出是:
a::funcxxoo()
php 5.3.0中增加了一个static关键字来引用当前类,即实现了延迟静态绑定:
class a
{
static public function callfuncxxoo()
{
print static::funcxxoo();
}
// ...
}
// ...
这样就会像预期一样输出了:
b::funcxxoo
#3、增加 goto 操作符https://php.net/manual/zh/control-structures.goto.phpgoto 语句有可能会导致程序流程不清晰,可读性减弱,但在某些情况下具有其独特的方便之处,例如中断深度嵌套的循环和 if 语句。
//以上运行时会输出 2
#4、添加了原生的闭包(lambda/匿名函数)支持https://php.net/manual/zh/functions.anonymous.php#5、新增两个魔术方法, __callstatic 和 __invokehttps://php.net/manual/zh/language.oop5.magic.php用静态方式中调用一个不可访问方法时,__callstatic() 会被调用。当尝试以调用函数的方式调用一个对象时,__invoke() 方法会被自动调用。
class a
{
public function __invoke($str)
{
print a::__invoke(): {$str};
}
}
$a = new a;
$a(hello world);
输出是:
a::__invoke(): hello world
#6、添加 nowdoc 语法支持https://php.net/manual/zh/language.types.string.php#language.types.string.syntax.nowdoc
$str = <<<'eod'
example of string
spanning multiple lines
using nowdoc syntax.
eod;
就象 heredoc 结构类似于双引号字符串,nowdoc 结构是类似于单引号字符串的。nowdoc 结构很象 heredoc 结构,但是 nowdoc 中不进行解析操作。#7、heredoc 结构中可以用双引号来声明标识符了。https://php.net/manual/zh/language.types.string.php#language.types.string.syntax.heredoc
#8、const 关键字可用来在类定义之外定义常量了。https://php.net/manual/zh/language.constants.syntax.php
define(constant_a, hello world);
const constant_b = 'hello world';
const 形式仅适用于常量,不适用于运行时才能求值的表达式:
// 正确
const xxoo = 1234;
// 错误
const xxoo = 2 * 617;
和使用 define() 来定义常量不同的是,使用 const 关键字定义常量必须处于最顶端的作用域,因为用此方法是在编译时定义的。即不能在函数内,循环内以及 if 语句之内用 const 来定义常量。#9、三元运算符可以简写省略中间的部分表达式 expr1 ?: expr3 ,当 expr1 为 true 时返回 expr1,否则返回 expr3。#10、异常可以嵌套了
#11、可以动态访问静态变量了
上边运行时输出:
123
```
12、mail()函数支持记录发送日志了 在配置文件 php.ini 中可设置日志路径。参数名:mail.log
参考资料: 1、https://php.net/manual/zh/migration53.new-features.php
2、https://php.net/manual/zh/migration54.new-features.php
3、https://php.net/manual/zh/migration55.new-features.php
4、https://php.net/manual/zh/migration56.new-features.php
5、http://segmentfault.com/a/1190000000403307
6、http://blog.csdn.net/heiyeshuwu/article/details/16884725
