23,093
回編集
(→match式: print match ほか) |
(→※応用: 関数とmatch: <code>return match</code>) |
||
コード例
<syntaxhighlight lang="PHP">
<?php
$x = 1;
function f($x){
match ($x) {
qwer
qwer
</pre>
関数の戻り値の命令 return と match 式を組み合わせた<code>return match</code>を使うと、下記のような処理も出来る。return match により、アロー分岐後に指定されている値を戻り値に指定できる。
コード例
<syntaxhighlight lang="PHP">
<?php
$x = 2;
function f($x){
return match ($x) {
0 => "aa" . "\n" ,
1 => "qw" . "\n" ,
2 => "te" . "\n" ,
};
}
print(f($x) );
print(f(1) );
?>
</syntaxhighlight>
実行結果
<pre>
te
qw
</pre>
|
回編集