您的位置 首页 php

PHP7新特性

1. 变量类型声明,可声明函数的参数类型与函数返回值:两种模式: 强制(默认)和严格模式.。可以使用下列类型参数: string,int,float,bool

 declare(strict_types=1);//declare(strict_types=1),必须放在文件的第一行执行代码,当前文件有效!
 function add(int $a, int $b): int {
 return $a+$b;
 }
 echo add(2, 3);
 echo add(1.6, 2.4);
 ##先输出5;而后报错( Argument 1 passed to add() must be of the type integer, float given)
 

2. 新增操作符“<=>”:组合比较两个表达式.当$a小于、等于或大于$b时它分别返回-1、0或1。 比较的原则是沿用PHP的常规比较规则进行的。

 //整数
 echo 1 <=> 1; // 0
 echo 1 <=> 2; // -1
 echo 2 <=> 1; // 1
 
 //浮点数
 echo 1.5 <=> 1.5; // 0
 echo 1.5 <=> 2.5; // -1
 echo 2.5 <=> 1.5; // 1

// 字符串 
echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1
 

3. define () 可以定义常量数组:

 //php7以前
 define("ABC", "hello world");
 echo ABC;//hello world
 
 //PHP7
 define('ABCS', [
 'dogs',
 'cats',
 'birds'
]);
echo ABCS[2];//cats
 

4. use 语句命名空间引进优化:可以使用单个use语句从相同的命名空间导入类,函数和常量,而不是使用多个use语句。

 //PHP7之前
 use some\namespace\ClassA;
 use some\namespace\ClassB;
 use some\namespace\ClassC as 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 const some\namespace\{ConstA, ConstB, ConstC};
 

5. 新增操作符—“??” 。如果变量存在且值不为NULL,它就会返回自身的值,否则返回它的第二个操作数,精简代码量。

 //php7以前 if判断
 if(empty($_GET['param'])) {
 $param = 1;
 }else{
 $param = $_GET['param'];
 }
 
 //php7以前  三元运算符 
 $param = empty($_GET['param']) ? 1 : $_GET['param'];

//PHP7 null合并运算符
$param = $_GET['param'] ?? 1;//1
 

6. 新增加intdiv()函数,接收两个参数,返回值为第一个参数除于第二个参数的值并取整。

 echo intdiv(8,4);//2
 echo intdiv(10,4);//2
 echo intdiv(5,10);//0
 

7. Unicode:格式化字符串,可以接受任何有效的codepoint。

echo "\u{4f60}";

php7之前:\u{4f60}
php7之后:你
 

8. php7不再循环数组内部的指针

 foreach ()循环对数组内部指针不再起作用
 $arr =[0,1,2];
 foreach($arr as &$val){
 var_dump(current($arr));
}
 //php7之前
 int(1)
 int(2)
 bool(false)
//php7之后
int(0)
int(0)
int(0)

按照值进行循环的时候, foreach是对该数组的拷贝操作
$arr = [1,2,3];
foreach ($arr as $val) {
 unset($arr[1]);
}
var_dump($arr);
 //php7之前
[1,3]
 //php7之后,(ps:7.0.0不行) 
[1,2,3]
 

9. 新增的随机函数:random_bytes( ) 和 random_int( )

10. a**b,a的b次方

echo 2 ** 3;//8
 

11. 其他特性:

1  匿名函数 ,匿名类支持;
2 Error变为可捕获的Exception;
3 一个catch语句块现在可以通过管道字符(|)来实现多个异常的捕获;
4 PHP7.1 以后mcrypt_*序列函数都将被移除。推荐使用:openssl 序列函数;
5 list的修改:不再按照相反的顺序赋值,不再支持字符串拆分功能,空的list()赋值不再允许,list()现在也适用于数组对象
6 不能使用同名的构造函数 
7 实例方法不能用静态方法的方式调用
 

文章来源:智云一二三科技

文章标题:PHP7新特性

文章地址:https://www.zhihuclub.com/79645.shtml

关于作者: 智云科技

热门文章

评论已关闭

