It has been half a month since php7 was released. I recently had time to learn about the new features of php7. Of course, the biggest feature of this version is its performance. promote. I'm not an expert, so everyone is welcome to point out my mistakes and look forward to communicating together
1. Added group support in the use statement
use FooLibraryBarBaz{ ClassA, ClassB, ClassC, ClassD as Fizbo };
2. Add ?? operator
isset($_GET['mykey']) ? $_GET['mykey'] : ""Cumbersome
$_GET['mykey'] ?: "" When mykey does not exist, an E_NOTICE
$_GET['mykey'] ?? 'defaultvalue' will be reported. It is safe and will not report E_NOTICE
$username = $_GET['user'] ?? 'nobody';
3. The length of 64-bit PHP7 string can exceed 2^31 bytes.
4. Add Closure::call support
Closure::call dynamically binds a closure function to a new object instance and calls and executes the function,
<span> 1</span> <?<span>php </span><span> 2</span> <span>class</span><span> Value { </span><span> 3</span> <span>protected</span> <span>$value</span><span>; </span><span> 4</span> <span> 5</span> <span>public</span> <span>function</span> __construct(<span>$value</span><span>) { </span><span> 6</span> <span>$this</span>->value = <span>$value</span><span>; </span><span> 7</span> <span>} </span><span> 8</span> <span> 9</span> <span>public</span> <span>function</span><span> getValue() { </span><span>10</span> <span>return</span> <span>$this</span>-><span>value; </span><span>11</span> <span>} </span><span>12</span> <span>} </span><span>13</span> <span>14</span> <span>$three</span> = <span>new</span> Value(3<span>); </span><span>15</span> <span>$four</span> = <span>new</span> Value(4<span>); </span><span>16</span> <span>17</span> <span>$closure</span> = <span>function</span> (<span>$delta</span>) { <span>var_dump</span>(<span>$this</span>->getValue() + <span>$delta</span><span>); }; </span><span>18</span> <span>$closure</span>->call(<span>$three</span>, 4<span>); </span><span>19</span> <span>$closure</span>->call(<span>$four</span>, 4<span>); </span><span>20</span> ?> <span>21</span> <span>//</span><span> outputs int(7),int(8)</span>
5. Double-quoted strings and heredocs support the use of u{xxxxx} to declare unicode characters.
6.define support for arrays
<span>1</span> <span>define</span>('ANIMALS', <span>array</span><span>( </span><span>2</span> 'dog', <span>3</span> 'cat', <span>4</span> 'bird' <span>5</span> <span>)); </span><span>6</span> <span>echo</span> ANIMALS[1]; <span>//</span><span> outputs "cat"</span>
7. Add comparison operator<=>
$a <=> $b
If a is equal to b, it is 0
If a is greater than b, it is 1
If If a is less than b, it is -1
8.php global reserved words can be declared for use
<span>1</span> <span>class</span><span> View { </span><span>2</span> <span>public</span> <span>function</span> <span>include</span>(View <span>$view</span><span>) { </span><span>3</span> <span>//</span><span>...</span> <span>4</span> <span> } </span><span>5</span> }
The include keyword can be used like an ordinary string keyword
9. Scalar types (int, float, string, bool) support
Add the declare(strict_types=1) instruction to declare whether strict type checking is required.
When declaring declare(strict_types=1)< in the file header 🎜>
<span>1</span> <<span>php </span><span>2</span> <span>3</span> <span>declare</span>(strict_types=1<span>); </span><span>4</span> <span>function</span> add(<span>float</span> <span>$a</span>, <span>float</span> <span>$b</span>): <span>float</span><span> { </span><span>5</span> <span>return</span> <span>$a</span> + <span>$b</span><span>; </span><span>6</span> <span>} </span><span>7</span> add(1, 2); <span>//</span><span> float(3)</span>
Enable declare(strict_types=1), if the parameter type is not flat Maybe the return type is not float and an error is thrown
10. Add an interface to provide a safe and convenient random number generator for the user layer. RFC: https://wiki.php.net/rfc/easy_userland_csprng (discuss later)
11. Added yield from operator. https://wiki.php.net/rfc/generator-delegation (discuss later)