admin 发表于 2023-2-16 18:54:50

match 表达式基于值的一致性进行分支计算

<div id="article_content" class="article_content clearfix">
      <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
      <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-6e43165c0a.css">
                <div id="content_views" class="htmledit_views">
                  <p><code>match</code> 表达式基于值的一致性进行分支计算。 <code>match</code>表达式和 <code>switch</code> 语句类似&#xff0c; 都有一个表达式主体&#xff0c;可以和多个可选项进行比较。 与 <code>switch</code> 不同点是&#xff0c;它会像三元表达式一样求值。 与 <code>switch</code> 另一个不同点&#xff0c;它的比较是严格比较&#xff08; <code>&#61;&#61;&#61;</code>&#xff09;而不是松散比较&#xff08;<code>&#61;&#61;</code>&#xff09;。 Match 表达式从 PHP 8.0.0 起可用。</p>
<p><strong>示例 #1 <code>match</code> 表达式结构</strong></p>
<p><code><?php<br> $return_value &#61; match (subject_expression) {<br>     single_conditional_expression &#61;> return_expression,<br>     conditional_expression1, conditional_expression2 &#61;> return_expression,<br> };<br> ?></code></p>
<p><strong>示例 #2 <code>match</code> 的基础用法</strong></p>
<p><code><?php<br> $food &#61; 'cake';<br><br> $return_value &#61; match ($food) {<br>     'apple' &#61;> 'This food is an apple',<br>     'bar' &#61;> 'This food is a bar',<br>     'cake' &#61;> 'This food is a cake',<br> };<br><br> var_dump($return_value);<br> ?></code></p>
<p>以上例程会输出&#xff1a;</p>
<pre>string(19) &#34;This food is a cake&#34;
</pre>
<blockquote>
<p><strong>注意</strong>: 不一定要使用 <code>match</code> 表达式的结果。</p>
</blockquote>
<blockquote>
<p><strong>注意</strong>: <code>match</code> 表达式<em>必须</em>使用分号 <code>;</code> 结尾。</p>
</blockquote>
<p><code>match</code> 表达式跟 <code>switch</code> 语句相似&#xff0c;但是有以下关键区别&#xff1a;</p>
<ul><li><code>match</code> 比较分支值&#xff0c;使用了严格比较 (<code>&#61;&#61;&#61;</code>)&#xff0c; 而 switch 语句使用了松散比较。</li><li><code>match</code> 表达式会返回一个值。</li><li><code>match</code> 的分支不会像 <code>switch</code> 语句一样&#xff0c; 落空时执行下个 case。</li><li><code>match</code> 表达式必须彻底列举所有情况。</li></ul>
<p><code>match</code> 表达式和 <code>switch</code> 语句类似&#xff0c; 逐个检测匹配分支。一开始不会执行代码。 只有在所有之前的条件不匹配主体表达式时&#xff0c;才会执行剩下的条件表达式。 只会执行返回的表达式所对应的匹配条件表达式。 举例&#xff1a;</p>
<p><code><?php<br> $result &#61; match ($x) {<br>     foo() &#61;> ...,<br>     $this->bar() &#61;> ..., // 如果 foo() &#61;&#61;&#61; $x&#xff0c;不会执行 $this->bar()<br>     $this->baz &#61;> beep(), // 只有 $x &#61;&#61;&#61; $this->baz 时才会执行 beep() <br>     // 等等<br> };<br> ?></code></p>
<p><code>match</code> 表达式分支可以通过逗号分隔&#xff0c;包含多个表达式。 这是一个逻辑 OR&#xff0c;当多个分支表达式右侧相同时&#xff0c;就可以用这种缩写。</p>
<p></p>
<p><code><?php<br> $result &#61; match ($x) {<br>     // 匹配分支&#xff1a;<br>     $a, $b, $c &#61;> 5,<br>     // 等同于以下三个分支&#xff1a;<br>     $a &#61;> 5,<br>     $b &#61;> 5,<br>     $c &#61;> 5,<br> };<br> ?></code></p>
<p><code>default</code> 模式是个特殊的条件。 当之前的条件都不匹配时&#xff0c;会匹配到该模式。 For example:</p>
<p><code><?php<br> $expressionResult &#61; match ($condition) {<br>     1, 2 &#61;> foo(),<br>     3, 4 &#61;> bar(),<br>     default &#61;> baz(),<br> };<br> ?></code></p>
<blockquote>
<p><strong>注意</strong>: 多个 default 模式将会触发 <strong><code>E_FATAL_ERROR</code></strong> 错误。</p>
</blockquote>
<p><code>match</code> 表达式必须详尽列出所有情况。 如果主体表达式不能被任意分支条件处理&#xff0c; 会抛出 <ahref="https://www.php.net/manual/zh/class.unhandledmatcherror.php">UnhandledMatchError</a>。</p>
<p><strong>示例 #3 match 表达式存在未处理的示例</strong></p>
<p><code><?php<br> $condition &#61; 5;<br><br> try {<br>     match ($condition) {<br>         1, 2 &#61;> foo(),<br>         3, 4 &#61;> bar(),<br>     };<br> } catch (\UnhandledMatchError $e) {<br>     var_dump($e);<br> }<br> ?></code></p>
<p>以上例程会输出&#xff1a;</p>
<pre>object(UnhandledMatchError)#1 (7) {
[&#34;message&#34;:protected]&#61;>
string(33) &#34;Unhandled match value of type int&#34;
[&#34;string&#34;:&#34;Error&#34;:private]&#61;>
string(0) &#34;&#34;
[&#34;code&#34;:protected]&#61;>
int(0)
[&#34;file&#34;:protected]&#61;>
string(9) &#34;/in/ICgGK&#34;
[&#34;line&#34;:protected]&#61;>
int(6)
[&#34;trace&#34;:&#34;Error&#34;:private]&#61;>
array(0) {
}
[&#34;previous&#34;:&#34;Error&#34;:private]&#61;>
NULL
}
</pre>
<h3>使用 match 表达式处理非一致性检查</h3>
<p>可以使用 <code>match</code> 表达式将 <code>true</code> 作为主项表达式来处理非一致性条件的情况。</p>
<p><strong>示例 #4 针对整数范围&#xff0c;使用宽泛的表达式匹配分支</strong></p>
<p><code><?php<br><br> $age &#61; 23;<br><br> $result &#61; match (true) {<br>     $age >&#61; 65 &#61;> 'senior',<br>     $age >&#61; 25 &#61;> 'adult',<br>     $age >&#61; 18 &#61;> 'young adult',<br>     default &#61;> 'kid',<br> };<br><br> var_dump($result);<br> ?></code></p>
<p>以上例程会输出&#xff1a;</p>
<pre>string(11) &#34;young adult&#34;
</pre>
<p><strong>示例 #5 针对字符串内容&#xff0c;使用宽泛的表达式匹配分支</strong></p>
<p><code><?php<br><br> $text &#61; 'Bienvenue chez nous';<br><br> $result &#61; match (true) {<br>     str_contains($text, 'Welcome') || str_contains($text, 'Hello') &#61;> 'en',<br>     str_contains($text, 'Bienvenue') || str_contains($text, 'Bonjour') &#61;> 'fr',<br>     // ...<br> };<br><br> var_dump($result);<br> ?></code></p>
<p>以上例程会输出&#xff1a;</p>
<pre>string(2) &#34;fr&#34;</pre>
                </div>
      </div>
      <div id="treeSkill"></div>
页: [1]
查看完整版本: match 表达式基于值的一致性进行分支计算