指尖上的记忆指尖上的记忆
首页
  • 基础
  • Laravel框架
  • Symfony框架
  • 基础
  • Gin框架
  • 基础
  • Spring框架
  • 命令
  • Nginx
  • Ai
  • Deploy
  • Docker
  • K8s
  • Micro
  • RabbitMQ
  • Mysql
  • PostgreSsql
  • Redis
  • MongoDb
  • Html
  • Js
  • 前端
  • 后端
  • Git
  • 知识扫盲
  • Golang
🌟 gitHub
首页
  • 基础
  • Laravel框架
  • Symfony框架
  • 基础
  • Gin框架
  • 基础
  • Spring框架
  • 命令
  • Nginx
  • Ai
  • Deploy
  • Docker
  • K8s
  • Micro
  • RabbitMQ
  • Mysql
  • PostgreSsql
  • Redis
  • MongoDb
  • Html
  • Js
  • 前端
  • 后端
  • Git
  • 知识扫盲
  • Golang
🌟 gitHub

概念: match 表达式基于值的一致性进行分支计算。 match表达式和 switch 语句类似, 都有一个表达式主体,可以和多个可选项进行比较。 与 switch 不同点是,它会像三元表达式一样求值。 与 switch 另一个不同点,它的比较是严格比较( ===)而不是松散比较(==)。 Match 表达式从 PHP 8.0.0 起可用。

语法:

<?php
$return_value = match (subject_expression) {
    single_conditional_expression => return_expression,
    conditional_expression1, conditional_expression2 => return_expression,
};
?>

使用:

//php switch 替换为 match:
return match ($type) {
            'course' => $this->_em->getRepository(UserCourse::class)->getUserCourseList($params),
            'certification' => $this->_em->getRepository(UserCourse::class)->getUserCertificationList(),
            'favorite' => $this->_em->getRepository(UserFavorite::class)->getUserFavoriteList(),
            default => [],
        };


switch ($type) {
            case 'course':
                return $this->_em->getRepository(UserCourse::class)->getUserCourseList($params);
            case 'certification':
                return $this->_em->getRepository(UserCourse::class)->getUserCertificationList();
            case 'favorite':
                return $this->_em->getRepository(UserFavorite::class)->getUserFavoriteList();
        }