New features in PHP 5.3, 5.4, 5.5, 5.6 "article format to be adjusted"

Source: Internet
Author: User
Tags crypt traits
PHP 5.6

1. You can define constants by using an expression

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

In previous versions of PHP, you had to use static values to define constants, declare properties, and specify default values for function parameters. You can now use numeric expressions, including numeric values, string literals, and other constants, to define constants, declare properties, and set default values for function parameters.

 
  F (). " \ n "; Echo c::sentence;? >

You can define a constant of type array by using the Const keyword.

 
  

2. Use ... operator defines variable-length parameter functions

Now you can not rely on Func_get_args (), using ... operator to implement variable-length parametric functions.

 
  1    [1] = 2    [2] = 3)?>

3. Use * * for power operation

Add the RIGHT Join operator * * to power the operation. It also supports the shorthand **= operator, which is a power operation and assigns a value.

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 in the class. The corresponding structure is the use function and the use Const.

 
  

5. Join the Hash_equals () function to perform string comparisons with constant time consumption to avoid sequential attacks

 
  

6. Join __debuginfo ()

When using the Var_dump () output object, you can control which properties and values you want to output.

 
  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 at a time when the function needs to return an iterator.

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

The return value of the function is an array:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

2. New finally keyword

Finally processing process:

3. Foreach Support list ()

foreach supports the separation of nested arrays into separate variables through list ().

 
  

4, empty () supports passing in an arbitrary expression, not just a variable

 2135, ' name ' = ' John '), array (' id ' = = 3245, ' name ' = = ' Smith '), array (' id ' = = 5342, ' name ' = ' Peter ') ;//Remove the Name column from the result set $names = Array_column ($records, ' name ');p Rint_r ($names);//always remove the Name column from the result set, with the corresponding ID as the key value $names = Array_col Umn ($records, ' name ', ' id ');p rint_r ($names), ' #PHP 5.4# #1, New traitshttps://php.net/manual/zh/ language.oop5.traits.php '//traits cannot be instantiated separately, only the trait sayworld{public Function SayHello () {echo ' world!) contained in the class    '; }}class myhelloworld{//include members of Sayworld in use Sayworld;} $xxoo = new Myhelloworld ();//SayHello () function is $xxoo->sayhello () from Sayworld component, "# #2, new short array syntax '//original array notation $arr = Array ( "Key" = "value", "key2" = "value2"), $arr = Array (1,2,3,4);//Shorthand form $arr = ["Key" = "value", "key2" = "value2" "]; $arr = [1,2,3,4]; ' # #3, added support for member access to function return array ' print func () [0]; ' # #4, regardless of whether Short_open_tag is set in PHP.ini,
 The format is always available. This shorthand form, known as the short open Tag, is turned on by default at PHP5.3 and is always available at PHP5.4. It is very convenient to embed PHP variables in HTML in this shorthand form. # #5, built-in CLI-mode Web server for development

Start the Web server
Php-s localhost:8000
At startup, specify the root directory
Php-s localhost:8000-t/home/me/public_html/foo
Using Routing (Router) scripts
Php-s localhost:8000 index.php//All requests will be handled by index.php.


# #6, new access to class members when instantiated

(New Foo)->bar ();


# #7, new ways to dynamically access static methods

$func = "Funcxxoo";
a::{$func} ();


# #8, Closures support $this # #9, add binary Direct volume

$bin = Bindec (110011); I need to write this before.
$bin = 0b110011;
Echo $bin; 51


# #10, SESSION provides upload progress support through the "$_session[" Upload_progress_name "" "to get the current file upload progress information, combined with Ajax can easily implement the upload progress bar. # #11, default using Mysqlnd now MySQL, mysqli, pdo_mysql default to use Mysqlnd local library, before PHP5.4 need: "./configure--with-mysqli=mysqlnd" "Now:" './configure--with-mysqli ' # #12, let JSON understand Chinese

Echo Json_encode ("Chinese", Json_unescaped_unicode);
Chinese


# #13, Default_charset from Iso-8859-1 has changed to UTF-8 default send "content-type:text/html; Charset=utf-8 "#PHP 5.3# #1, supporting namespaces https://php.net/manual/zh/language.namespaces.php

#2, add late static bindings https://php.net/manual/zh/language.oop5.late-static-bindings.php in PHP, we can pass the Self keyword in the class or __class__ To determine or invoke the current class. But there is a problem, if we are called in the subclass, the resulting result will be the parent class. Since the parent class is inherited, the static member is already bound.

Class A
{
static public Function Callfuncxxoo ()
{
Print Self::funcxxoo ();
}

static public Function Funcxxoo () {    return ' a::funcxxoo () ';}

}

Class B extends A
{
static public Function Funcxxoo ()
{
return "B::funcxxoo";
}
}

$b = new B;
$b->callfuncxxoo ();


The output is:

A::funcxxoo ()


PHP 5.3.0 adds a static keyword to refer to the current class, which implements deferred static binding:

Class A
{
static public Function Callfuncxxoo ()
{
Print Static::funcxxoo ();
}

// ...

}

// ...


This will output as expected:

B::funcxxoo


The above run time will output 2


#4, added native closures (lambda/anonymous functions) support https://php.net/manual/zh/functions.anonymous.php#5, new two magic methods, __callstatic and __ invokehttps://php.net/manual/zh/language.oop5.magic.php __callstatic () is called when a non-accessible method is called in a static manner. The __invoke () method is called automatically when an attempt is made to invoke an object in a way that invokes a function.

Class A
{
Public Function __invoke ($STR)
{
Print "A::__invoke (): {$str}";
}
}

$a = new A;
$a ("Hello world");


The output is:

A::__invoke (): Hello World


#6, add Nowdoc syntax 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 as the heredoc structure resembles a double-quote string, the Nowdoc structure is similar to a single-quote string. The NOWDOC structure is much like the heredoc structure, but no parsing is performed in the Nowdoc. Identifiers can be declared in double quotes in the #7 and Heredoc structures. Https://php.net/manual/zh/language.types.string.php#language.types.string.syntax.heredoc


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

Define ("Constant_a", "Hello World");
Const CONSTANT_B = ' Hello world ';


The const form applies only to constants, not to expressions that can be evaluated at run time:

That's right
Const XXOO = 1234;
Error
Const XXOO = 2 * 617;

Unlike using define () to define constants, the Const keyword is used to define constants that must be at the top of the scope, because this method is defined at compile time. That is, you cannot define constants within a function, within loops, and within an if statement with Const. #9, ternary operators can be abbreviated to omit the middle part of the expression Expr1?: Expr3, when EXPR1 is TRUE returns EXPR1, otherwise returns EXPR3. #10, exceptions can be nested

#11, you can dynamically access static variables.
Upper run-time output:

123
```

12. Mail () function supports record sending log.

You can set the log path in configuration file php.ini. Parameter name: Mail.log

Resources:

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.