> 백엔드 개발 > PHP 튜토리얼 > 小弟我整理的PHP 7.0主要新特性

小弟我整理的PHP 7.0主要新特性

WBOY
풀어 주다: 2016-06-13 12:28:19
원래의
714명이 탐색했습니다.

我整理的PHP 7.0主要新特性

原文:http://php.net/manual/en/migration70.new-features.php

1.标量参数类型声明

现在支持字符串(string)、整型(int)、浮点数(float)、及布尔型(bool)参数声明,以前只支持类名、接口、数组及Callable

两种风格:强制转换模式(默认)与严格模式

<code class="sourceCode php"><span class="kw"><?php</span><span class="co">// Coercive mode</span><span class="kw">function</span> sumOfInts<span class="ot">(</span>int <span class="st">...</span><span class="kw">$ints</span><span class="ot">)</span>{    <span class="kw">return</span> <span class="fu">array_sum</span><span class="ot">(</span><span class="kw">$ints</span><span class="ot">);</span>}<span class="fu">var_dump</span><span class="ot">(</span>sumOfInts<span class="ot">(</span><span class="dv">2</span><span class="ot">,</span> <span class="st">&#39;3&#39;</span><span class="ot">,</span> <span class="fl">4.1</span><span class="ot">));</span></code>
로그인 후 복사

2.返回类型声明

<code class="sourceCode php"><span class="kw"><?php</span><span class="kw">function</span> arraysSum<span class="ot">(</span><span class="fu">array</span> <span class="st">...</span><span class="kw">$arrays</span><span class="ot">)</span>: <span class="fu">array</span>{    <span class="kw">return</span> <span class="fu">array_map</span><span class="ot">(</span><span class="kw">function</span><span class="ot">(</span><span class="fu">array</span> <span class="kw">$array</span><span class="ot">)</span>: int {        <span class="kw">return</span> <span class="fu">array_sum</span><span class="ot">(</span><span class="kw">$array</span><span class="ot">);</span>    }<span class="ot">,</span> <span class="kw">$arrays</span><span class="ot">);</span>}<span class="fu">print_r</span><span class="ot">(</span>arraysSum<span class="ot">([</span><span class="dv">1</span><span class="ot">,</span><span class="dv">2</span><span class="ot">,</span><span class="dv">3</span><span class="ot">],</span> <span class="ot">[</span><span class="dv">4</span><span class="ot">,</span><span class="dv">5</span><span class="ot">,</span><span class="dv">6</span><span class="ot">],</span> <span class="ot">[</span><span class="dv">7</span><span class="ot">,</span><span class="dv">8</span><span class="ot">,</span><span class="dv">9</span><span class="ot">]));</span></code>
로그인 후 복사

3.??运算符

?? 用于替代需要isset的场合,这是一个语法糖。

<code class="sourceCode php"><span class="kw"><?php</span><span class="co">// Fetches the value of $_GET[&#39;user&#39;] and returns &#39;nobody&#39;</span><span class="co">// if it does not exist.</span><span class="kw">$username</span> = <span class="kw">$_GET</span><span class="ot">[</span><span class="st">&#39;user&#39;</span><span class="ot">]</span> <span class="ot">??</span> <span class="st">&#39;nobody&#39;</span><span class="ot">;</span><span class="co">// This is equivalent to:</span><span class="kw">$username</span> = <span class="fu">isset</span><span class="ot">(</span><span class="kw">$_GET</span><span class="ot">[</span><span class="st">&#39;user&#39;</span><span class="ot">])</span> <span class="ot">?</span> <span class="kw">$_GET</span><span class="ot">[</span><span class="st">&#39;user&#39;</span><span class="ot">]</span> <span class="ot">:</span> <span class="st">&#39;nobody&#39;</span><span class="ot">;</span><span class="co">// Coalescing can be chained: this will return the first</span><span class="co">// defined value out of $_GET[&#39;user&#39;], $_POST[&#39;user&#39;], and</span><span class="co">// &#39;nobody&#39;.</span><span class="kw">$username</span> = <span class="kw">$_GET</span><span class="ot">[</span><span class="st">&#39;user&#39;</span><span class="ot">]</span> <span class="ot">??</span> <span class="kw">$_POST</span><span class="ot">[</span><span class="st">&#39;user&#39;</span><span class="ot">]</span> <span class="ot">??</span> <span class="st">&#39;nobody&#39;</span><span class="ot">;</span></code>
로그인 후 복사

