PHP7 新特性 簡介

來源:互聯網
上載者:User

標籤:

整理了一些常用的新特性,歡迎點贊!!!

 

新增操作符
1、??
$username = $_GET[‘user‘] ?? ‘‘;
$username = isset($_GET[‘user‘]) ? $_GET[‘user‘] : ‘nobody‘;

2、<=>
$number1 <=> $number2; 當 $number1 小於、等於、大於 $number2 時 分別返回 -1,0,1


新增函數
intdiv(被除數, 除數) — 對除法結果取整
intdiv(3, 2) //1



define 可以定義數組
define(‘ANIMALS‘, [
‘dog‘,
‘cat‘,
‘bird‘
]);

 


傳回型別聲明
function test() :int
{
return 1; //true
return ‘1‘; //true
return ‘string‘; //false
}


標量型別宣告
function test(string $name) :int
{
return 22;
}
string integer float boolean


可以捕獲核心錯誤
Error階層
Throwable
  Error
    ArithmeticError
      DivisionByZeroError

    AssertionError
    ParseError
    TypeError

  Exception


核心排序的最佳化
php5: array(1=>0, 0=>0) //快速排序(非穩定排序)
php7: array(0=>0, 1=>0) //快速排序+選擇排序(穩定排序)



比php5多了個抽象文法數(abstract snytax tree) AST
PHP -> Parser -> AST ->Opcodes -> Execution
效能增加了,記憶體消耗也在增加但可以忽略不計


匿名類 可以通過new class執行個體化一個匿名類
function getAnonymousClass($config) {
return new class($config) {};
}



暫時綁定一個方法到對象上並調用
$f = function() {
p($this->name);
};

class F {
private $name = ‘F‘;
}

$f->call(new F);




統一的文法變數
括弧不影響行為 從左至右

$a = ‘b‘;
$b = [‘1‘, ‘2‘, ‘3‘];
var_dump($$a[1]);
var_dump(($$a)[1]);

php5:
Notice: Uninitialized string offset: 1 in E:\Program Files\phpStudy\WWW\test.php on line 4
Notice: Undefined variable: in E:\Program Files\phpStudy\WWW\test.php on line 4
NULL

Parse error: syntax error, unexpected ‘[‘ in E:\Program Files\phpStudy\WWW\test.php on line 4

php7: string(1) "2" string(1) "2"


     Expression        PHP5        PHP7
$$foo[‘bar‘][‘baz‘]  ${$foo[‘bar‘][‘baz‘]}  ($$foo)[‘bar‘][‘baz‘]
$foo->$bar[‘baz‘]  $foo->{$bar[‘baz‘]}  ($foo->$bar)[‘baz‘]
$foo->$bar[‘baz‘]()  $foo->{$bar[‘baz‘]}()  ($foo->$bar)[‘baz‘]()
Foo::$bar[‘baz‘]()  Foo::{$bar[‘baz‘]}()  (Foo::$bar)[‘baz‘]()




關於命名空間
// Pre PHP 7 code
use some\namespace\ClassA;
use some\namespace\ClassB;
use some\namespace\ClassC as C;

use function some\namespace\fn_a;
use function some\namespace\fn_b;
use function some\namespace\fn_c;

use const some\namespace\ConstA;
use const some\namespace\ConstB;
use const some\namespace\ConstC;

// PHP 7+ code
use some\namespace\{ClassA, ClassB, ClassC as C};
use function some\namespace\{fn_a, fn_b, fn_c};
use const some\namespace\{ConstA, ConstB, ConstC};


list 的修改
  1、
  list($array[], $array[], $array[]) = [1, 2, 3];
  var_dump($array);
  php5: array(3) { [0]=> int(3) [1]=> int(2) [2]=> int(1) }
  php7: array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }


  2、不允許賦空值
  list() = $a;
  list(,,) = $a;
  list($x, list(), $y) = $a;

  php7會報錯 Fatal error: Cannot use empty list

  3、不再支援字串拆分
  $string = "xy";
  list($x, $y) = $string;
  var_dump($x, $y);

  php5: string(1) "x" string(1) "y"
  php7: null null

foreach 的修改
變數引用,會影響對數組的迴圈
$array = [0];
foreach ($array as &$val) {
var_dump($val);
$array[1] = 1;
}
php5:int(0)
php7: int(0) int(1)

 

參考的源地址:http://www.php7.site/book/php7/variable-changes-22.html

提高php效能的tips:http://www.laruence.com/2015/12/04/3086.html

PHP7 新特性 簡介

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.