1、try catch 支持捕获多个异常
#写代码的时候不免要自定义异常,以前捕获多个异常需要这样写
try{
...
}catch(myException $e) {
...
}catch(anotherExcpetion $e) {
...
}catch(Exception $e) {
...
}
php7.1可以这么写了
try{
...
}catch(myException | anotherException $e) {
...
}catch(Exception $e) {
...
}
- #写代码的时候不免要自定义异常,以前捕获多个异常需要这样写
- try{
- ...
- }catch(myException $e) {
- ...
- }catch(anotherExcpetion $e) {
- ...
- }catch(Exception $e) {
- ...
- }
- #php7.1可以这么写了
- try{
- ...
- }catch(myException | anotherException $e) {
- ...
- }catch(Exception $e) {
- ...
- }
#写代码的时候不免要自定义异常,以前捕获多个异常需要这样写
try{
...
}catch(myException $e) {
...
}catch(anotherExcpetion $e) {
...
}catch(Exception $e) {
...
}
php7.1可以这么写了
try{
...
}catch(myException | anotherException $e) {
...
}catch(Exception $e) {
...
}
1、class 支持常量的可见性设置
#以前的类里面的常量不可以设置可见性,例如
class MyClass
{
protected $orderArr;
private $key;
const OUT_TIME = 1000; #这里不能添加修饰
}
现在你可以为const设置可见性了
class MyClass
{
public $name
protecte $orderArr;
private $key;
protected const OUT_TIME = 1000; #这里可以设置可见性了
}
- #以前的类里面的常量不可以设置可见性,例如
- class MyClass
- {
- protected $orderArr;
- private $key;
- const OUT_TIME = 1000; #这里不能添加修饰
- }
- #现在你可以为const设置可见性了
- class MyClass
- {
- public $name
- protecte $orderArr;
- private $key;
- protected const OUT_TIME = 1000; #这里可以设置可见性了
- }
#以前的类里面的常量不可以设置可见性,例如
class MyClass
{
protected $orderArr;
private $key;
const OUT_TIME = 1000; #这里不能添加修饰
}
现在你可以为const设置可见性了
class MyClass
{
public $name
protecte $orderArr;
private $key;
protected const OUT_TIME = 1000; #这里可以设置可见性了
}
3、多变量同时赋值
#以前多个变量赋值你可以这样写
$a = 1;
$b = 2;
$c = 4;
#或者相同值的你可以这样写
$a = $b = $c = [];
现在你可以这样为不同值的多个变量赋值
[$a, $b, \(c] = [1, 2, 3];</pre><div class="EnlighterJSWrapper atomicEnlighterJSWrapper"><ol class="hoverEnabled atomicEnlighterJS EnlighterJS"><li class=" odd"><span class="co1">#以前多个变量赋值你可以这样写</span><span class=""></span></li><li class=" even"><span class="">\)a = 1;
\(b = </span><span class="nu0">2</span><span class="">;</span></li><li class=" even"><span class="">\)c = 4;#或者相同值的你可以这样写 $a = $b = \(c = </span><span class="br0">[</span><span class="br0">]</span><span class="">;</span></li><li class=" odd"><span class=""></span></li><li class=" even"><span class=""></span></li><li class=" odd"><span class=""></span><span class="co1">#现在你可以这样为不同值的多个变量赋值</span><span class=""></span></li><li class=" even"><span class=""></span></li><li class=" odd"><span class=""></span><span class="br0">[</span><span class="">\)a, $b, \(c</span><span class="br0">]</span><span class=""> = </span><span class="br0">[</span><span class="nu0">1</span><span class="">, </span><span class="nu0">2</span><span class="">, </span><span class="nu0">3</span><span class="br0">]</span><span class="">;</span></li></ol><pre style="display: none;">#以前多个变量赋值你可以这样写
\)a = 1;
\(b = 2;
\)c = 4;
或者相同值的你可以这样写
$a = $b = $c = [];
现在你可以这样为不同值的多个变量赋值
[$a, $b, $c] = [1, 2, 3];
4、放弃mcrypt扩展
#以前用mcrypt写一个加密大概是这样
public function encryptData($str, $key)
{
$block = mcrypt_get_block_size('des', 'ecb');
$pad = \(block - (strlen(\)str) % $block);
\(str .= str_repeat(chr(\)pad), $pad);
$res = mcrypt_encrypt(MCRYPT_DES, $key, \(str, MCRYPT_MODE_ECB);
return strtoupper(bin2hex(\)res));
}
现在你需要用openssl来替换了
public function encryptData($str, $key)
{
\(res = openssl_encrypt(\)str, 'DES-ECB', \(key, OPENSSL_RAW_DATA);
return strtoupper(bin2hex(\)res));
}
以前用mcrypt写一个解密大概是这样
public function decryptData($str, $key)
{
\(str = strtolower(\)str);
$str = pack('H*', $str);
$decrypted = mcrypt_decrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB);
\(dec_s = strlen(\)decrypted);
\(padding = ord(\)decrypted[$dec_s - 1]);
\(decrypted = substr(\)decrypted, 0, -$padding);
return $decrypted;
}
现在也要用openssl替换了
public function decryptData($str, $key)
{
\(str = strtolower(\)str);
$str = pack('H*', $str);
\(decrypted = openssl_decrypt(\)str,'DES-ECB', $key,OPENSSL_RAW_DATA);
return $decrypted;
}
- #以前用mcrypt写一个加密大概是这样
- public function encryptData($str, $key)
- {
- $block = mcrypt_get_block_size('des', 'ecb');
- $pad = \(block - </span><span class="br0">(</span><span class="kw1">strlen</span><span class="br0">(</span><span class="">\)str) % $block);
- \(str .= </span><span class="kw1">str_repeat</span><span class="br0">(</span><span class="kw1">chr</span><span class="br0">(</span><span class="">\)pad), $pad);
- $res = mcrypt_encrypt(MCRYPT_DES, $key, \(str, MCRYPT_MODE_ECB</span><span class="br0">)</span><span class="">;</span></li><li class=" odd"><span class=""> return </span><span class="kw1">strtoupper</span><span class="br0">(</span><span class="kw1">bin2hex</span><span class="br0">(</span><span class="">\)res));
- }
- #现在你需要用openssl来替换了
- public function encryptData($str, $key)
- {
- \(res = </span><span class="kw1">openssl_encrypt</span><span class="br0">(</span><span class="">\)str, 'DES-ECB', \(key, OPENSSL_RAW_DATA</span><span class="br0">)</span><span class="">;</span></li><li class=" odd"><span class=""> return </span><span class="kw1">strtoupper</span><span class="br0">(</span><span class="kw1">bin2hex</span><span class="br0">(</span><span class="">\)res));
- }
- #以前用mcrypt写一个解密大概是这样
- public function decryptData($str, $key)
- {
- \(str = </span><span class="kw1">strtolower</span><span class="br0">(</span><span class="">\)str);
- $str = pack('H', $str);
- $decrypted = mcrypt_decrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB);
- \(dec_s = </span><span class="kw1">strlen</span><span class="br0">(</span><span class="">\)decrypted);
- \(padding = </span><span class="kw1">ord</span><span class="br0">(</span><span class="">\)decrypted[$dec_s - 1]);
- \(decrypted = </span><span class="kw1">substr</span><span class="br0">(</span><span class="">\)decrypted, 0, -$padding);
- return \(decrypted;</span></li><li class=" even"><span class=""></span><span class="br0">}</span><span class=""></span></li><li class=" odd"><span class=""></span></li><li class=" even"><span class=""></span></li><li class=" odd"><span class=""></span><span class="co1">#现在也要用openssl替换了</span><span class=""></span></li><li class=" even"><span class=""></span></li><li class=" odd"><span class="">public function </span><span class="kw1">decryptData</span><span class="br0">(</span><span class="">\)str, $key)
- {
- \(str = </span><span class="kw1">strtolower</span><span class="br0">(</span><span class="">\)str);
- $str = pack('H', $str);
- \(decrypted = </span><span class="kw1">openssl_decrypt</span><span class="br0">(</span><span class="">\)str,'DES-ECB', $key,OPENSSL_RAW_DATA);
- return $decrypted;
- }
#以前用mcrypt写一个加密大概是这样
public function encryptData($str, $key)
{
$block = mcrypt_get_block_size('des', 'ecb');
$pad = \(block - (strlen(\)str) % $block);
\(str .= str_repeat(chr(\)pad), $pad);
$res = mcrypt_encrypt(MCRYPT_DES, $key, \(str, MCRYPT_MODE_ECB);
return strtoupper(bin2hex(\)res));
}
现在你需要用openssl来替换了
public function encryptData($str, $key)
{
\(res = openssl_encrypt(\)str, 'DES-ECB', \(key, OPENSSL_RAW_DATA);
return strtoupper(bin2hex(\)res));
}
以前用mcrypt写一个解密大概是这样
public function decryptData($str, $key)
{
\(str = strtolower(\)str);
$str = pack('H*', $str);
$decrypted = mcrypt_decrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB);
\(dec_s = strlen(\)decrypted);
\(padding = ord(\)decrypted[$dec_s - 1]);
\(decrypted = substr(\)decrypted, 0, -$padding);
return $decrypted;
}
现在也要用openssl替换了
public function decryptData($str, $key)
{
\(str = strtolower(\)str);
$str = pack('H*', $str);
\(decrypted = openssl_decrypt(\)str,'DES-ECB', $key,OPENSSL_RAW_DATA);
return $decrypted;
}
5、算术运算中警告无效的字符串
# 以前可以这样
$num = 1 + 'abc323vccc'; //result 1;
$num = 1 +'100abc'; //result 101;
现在你不能那样写了,将会得到这样的一个警告
PHP warning: A non-numeric value encountered on line 1
- # 以前可以这样
- \(num = </span><span class="nu0">1</span><span class=""> + </span><span class="st0">'abc323vccc'</span><span class="">; </span><span class="co1"> //result 1;</span><span class=""></span></li><li class=" odd"><span class="">\)num = 1 +'100abc'; //result 101;
- #现在你不能那样写了,将会得到这样的一个警告
- PHP warning: A non-numeric value encountered on line 1
# 以前可以这样
\(num = 1 + 'abc323vccc'; //result 1;
\)num = 1 +'100abc'; //result 101;
现在你不能那样写了,将会得到这样的一个警告
PHP warning: A non-numeric value encountered on line 1
</div>
标签:decrypted,key,...,几个,特性,PHP7.1,str,catch,mcrypt
来源: https://www.cnblogs.com/samRoot/p/14207987.html