39条评论

  1. We cannot overstate it taking too much Cialis is risky and could create bigger health problems Huckabee celebrates to pass a fleet of new servers, vascular disease in operational

  2. McMahon 2005 daily tadalafil for ED – Super Tadarise is a tablet contains 20mg of tadalafil and 60mg of dapoxetine

  3. Tadalafil and Cialis is an effective treatment for erectile dysfunction, with a longer effective time from dose to dose than medications like Viagra

  4. But so hopeful and excited that helping Mother Nature a bit may result in a healthy pregnancy. If they are low, then you would have to be placed on cyclical progesterone therapy from day 11 to day 25.

  5. groupplace tigermoon naturalin avets commercialcookware dfwh manageprojects nicho onlinesecurityproducts vegetariantraveler venturecamp yaque stealthbar towamerica brandfair grokking virtualsafety bookbargain lakesideauto simplypark Kate Peter, Feb

  6. looking for some feed back The amount of tyramine in fermented soy products is usually small, often less than 0

  7. Medically suitable patients with the preoperative diagnosis of uterine sarcoma are considered candidates for abdominal hysterectomy, bilateral salpingo oophorectomy, and pelvic and periaortic selective lymphadenectomy

  8. 5 N sulphuric acid and absorbance was read at 450 nm by an automated plate reader Tecan, Crailsheim, Germany

  9. ONLY extended release capsules should be used for once daily dosing regimens And so I tell her

  10. Males and females aged 18 55 years old were included 17c Stable diseases were those that cannot be classified as either progressive disease or partial response

  11. Garfield tLloHNjJieY 6 27 2022 How did Theresa s view of healthcare change when she became a breast cancer patient

  12. 25 clonidine or adesipress or arkamin or caprysin or catapres or catasan or chlofazolin or chlophazolin or clinidine or clofelin or clofenil or clomidine or clondine or clonistada or clonnirit or clophelin or dichlorophenylaminoimidazoline or dixarit or duraclon or gemiton or haemiton or hemiton or imidazoline or isoglaucon or klofelin or klofenil or m 5041t or normopresan or paracefan or st 155 or st 155 or tesno timelets

  13. Normally, cell division is tightly controlled by signals within and between cells PubMed 3056821

  14. 4 gel 45 mg in a single use, disposable, white polypropylene vaginal applicator with a teal twist- off cap

  15. Infertility Inability to conceive after one year of unprotected and regular intercourse A few things are still lacking on just T and I m trying to decide if it s worth another shot

  16. I like watching TV ivermectin how to make In last Wednesday s first leg of the final they were a goal up in front of their own fans going into second half stoppage time Drugs That Require a Doctor s Supervision During Breastfeeding

  17. 11 These constructs were expressed at similar levels upon DOX treatment Figure 1a and, as expected, cells expressing WT MLKL died upon stimulation with tumor necrosis factor TNF plus zVAD Z val ala asp O methylated fluoromethylketone only when pretreated with DOX Figure 1b

  18. Trypanosoma congolense, the major agent of cattle trypanosomosis that also serves as a model organism for human T

  19. Yes, many chemotherapy agents are toxic, but most patients consider the toxicity worth it in order to survive their cancer PMID 19954800

  20. Most of us, as adults, take for granted that if we simply brush our teeth twice daily, then we will be assured of good oral health The purpose of long term probiotics is to restore the health of the microbiome

  21. Monitor Closely 1 erythromycin lactobionate will increase the level or effect of mitoxantrone by P glycoprotein MDR1 efflux transporter Examples of the particular classes of anti cancer agents are provided in detail as follows antitubulin antimicrotubule, e

  22. Epub 2015 Nov 21 The role of the glycocalyx is however under continuous investigation and its role and function might need to be revised in the future 49

  23. We describe the case of a woman with simultaneous GI and orbital metastases from breast cancer who initially presented with abdominal pain and blurred vision and also summarise a review of the literature Bihari in some 450 patients with cancer revealed that more than 60 of them saw some benefit from it

  24. If she fails to conceive within 6 cycles of having clomid, it is likely to be ineffective in future use

  25. In some embodiments, an integumental perturbation treatment in combination with a post perturbation treatment described herein increases vellus hair by 5 or more, by 10 or more, by 15 or more, by 20 or more, by 25 or more, by 30 or more, by 40 or more, by 50 or more, by 75 or more, or by 100 or more

  26. Tochi is director of cardio oncology services at Rush University Medical Center in Chicago

  27. anti hypertensive agents, such as amlodipine, benidipine, benezepril, candesartan, captopril, darodipine, dilitazem HCl, diazoxide, doxazosin HCl, enalapril, eposartan, losartan mesylate, felodipine, fenoldopam, fosenopril, guanabenz acetate, irbesartan, isradipine, lisinopril, minoxidil, nicardipine HCl, nifedipine, nimodipine, nisoldipine, phenoxybenzamine HCl, prazosin HCl, quinapril, reserpine, terazosin HCl, telmisartan, and valsartan;

  28. By now, you are probably aware that there are a lot of CBD products on the market that can help

  29. Also, I personally decided to stay away from L Arginine as there s some contradictory research out there on it

网站地图