「PHP/Webアプリケーション向けの機能」の版間の差分

削除された内容 追加された内容
→‎match式: print match ほか
→‎※応用: 関数とmatch: <code>return match</code>
514 行
コード例
<syntaxhighlight lang="PHP">
<?php
$x = 1;
 
function f($x){
match ($x) {
535 行
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>