Recommended (free): PHP7
So many good features and good methods, why not use them , I also hope that PHP will get better and better.
Here we organize all the new features of PHP 5.1, PHP5.2, PHP5.3, PHP5.4, PHP5.5, PHP5.6, PHP7, PHP7.1, ready for everyone to learn and use
Buid-in web server has a simple built-in web server
To use the current directory as the Root Document, you only need this command:
php -S localhost:3300
Also Specify other paths
php -S localhost:3300 -t /path/to/root
You can also specify routes
php -S localhost:3300 router.php
Namespace (php5.3)
The delimiter of the namespace is a backslash\
namespace fox\lanmps\Table; class Select {}
Get the complete category name
The functions of namespace alias classes and namespace short versions were introduced in PHP5.3. Although this does not work for string class names
use Some\Deeply\Nested\Namespace\FooBar; // does not work, because this will try to use the global `FooBar` class $reflection = new ReflectionClass('FooBar'); echo FooBar::class;
To solve this problem adopt the new FooBar::class syntax, which returns the full class name of the class
Namespace use operation Symbol starts to support the import of functions and constants
namespace Name\Space { const FOO = 42; function f() { echo __FUNCTION__."\n"; } } namespace { use const Name\Space\FOO; use function Name\Space\f; echo FOO."\n"; f(); }
Output
42
Name\Space\f
Group use declarations
Classes, functions and constants imported from the same namespace can now be imported at once with a single use statement.
//PHP7之前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;// PHP7之后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};
Support delayed static binding
static keyword to reference the current class, which implements delayed static binding
class A { public static function who() { echo __CLASS__; } public static function test() { static::who(); // 这里实现了延迟的静态绑定 } } class B extends A { public static function who() { echo __CLASS__; } } B::test();
Output result:
B
Support goto statement
Most computer programming languages support the unconditional goto statement goto. When the program executes the goto statement, it will turn to the goto statement. Execution continues at the program location indicated by the label. Although the goto statement may lead to unclear program flow and reduced readability, it has its own unique convenience in certain situations, such as interrupting deeply nested loops and if statements.
goto a; echo 'Foo'; a: echo 'Bar'; for($i=0,$j=50; $i<100; $i++) { while($j--) { if($j==17) goto end; } } echo "i = $i"; end: echo 'j hit 17';
Support closures, Lambda/Anonymous functions
The concepts of closure (Closure) functions and Lambda functions come from the field of functional programming. JavaScript, for example, is one of the most common languages that supports closures and lambda functions.
In PHP, we can also create functions while the code is running through create_function(). But there is a problem: the created function is only compiled at runtime and is not compiled into executable code at the same time as other codes. Therefore, we cannot use executable code caches like APC to improve code execution efficiency.
In PHP5.3, we can use Lambda/anonymous functions to define some temporary (use and throw away) functions as callback functions for array_map()/array_walk() and other functions.
echo preg_replace_callback('~-([a-z])~', function ($match) { return strtoupper($match[1]); }, 'hello-world'); // 输出 helloWorld $greet = function($name) { printf("Hello %s\r\n", $name); }; $greet('World'); $greet('PHP'); //...在某个类中 $callback = function ($quantity, $product) use ($tax, &$total) { $pricePerItem = constant(__CLASS__ . "::PRICE_" . strtoupper($product)); $total += ($pricePerItem * $quantity) * ($tax + 1.0); };
Magic methods __callStatic() and __invoke()
There is originally a magic method __call() in PHP. When the code calls an object that is not This magic method will be automatically called when the method exists. The new __callStatic() method is only used for static class methods. When trying to call a static method that does not exist in the class, the __callStatic() magic method will be automatically called.
class MethodTest { public function __call($name, $arguments) { // 参数 $name 大小写敏感 echo "调用对象方法 '$name' " . implode(' -- ', $arguments). "\n"; } /** PHP 5.3.0 以上版本中本类方法有效 */ public static function __callStatic($name, $arguments) { // 参数 $name 大小写敏感 echo "调用静态方法 '$name' " . implode(' -- ', $arguments). "\n"; } } $obj = new MethodTest; $obj->runTest('通过对象调用'); MethodTest::runTest('静态调用'); // As of PHP 5.3.0
The output after the above code is executed is as follows:
Call the object method 'runTest' –- Call the static method 'runTest' through the object call –- Static call
When calling the object in the form of a function, __invoke () method will be called automatically.
class MethodTest { public function __call($name, $arguments) { // 参数 $name 大小写敏感 echo "Calling object method '$name' " . implode(', ', $arguments). "\n"; } /** PHP 5.3.0 以上版本中本类方法有效 */ public static function __callStatic($name, $arguments) { // 参数 $name 大小写敏感 echo "Calling static method '$name' " . implode(', ', $arguments). "\n"; } } $obj = new MethodTest; $obj->runTest('in object context'); MethodTest::runTest('in static context'); // As of PHP 5.3.0
Nowdoc syntax
Usage is similar to Heredoc, but uses single quotes. Heredoc needs to be declared using double quotes.
Nowdoc does not do any variable parsing, which is very suitable for passing a piece of PHP code.
// Nowdoc 单引号 PHP 5.3之后支持 $name = 'MyName'; echo <<<'EOT' My name is "$name". EOT; //上面代码输出 My name is "$name". ((其中变量不被解析) // Heredoc不加引号 echo <<<FOOBAR Hello World! FOOBAR; //或者 双引号 PHP 5.3之后支持 echo <<<"FOOBAR" Hello World! FOOBAR;
Supports initialization of static variables, class members and class constants through Heredoc.
// 静态变量 function foo() { static $bar = <<<LABEL Nothing in here... LABEL; } // 类成员、常量 class foo { const BAR = <<<FOOBAR Constant example FOOBAR; public $baz = <<<FOOBAR Property example FOOBAR; }
Const can also be used to define constants outside the class
//PHP中定义常量通常是用这种方式 define("CONSTANT", "Hello world."); //并且新增了一种常量定义方式 const CONSTANT = 'Hello World';
The ternary operator adds a shortcut writing method
The original format is (expr1) ? (expr2) : (expr3)
If the result of expr1 is True, the result of expr2 is returned.
Add a new writing method, you can omit the middle part and write it as expr1 ?: expr3
If the result of expr1 is True, the result of expr1 is returned
$expr1=1;$expr2=2;//原格式 $expr=$expr1?$expr1:$expr2 //新格式 $expr=$expr1?:$expr2
Output result:
1
1
Null coalescing operator (??)
Simplified judgment
$param = $_GET['param'] ?? 1;
is equivalent to:
$param = isset($_GET['param']) ? $_GET['param'] : 1;
Json understands Chinese better (JSON_UNESCAPED_UNICODE)
echo json_encode("中文", JSON_UNESCAPED_UNICODE); //输出:"中文"
Binary
$bin = 0b1101; echo $bin; //13
Unicode codepoint translation syntax
This is accepted A Unicode codepoint in hexadecimal form and prints out a UTF-8 encoded string surrounded by double quotes or heredoc. Any valid codepoint is accepted, and the leading 0 can be omitted.
echo "\u{9876}"
Old version output: \u{9876}
New version input: Top
Use ** for power operation
Add right join operation Use the symbol * to perform exponentiation operations. It also supports the abbreviated *= operator, which means performing exponentiation and assigning values.
printf("2 ** 3 == %d\n", 2 ** 3);printf("2 ** 3 ** 2 == %d\n", 2 ** 3 ** 2);$a = 2;$a **= 3;printf("a == %d\n", $a);
Output
2 ** 3 == 8
2 * 3 * 2 == 512
a == 8
Spaceship operator (combination comparison operator)
The spaceship operator is used to compare two expressions. when ##a##大At,equal or ##小At
The above is the detailed content of Integrate features from PHP5.X to PHP7.1.x. For more information, please follow other related articles on the PHP Chinese website!