4.<=> 比较运算符

就是看两个表达式值的大小,三种关系: = 返回0、< 返回-1、 > 返回 1

<code class="sourceCode php"><span class="kw"><?php</span><span class="co">// Integers</span><span class="fu">echo</span> <span class="dv">1</span> <=> <span class="dv">1</span><span class="ot">;</span> <span class="co">// 0</span><span class="fu">echo</span> <span class="dv">1</span> <=> <span class="dv">2</span><span class="ot">;</span> <span class="co">// -1</span><span class="fu">echo</span> <span class="dv">2</span> <=> <span class="dv">1</span><span class="ot">;</span> <span class="co">// 1</span><span class="co">// Floats</span><span class="fu">echo</span> <span class="fl">1.5</span> <=> <span class="fl">1.5</span><span class="ot">;</span> <span class="co">// 0</span><span class="fu">echo</span> <span class="fl">1.5</span> <=> <span class="fl">2.5</span><span class="ot">;</span> <span class="co">// -1</span><span class="fu">echo</span> <span class="fl">2.5</span> <=> <span class="fl">1.5</span><span class="ot">;</span> <span class="co">// 1</span> <span class="co">// Strings</span><span class="fu">echo</span> <span class="st">"a"</span> <=> <span class="st">"a"</span><span class="ot">;</span> <span class="co">// 0</span><span class="fu">echo</span> <span class="st">"a"</span> <=> <span class="st">"b"</span><span class="ot">;</span> <span class="co">// -1</span><span class="fu">echo</span> <span class="st">"b"</span> <=> <span class="st">"a"</span><span class="ot">;</span> <span class="co">// 1</span></code>
로그인 후 복사

5.define支持定义数组类型的值

php 5.6已经支持CONST 语法定义数组类的常量,PHP7中支持define语法。

<code class="sourceCode php"><span class="kw"><?php</span><span class="fu">define</span><span class="ot">(</span><span class="st">&#39;ANIMALS&#39;</span><span class="ot">,</span> <span class="ot">[</span>    <span class="st">&#39;dog&#39;</span><span class="ot">,</span>    <span class="st">&#39;cat&#39;</span><span class="ot">,</span>    <span class="st">&#39;bird&#39;</span><span class="ot">]);</span><span class="fu">echo</span> <span class="kw">ANIMALS</span><span class="ot">[</span><span class="dv">1</span><span class="ot">];</span> <span class="co">// outputs "cat"</span></code>
로그인 후 복사

6.匿名类

<code class="sourceCode php"><span class="kw"><?php</span><span class="kw">interface</span> Logger {    <span class="kw">public</span> <span class="kw">function</span> <span class="fu">log</span><span class="ot">(</span>string <span class="kw">$msg</span><span class="ot">);</span>}<span class="kw">class</span> Application {    <span class="kw">private</span> <span class="kw">$logger</span><span class="ot">;</span>    <span class="kw">public</span> <span class="kw">function</span> getLogger<span class="ot">()</span>: Logger {         <span class="kw">return</span> <span class="kw">$this</span>->logger<span class="ot">;</span>    }    <span class="kw">public</span> <span class="kw">function</span> setLogger<span class="ot">(</span>Logger <span class="kw">$logger</span><span class="ot">)</span> {         <span class="kw">$this</span>->logger = <span class="kw">$logger</span><span class="ot">;</span>    }}<span class="kw">$app</span> = <span class="kw">new</span> Application<span class="ot">;</span><span class="kw">$app</span>->setLogger<span class="ot">(</span><span class="kw">new</span> <span class="kw">class</span> <span class="kw">implements</span> Logger {    <span class="kw">public</span> <span class="kw">function</span> <span class="fu">log</span><span class="ot">(</span>string <span class="kw">$msg</span><span class="ot">)</span> {        <span class="fu">echo</span> <span class="kw">$msg</span><span class="ot">;</span>    }}<span class="ot">);</span><span class="fu">var_dump</span><span class="ot">(</span><span class="kw">$app</span>->getLogger<span class="ot">());</span></code>
로그인 후 복사

7.增加了整除函数 intdiv

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