New features in PHP5.3, 5.4, 5.5, and 5.6: "article format to be adjusted 」

Source: Internet
Author: User
New features in PHP5.3, 5.4, 5.5, and 5.6: "article format to be adjusted" PHP 5.6 1. constants can be defined using expressions

Https://php.net/manual/zh/migration56.new-features.php

In the previous PHP version, static values must be used to define constants, declare attributes, and specify the default values of function parameters. Now you can use numeric expressions including numeric values, string literal values, and other constants to define constants, declare attributes, and set default values of function parameters.

 F (). "\ n"; echo C: SENTENCE;?>

You can use the const keyword to define constants of the array type.

 
2. use the... operator to define the variable length parameter function

Currently, variable-length parameter functions can be implemented using the... operator without relying on func_get_args.

 1 [1] => 2 [2] => 3)?>
3. use ** for power calculation

Add the right join operator ** to perform power operations. You can also use the abbreviated ** = operator to perform power operations and assign values.

printf(2 ** 3); // 8$a = 2;$a **= 3;printf($a);  // 8
4. use function and use const

The use operator can import external functions and constants into the class. The corresponding structure is use function and use const.

 
5. add the hash_equals () function to compare strings with constant time consumption to avoid time series attacks.
 
6. add _ debugInfo ()

When var_dump () is used to output an object, the attribute and value to be output can be controlled.

 prop = $val;    }    public function __debugInfo() {        return $this->prop;    }}var_dump(new C(42));?>
PHP 5.5 1. new Generators

The yield keyword is used to return values one by one when the function needs to return an iterator.

function number10(){    for($i = 1; $i <= 10; $i += 1)        yield $i;}

The return value of this function is an array:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2. added the finally keyword.

Finally processing process:

3. foreach supports list ()

Foreach supports separating nested arrays from independent variables through list.

 
4. empty () supports inputting an arbitrary expression, not just a variable
 2135, 'name' => 'John'), array ('id' => 3245, 'name' => 'Smith '), array ('id' => 5342, 'name' => 'Peter '); // Retrieve the name column from the result set $ names = array_column ($ records, 'name'); print_r ($ names ); // Retrieve the name column from the result set and use the corresponding id as the key value $ names = array_column ($ records, 'name', 'id'); print_r ($ names ); "# PHP 5.4 #1. added Traitshttps: // php.net/manual/zh/language.oop5.traits.php" // Traits cannot be instantiated independently and can only be included by trait SayWorld {public safunction sayHe Llo () {echo 'World! ';}} Class MyHelloWorld {// include the members in SayWorld in use SayWorld;} $ xxoo = new MyHelloWorld (); // sayHello () the function is from the SayWorld component $ xxoo-> sayHello (); "## 2. new short array syntax" // original array syntax $ arr = array ("key" => "value", "key2" => "value2 "); $ arr = array (1, 2, 4); // abbreviated form $ arr = ["key" => "value", "key2" => "value2"]; $ arr = [1, 2, 3, 4]; "#3. added support for accessing the array members returned by the function." print func () [0]; "## 4. regardless of php. whether to set short_open_tag in ini,
 The format is always available. This Short form is called the Short Open Tag, which is enabled by default in PHP5.3 and always available in php5.4. It is very convenient to embed PHP variables in HTML using this short form. #5. built-in CLI mode for development web server

// Start the Web server
Php-S localhost: 8000
// Specify the root directory at startup
Php-S localhost: 8000-t/home/me/public_html/foo
// Use the routing script
Php-S localhost: 8000 index. php // all requests are processed by index. php.


#6. added the alias Class member during instantiation.

(New Foo)-> bar ();


#7. added dynamic access to static methods

$ Func = "funcXXOO ";
A: {$ func }();


#8. the closure supports $ this #9. the binary direct quantity is added.

$ Bin = bindec (110011); // you need to write it like this before.
$ Bin = 0b110011;
Echo $ bin; // 51


#10. the upload progress of the current file can be obtained through "$ _ session [" upload_progress_name, with Ajax, you can easily implement the upload progress bar. #11. mysqlnd is used by default. now mysql, mysqli, and pdo_mysql use the mysqlnd local library by default. before PHP5.4, you need :". /configure -- with-mysqli = mysqlnd "now :". /configure -- with-mysqli "#12. make json better understand Chinese

Echo json_encode ("Chinese", JSON_UNESCAPED_UNICODE );
// "Chinese"


#13, default_charset from the ISO-8859-1 has been changed to the UTF-8 by default to send "Content-Type: text/html; charset = utf-8" # PHP 5.3 #1, support for namespace https://php.net/manual/zh/language.namespaces.php

#2. add static binding shards later. But there is a problem. if we call it in the subclass, the result will be the parent class. Because when inheriting the parent class, the static member has been bound.

Class
{
Static public function callFuncXXOO ()
{
Print self: funcXXOO ();
}

static public function funcXXOO(){    return "A::funcXXOO()";}

}

Class B extends
{
Static public function funcXXOO ()
{
Return "B: funcXXOO ";
}
}

$ B = new B;
$ B-> callFuncXXOO ();


The output is:

A: funcXXOO ()


A static keyword is added to PHP 5.3.0 to reference the current class, that is, delayed static binding is implemented:

Class
{
Static public function callFuncXXOO ()
{
Print static: funcXXOO ();
}

// ...

}

//...


The output will be as expected:

B: funcXXOO


#3. the addition of the goto operator https://php.net/manual/zh/control-structures.goto.phpgoto statement may cause the program flow to be unclear, the readability is reduced, but in some cases it has its unique convenience, for example, the loop with deep nesting interruption and the if statement.

// The above output is 2


#4. added native closures (Lambda/anonymous functions) to support https://php.net/manual/zh/functions.anonymous.php?5=add two magic methods. _ callStatic and _ invokehttps: // Callback () will be called. When you try to call an object by calling a function, the __invoke () method is automatically called.

Class
{
Public function _ invoke ($ str)
{
Print "A ::__ invoke (): {$ str }";
}
}

$ A = new;
$ A ("Hello World ");


The output is:

A: :__ invoke (): Hello World


#6. add Nowdoc syntax to support 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;


Just like the heredoc structure is similar to the double quotation mark string, and the Nowdoc structure is similar to the single quotation mark string. The Nowdoc structure is similar to the heredoc structure, but no parsing is performed in nowdoc. #7. in the Heredoc structure, double quotation marks can be used to declare the identifier. Https://php.net/manual/zh/language.types.string.php#language.types.string.syntax.heredoc


#8. the const keyword can be used to define constants outside the class definition. Https://php.net/manual/zh/language.constants.syntax.php

Define ("CONSTANT_A", "Hello world ");
Const CONSTANT_ B = 'Hello World ';


The const format is only applicable to constants and does not apply to expressions that can be evaluated at runtime:

// Correct
Const XXOO = 1234;
// Error
Const XXOO = 2*617;

Unlike the definition of constants using define (), the definition of constants using the const keyword must be at the top of the scope, because this method is defined during compilation. That is, you cannot use const to define constants in the function, in the loop, or in the if statement. #9. you can omit the partial expression expr1? : Expr3. if expr1 is TRUE, expr1 is returned. otherwise, expr3 is returned. #10. exceptions can be nested

#11. dynamic access to static variables
Output during running:

123
"

12. The mail () function supports logging and sending logs.

In the configuration file php. ini, you can set the log path. Parameter name: mail. log

References:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.