Example analysis of modifying php7 parameter, shaping and string processing mechanism

Time:2020-10-2

This paper introduces the modification of php7 parameter, shaping and string processing mechanism. For your reference, the details are as follows:

Modification of parameter processing mechanism

1、 Duplicate parameter naming is no longer supported.

Duplicate parameter naming is no longer supported. For example, an error will be reported when the following code is executed:


public function foo($a, $b, $unused, $unused) {
// ... 
}

Editor’s note: no one should use it like this.

2、 Func_ get_ Arg and func_ get_ Args() adjustment

func_ get_ Arg() and func_ get_ The args() methods return the current value of the parameter, not the value when passed in. The current value may be modified


function foo($x) {
   $x++;
   var_dump(func_get_arg(0));
}
foo(1);

The code above prints 2 instead of 1. If you want to print the original value, you can adjust the calling order.

3、 The modified value is also displayed when printing the exception backtracking information.


function foo($x) {  
 $x = 42;
 throw new Exception; 
} 
foo("string"); 

The operation results of php7 are as follows

Stack trace:
#0 file.php(4): foo(42)
#1 {main} 

The operation results of PHP5 are as follows

Stack trace:
#0 file.php(4): foo(‘string’)
#1 {main}

This adjustment does not affect the behavior of the code, but you need to be aware of this change when debugging.

Other functions related to parameters are the same adjustment, such as debug_ backtrace() .

Modification of integer processing mechanism

1、 An invalid octal number will report a compilation error

Invalid octal digits (including numbers greater than 7) will report compilation errors, such as the following code will report errors:

$i = 0781; // 8 is not a valid octal digit!
[click and drag to move]

Older versions of PHP ignore invalid numbers.

2、 The position with negative displacement will produce abnormality


var_dump(1 >> -1); // ArithmeticError: Bit shift by negative number

3、 Left displacement returns 0 if it exceeds the number of digits


var_dump(1 << 64); // int(0)

The running results of the old version of PHP have something to do with the CPU architecture. For example, x86 returns 1.

4、 If the right displacement exceeds, it will return 0 or – 1


var_dump(1 >> 64); // int(0)
var_dump(-1 >> 64); // int(-1)

Modification of string processing mechanism

1、 Strings containing hexadecimal characters are no longer treated as numbers

Strings containing hexadecimal characters are no longer treated as numbers and are no longer treated differently. For example, the following code:


var_dump("0x123" == "291");   // bool(false)   (previously true) 
var_dump(is_numeric("0x123")); // bool(false)   (previously true) 
var_dump("0xe" + "0x1");    // int(0)     (previously 16) 
var_dump(substr("foo", "0x1")); // string(3) "foo" (previously "oo") 
// Notice: A non well formed numeric value encountered

Filter can be used_ The VaR function checks whether a string contains hexadecimal characters or can be converted to an integer


$str = "0xffff"; 
$int = filter_var($str, FILTER_VALIDATE_INT, FILTER_FLAG_ALLOW_HEX); 
if (false === $int) {   
throw new Exception("Invalid integer!"); 
}
var_dump($int); // int(65535)

2、 An error will be reported if there are illegal characters after

Unicode code point escape syntax is added in double quotation mark and heredoc syntax. UTF-8 character must be followed by “{ u {“. If it is a non UTF-8 character, an error will be reported:


$str = "\u{xyz}"; // Fatal error: Invalid UTF-8 codepoint escape sequence

You can avoid this error by escaping the first \.


$str = "\u{xyz}"; // Works fine

If there is no {after “{ U”, then there is no effect:


$str = "\u202e"; // Works fine

PHP IntlChar()

PHP 7 supports internationalization (I18N) and localization (L10N) through Intl extensions. This extension is only the basic packaging of ICU library, and provides similar methods and features as ICU library.

PHP 7 exposes the Unicode character feature in ICU through the new intlchar class. This class itself defines a number of static methods for manipulating Unicode characters in a multi character set.

example


<?php 
printf('%x', IntlChar::CODEPOINT_MAX); 
echo IntlChar::charName('@'); 
var_dump(IntlChar::ispunct('!')); 
?>

The output of the above program is as follows:

10ffff
COMMERCIAL AT
bool(true)

More interested readers about PHP related content can view the special topics of this website: summary of common functions and skills of PHP, summary of PHP string usage, Encyclopedia of PHP array operation skills, introduction course of PHP basic syntax, introduction course of PHP + MySQL database operation and summary of common database operation skills of PHP

I hope this article will help you with PHP programming.