This article introduces some new grammatical features of PHP8 to you. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Some new syntax features of PHP8
Named parameters
function test($name, $age='18', $sex='男') { echo $name . '-------' . $age . '--------'. $sex; } test('Landy', age: 20, sex: '女'); //Landy-------20--------女
You can alsoSkip parameters
test('Landy', sex: '女'); //Landy-------18--------女
The order of parameters does not need to be fixed
test(age: 30, sex: '女', name: 'tom'); //tom-------30--------女
<?php class Person { public static function test($name, $age) { echo $name.'|'.$age; } }
Person::test(age:100, name:'Landy'); //Landy|100
It can also be like this
function test1($arg1,$arg2, ...$args) { print_r($args); } test1(1,2, name:'Landy', age:11, sex:2); Array ( [name] => Landy [age] => 11 [sex] => 2 )
It is not backward compatible. Functions after PHP8.0 can use named parameters
match expression
$a = 8.0; echo match($a) { 8.0 => '匹配8.0', '8.0' => 'test 8.0', default => '没有匹配值' }; //匹配8.0
can match the expression
function test3() { return 8.0; } $a = 8.0; echo match($a) { test3() => '匹配函数', 8.0 => '匹配8.0', '8.0' => 'test 8.0', 9,10,11 => '多次匹配', //多次匹配 default => '没有匹配值' }; //匹配函数
match is a strong type match. Another thing to note is that before match (){} You should write after the curly braces;
, switch is not used
Properties can be defined directly in the constructor
class Point { public function __construct( public float $x = 1.0, public float $y = 2.0, public float $z = 3.0, ) {} } echo (new Point())->x; // 1
Recommended study: " PHP video tutorial》
The above is the detailed content of Let's talk about some new grammatical features of PHP8. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use