diff options
| author | nsfisis <nsfisis@gmail.com> | 2025-06-27 23:39:31 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2025-06-27 23:39:31 +0900 |
| commit | 674fe965550444db87edc7937ff6932e1a918d9d (patch) | |
| tree | e8a80dd958d3e082485286bf5785a7992b6e6b0e /services/blog/public/posts/2022-09-29/write-fizzbuzz-in-php-2-letters-per-line/index.html | |
| parent | fe4d1d625b53796c5f20399790e5ff8c7a7e1608 (diff) | |
| download | nsfisis.dev-674fe965550444db87edc7937ff6932e1a918d9d.tar.gz nsfisis.dev-674fe965550444db87edc7937ff6932e1a918d9d.tar.zst nsfisis.dev-674fe965550444db87edc7937ff6932e1a918d9d.zip | |
feat(meta): rename vhosts/ directory to services/
Diffstat (limited to 'services/blog/public/posts/2022-09-29/write-fizzbuzz-in-php-2-letters-per-line/index.html')
| -rw-r--r-- | services/blog/public/posts/2022-09-29/write-fizzbuzz-in-php-2-letters-per-line/index.html | 704 |
1 files changed, 704 insertions, 0 deletions
diff --git a/services/blog/public/posts/2022-09-29/write-fizzbuzz-in-php-2-letters-per-line/index.html b/services/blog/public/posts/2022-09-29/write-fizzbuzz-in-php-2-letters-per-line/index.html new file mode 100644 index 00000000..ab8c9867 --- /dev/null +++ b/services/blog/public/posts/2022-09-29/write-fizzbuzz-in-php-2-letters-per-line/index.html @@ -0,0 +1,704 @@ +<!DOCTYPE html> +<html lang="ja-JP"> + <head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <meta name="author" content="nsfisis"> + <meta name="copyright" content="© 2022 nsfisis"> + <meta name="description" content="PHP で fizzbuzz を書いた。ただし、1行あたりに使える文字数は2文字まで。"> + <meta name="keywords" content="PHP"> + <meta property="og:type" content="article"> + <meta property="og:title" content="【PHP】 fizzbuzz を書く。1行あたり2文字で。|REPL: Rest-Eat-Program Loop"> + <meta property="og:description" content="PHP で fizzbuzz を書いた。ただし、1行あたりに使える文字数は2文字まで。"> + <meta property="og:site_name" content="REPL: Rest-Eat-Program Loop"> + <meta property="og:locale" content="ja_JP"> + <link rel="icon" type="image/svg+xml" href="/favicon.svg"> + <title>【PHP】 fizzbuzz を書く。1行あたり2文字で。|REPL: Rest-Eat-Program Loop</title> + <link rel="stylesheet" href="/style.css?h=9513229b52eb2041b99ba1b959305633"> + </head> + <body class="single"> + <header class="header"> + <div class="site-logo"> + <a href="/">REPL: Rest-Eat-Program Loop</a> + </div> + <nav class="nav"> + <ul> + <li> + <a href="/about/">About</a> + </li> + <li> + <a href="/posts/">Posts</a> + </li> + <li> + <a href="/slides/">Slides</a> + </li> + <li> + <a href="/tags/">Tags</a> + </li> + </ul> + </nav> + </header> + <main class="main"> + <article class="post-single"> + <header class="post-header"> + <h1 class="post-title">【PHP】 fizzbuzz を書く。1行あたり2文字で。</h1> + <ul class="post-tags"> + <li class="tag"> + <a href="/tags/php/">PHP</a> + </li> + </ul> + </header> + <div class="post-content"> + <section id="changelog"> + <h2><a href="#changelog">更新履歴</a></h2> + <ol> + <li class="revision"> + <time datetime="2022-09-28">2022-09-28</time>: 公開 + </li> + <li class="revision"> + <time datetime="2022-09-29">2022-09-29</time>: 小さな文言の修正・変更 + </li> + </ol> + </section> + <section id="section--intro"> + <h2><a href="#section--intro">記事の構成について</a></h2> + <p> + この記事は、普通の fizzbuzz を徐々に変形して最終形にしていく、という構成で書かれている。最終形を見てどのような仕組みで動いているのか解読してから解説を読みたい、というかたがいれば、<a href="https://gist.github.com/nsfisis/04c227d5a419867472a0b23a83ad2919#file-fizzbuzz-php-2-letters-per-line-and-supports-php-8-x-without-warnings" rel="noreferrer" target="_blank">このページ</a> にソースコードがあるので、そちらを先に見てほしい。 + </p> + </section> + <section id="section--regulations"> + <h2><a href="#section--regulations">レギュレーション</a></h2> + <p> + PHP で、次のような制約の下に fizzbuzz を書いた。 + </p> + <ul> + <li> + 1行あたりの文字数は2文字までに収めること (ただし <code><?php</code> タグは除く) + <ul> + <li> + 厳密な定義: <code><?php</code> タグ以降のソースコードが、2 byte ごとにラインフィード (LF) で区切られること + </li> + </ul> + </li> + <li> + スペースやタブを使用しないこと + </li> + <li> + ループのアンロールをしないこと + <ul> + <li> + 100 回ループの代わりに 100 回コードをコピペ、というのは禁止 + </li> + </ul> + </li> + <li> + PHP 7.4〜8.1 で動作すること + </li> + <li> + 実行時に Notice や Warning が出ないこと + </li> + <li> + 標準的なインストール構成の PHP で実現できること (デフォルトで有効になっていない拡張等を使わないこと) + </li> + </ul> + <p> + 備考: PHP には <code>short_open_tag</code> というオプションがあり、これを有効にするとファイル冒頭の <code><?php</code> の代わりに <code><?</code> を使うことができ、文字どおり1行2文字で書ける。ただ、このオプションはデフォルト off になっている環境が多いようなので、今回は使わないことにした。 + </p> + </section> + <section id="section--problems"> + <h2><a href="#section--problems">主な障害</a></h2> + <p> + 1行あたりの文字数など、適当に改行を挟めばいいだけではないのか? + </p> + <p> + 特に、C言語でこのような試みをおこなったことがあるかたならそう思うだろう。事実、Cでのこの制約はほとんど無意味に等しい。 + </p> + <div class="codeblock"> + <pre class="shiki github-light" style="background-color:#f5f5f5;color:#24292e" tabindex="0"><code><span class="line"><span style="color:#24292E">#</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#24292E">i</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#24292E">n</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#24292E">c</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#24292E">l</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#24292E">u</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#24292E">d</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#24292E">e</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#D73A49"><</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#24292E">s</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#24292E">t</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#24292E">d</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#24292E">i</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#24292E">o</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#24292E">.</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#24292E">h</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#D73A49">></span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#6A737D">/*</span></span> +<span class="line"><span style="color:#6A737D">*/</span></span> +<span class="line"><span style="color:#24292E">i</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#24292E">n</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#24292E">t</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#6A737D">/*</span></span> +<span class="line"><span style="color:#6A737D">*/</span></span> +<span class="line"><span style="color:#24292E">m</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#24292E">a</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#24292E">i</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#6F42C1">n</span><span style="color:#24292E">(</span></span> +<span class="line"><span style="color:#24292E">){</span></span> +<span class="line"><span style="color:#24292E">f</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#24292E">o</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#6F42C1">r</span><span style="color:#24292E">(</span></span> +<span class="line"><span style="color:#24292E">i</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#24292E">n</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#24292E">t</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#6A737D">/*</span></span> +<span class="line"><span style="color:#6A737D">*/</span></span> +<span class="line"><span style="color:#24292E">i</span><span style="color:#D73A49">=</span></span> +<span class="line"><span style="color:#005CC5">1</span><span style="color:#24292E">;</span></span> +<span class="line"><span style="color:#24292E">i</span><span style="color:#D73A49"><</span></span> +<span class="line"><span style="color:#005CC5">1\</span></span> +<span class="line"><span style="color:#005CC5">0\</span></span> +<span class="line"><span style="color:#005CC5">0</span><span style="color:#24292E">;</span></span> +<span class="line"><span style="color:#24292E">i</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#D73A49">+</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#D73A49">+</span><span style="color:#24292E">)</span></span> +<span class="line"><span style="color:#D73A49">if</span></span> +<span class="line"><span style="color:#24292E">(i</span></span> +<span class="line"><span style="color:#D73A49">%</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#005CC5">15</span></span> +<span class="line"><span style="color:#D73A49">==</span></span> +<span class="line"><span style="color:#005CC5">0</span><span style="color:#24292E">)</span></span> +<span class="line"><span style="color:#24292E">p</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#24292E">r</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#24292E">i</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#24292E">n</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#24292E">t</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#6F42C1">f</span><span style="color:#24292E">(</span></span> +<span class="line"><span style="color:#032F62">"</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#032F62">F</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#032F62">i</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#032F62">z</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#032F62">z</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#032F62">B</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#032F62">u</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#032F62">z</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#032F62">z</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#B31D28;font-style:italic">%</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#032F62">c</span><span style="color:#005CC5">\</span></span> +<span class="line"><span style="color:#032F62">"</span><span style="color:#24292E">,</span></span> +<span class="line"><span style="color:#005CC5">10</span></span> +<span class="line"><span style="color:#24292E">);</span></span> +<span class="line"></span> +<span class="line"><span style="color:#6A737D">/* あとは同じように普通のプログラムを変形するだけなので省略 */</span></span></code></pre> + </div> + <p> + バックスラッシュを使った行継続がトークンを区切らない、というのがポイントだ。 + </p> + <p> + さて、PHP ではそもそもバックスラッシュを行継続に使うことができない。これにより、「3文字以上からなるトークンが一切使えない」という制約が課される。例えば、<code>echo</code> で出力することや、<code>for</code> でループすること、<code>new</code> でインスタンスを生成することができない。特に、出力は fizzbuzz をどんなアルゴリズムで実装しようとおこなわなければならないので、できないのは致命的である。 + </p> + <p> + 当然、名前が3文字以上ある関数も使えない。なお、標準 PHP の範囲内において、名前が 2文字以下の関数は以下のとおりである: + </p> + <ul> + <li> + <code>_</code>: <code>gettext</code> のエイリアス + </li> + <li> + <code>dl</code>: 拡張モジュールをロードする + </li> + <li> + <code>pi</code>: 円周率を返す + </li> + </ul> + <p> + (環境によって多少は変わるかも) + </p> + <p> + 2文字の関数を定義しまくった拡張モジュールを用意しておいて <code>dl()</code> で読み込む行為は、レギュレーションで定めた + </p> + <blockquote> + <ul> + <li> + 標準的なインストール構成の PHP で実現できること (デフォルトで有効になっていない拡張等を使わないこと) + </li> + </ul> + </blockquote> + <p> + に反する (というより、「それだとおもしろくもなんともないので、このルールを足した」というのが正しい)。 + </p> + <p> + また、2文字だと文字列がまともに書けないのも辛い。<code>''</code> だけで2文字使うので、「1文字の文字列リテラル」というものを書くことができない。PHP では文字列リテラル中に生の改行が書けるので + </p> + <div class="codeblock"> + <pre class="shiki github-light" style="background-color:#f5f5f5;color:#24292e" tabindex="0"><code><span class="line"><span style="color:#24292E">$a</span></span> +<span class="line"><span style="color:#D73A49">=</span><span style="color:#032F62">'</span></span> +<span class="line"><span style="color:#032F62">a'</span></span> +<span class="line"><span style="color:#24292E">;;</span></span></code></pre> + </div> + <p> + とすると <code>$a</code> は <code>"\na"</code> になるのだが、余計な改行が入ってしまう。 + </p> + <p> + これらの障害をどのように乗り越えるのか、次節から見ていく。 + </p> + </section> + <section id="section--commentary"> + <h2><a href="#section--commentary">解説</a></h2> + <section id="section--commentary--normal-fizzbuzz"> + <h3><a href="#section--commentary--normal-fizzbuzz">普通の (?) fizzbuzz</a></h3> + <p> + まずは普通に書くとしよう。 + </p> + <div class="codeblock"> + <pre class="shiki github-light" style="background-color:#f5f5f5;color:#24292e" tabindex="0"><code><span class="line"><span style="color:#D73A49"><?</span><span style="color:#005CC5">php</span></span> +<span class="line"></span> +<span class="line"><span style="color:#D73A49">for</span><span style="color:#24292E"> ($i </span><span style="color:#D73A49">=</span><span style="color:#005CC5"> 1</span><span style="color:#24292E">; $i </span><span style="color:#D73A49"><</span><span style="color:#005CC5"> 100</span><span style="color:#24292E">; $i</span><span style="color:#D73A49">++</span><span style="color:#24292E">) {</span></span> +<span class="line"><span style="color:#005CC5"> echo</span><span style="color:#24292E"> (($i </span><span style="color:#D73A49">%</span><span style="color:#005CC5"> 3</span><span style="color:#D73A49"> ?</span><span style="color:#032F62"> ''</span><span style="color:#D73A49"> :</span><span style="color:#032F62"> 'Fizz'</span><span style="color:#24292E">) </span><span style="color:#D73A49">.</span><span style="color:#24292E"> ($i </span><span style="color:#D73A49">%</span><span style="color:#005CC5"> 5</span><span style="color:#D73A49"> ?</span><span style="color:#032F62"> ''</span><span style="color:#D73A49"> :</span><span style="color:#032F62"> 'Buzz'</span><span style="color:#24292E">) </span><span style="color:#D73A49">?:</span><span style="color:#24292E"> $i) </span><span style="color:#D73A49">.</span><span style="color:#032F62"> "</span><span style="color:#005CC5">\n</span><span style="color:#032F62">"</span><span style="color:#24292E">;</span></span> +<span class="line"><span style="color:#24292E">}</span></span></code></pre> + </div> + <p> + 素直に書いた fizzbuzz とは言い難いが、このくらいは普通だということにしておかないと、この先がやっていられないので許してほしい。 + </p> + </section> + <section id="section--commentary--remove-keywords"> + <h3><a href="#section--commentary--remove-keywords"><code>for</code> の排除</a></h3> + <p> + <code>for</code> は、3文字もある長いキーワードである。こんなものは使えない。<code>array_</code> 系の関数を使って、適当に置き換えるとしよう。 + </p> + <div class="codeblock"> + <pre class="shiki github-light" style="background-color:#f5f5f5;color:#24292e" tabindex="0"><code><span class="line"><span style="color:#D73A49"><?</span><span style="color:#005CC5">php</span></span> +<span class="line"></span> +<span class="line"><span style="color:#24292E">$s </span><span style="color:#D73A49">=</span><span style="color:#005CC5"> range</span><span style="color:#24292E">(</span><span style="color:#005CC5">1</span><span style="color:#24292E">, </span><span style="color:#005CC5">100</span><span style="color:#24292E">);</span></span> +<span class="line"><span style="color:#005CC5">array_walk</span><span style="color:#24292E">(</span></span> +<span class="line"><span style="color:#24292E">$s,</span></span> +<span class="line"><span style="color:#D73A49">fn</span><span style="color:#24292E">($i) =></span></span> +<span class="line"><span style="color:#005CC5">printf</span><span style="color:#24292E">((($i </span><span style="color:#D73A49">%</span><span style="color:#005CC5"> 3</span><span style="color:#D73A49"> ?</span><span style="color:#032F62"> ''</span><span style="color:#D73A49"> :</span><span style="color:#032F62"> 'Fizz'</span><span style="color:#24292E">) </span><span style="color:#D73A49">.</span><span style="color:#24292E"> ($i </span><span style="color:#D73A49">%</span><span style="color:#005CC5"> 5</span><span style="color:#D73A49"> ?</span><span style="color:#032F62"> ''</span><span style="color:#D73A49"> :</span><span style="color:#032F62"> 'Buzz'</span><span style="color:#24292E">) </span><span style="color:#D73A49">?:</span><span style="color:#24292E"> $i) </span><span style="color:#D73A49">.</span><span style="color:#032F62"> "</span><span style="color:#005CC5">\n</span><span style="color:#032F62">"</span><span style="color:#24292E">),</span></span> +<span class="line"><span style="color:#24292E">);</span></span></code></pre> + </div> + <p> + <code>array_walk</code> や <code>range</code>、<code>printf</code> といった <code>for</code> よりも長いトークンが現れてしまったが、これは次節で直すことにする。なお、<code>echo</code> は文 (statement) であり式 (expression) ではないので、式である <code>printf</code> に置き換えた。 + </p> + </section> + <section id="section--commentary--shorten-function-invocation"> + <h3><a href="#section--commentary--shorten-function-invocation">関数呼び出しの短縮</a></h3> + <p> + <code>range</code>、<code>array_walk</code>、<code>printf</code> は長すぎるのでどうにかせねばならない。ここで、PHP の可変関数を使う。可変関数とは、関数名が文字列として入った変数を経由して、関数を呼び出す機能である。 + </p> + <div class="codeblock"> + <pre class="shiki github-light" style="background-color:#f5f5f5;color:#24292e" tabindex="0"><code><span class="line"><span style="color:#D73A49"><?</span><span style="color:#005CC5">php</span></span> +<span class="line"></span> +<span class="line"><span style="color:#24292E">$r </span><span style="color:#D73A49">=</span><span style="color:#032F62"> 'range'</span><span style="color:#24292E">;</span></span> +<span class="line"><span style="color:#24292E">$w </span><span style="color:#D73A49">=</span><span style="color:#032F62"> 'array_walk'</span><span style="color:#24292E">;</span></span> +<span class="line"><span style="color:#24292E">$p </span><span style="color:#D73A49">=</span><span style="color:#032F62"> 'printf'</span><span style="color:#24292E">;</span></span> +<span class="line"></span> +<span class="line"><span style="color:#24292E">$s </span><span style="color:#D73A49">=</span><span style="color:#24292E"> $r(</span><span style="color:#005CC5">1</span><span style="color:#24292E">, </span><span style="color:#005CC5">100</span><span style="color:#24292E">);</span></span> +<span class="line"><span style="color:#24292E">$w(</span></span> +<span class="line"><span style="color:#24292E">$s,</span></span> +<span class="line"><span style="color:#D73A49">fn</span><span style="color:#24292E">($i) =></span></span> +<span class="line"><span style="color:#24292E">$p((($i </span><span style="color:#D73A49">%</span><span style="color:#005CC5"> 3</span><span style="color:#D73A49"> ?</span><span style="color:#032F62"> ''</span><span style="color:#D73A49"> :</span><span style="color:#032F62"> 'Fizz'</span><span style="color:#24292E">) </span><span style="color:#D73A49">.</span><span style="color:#24292E"> ($i </span><span style="color:#D73A49">%</span><span style="color:#005CC5"> 5</span><span style="color:#D73A49"> ?</span><span style="color:#032F62"> ''</span><span style="color:#D73A49"> :</span><span style="color:#032F62"> 'Buzz'</span><span style="color:#24292E">) </span><span style="color:#D73A49">?:</span><span style="color:#24292E"> $i) </span><span style="color:#D73A49">.</span><span style="color:#032F62"> "</span><span style="color:#005CC5">\n</span><span style="color:#032F62">"</span><span style="color:#24292E">),</span></span> +<span class="line"><span style="color:#24292E">);</span></span></code></pre> + </div> + <p> + これで関数を呼び出している所は短くなった。では、<code>$r</code> や <code>$w</code> や <code>$p</code>、また <code>'Fizz'</code> や <code>'Buzz'</code> はどうやって 1 行 2 文字に収めるのか。次のテクニックへ移ろう。 + </p> + </section> + <section id="section--commentary--incompatible-solution"> + <h3><a href="#section--commentary--incompatible-solution">余談: PHP 8.x で動作しなくてもいいなら</a></h3> + <p> + 今回使ったテクニックを説明する前に、余談として、文字列リテラルの短縮法として今回採用しなかったものを紹介する。 + </p> + <blockquote> + <ul> + <li> + PHP 7.4〜8.1 で動作すること + </li> + </ul> + </blockquote> + <p> + というルールがない場合、「未定義の定数が評価された場合、その定数の名前が値になる」という PHP 7.x までの仕様が利用できる。例えば、 <code>Fizz</code> という文字列が欲しければ、次のようにする。 + </p> + <div class="codeblock"> + <pre class="shiki github-light" style="background-color:#f5f5f5;color:#24292e" tabindex="0"><code><span class="line"><span style="color:#24292E">$f</span></span> +<span class="line"><span style="color:#D73A49">=</span><span style="color:#005CC5">F</span></span> +<span class="line"><span style="color:#D73A49">.</span><span style="color:#005CC5">i</span></span> +<span class="line"><span style="color:#D73A49">.</span><span style="color:#005CC5">z</span></span> +<span class="line"><span style="color:#D73A49">.</span><span style="color:#005CC5">z</span></span> +<span class="line"><span style="color:#24292E">;;</span></span></code></pre> + </div> + <p> + こうして簡単に文字列を作れる。なお、この仕様は 7.x 時点でも警告を受けるので、<code>@</code> 演算子を使って抑制してやるとよい。 + </p> + <div class="codeblock"> + <pre class="shiki github-light" style="background-color:#f5f5f5;color:#24292e" tabindex="0"><code><span class="line"><span style="color:#24292E">$f</span></span> +<span class="line"><span style="color:#D73A49">=@</span></span> +<span class="line"><span style="color:#005CC5">F</span><span style="color:#D73A49">.</span></span> +<span class="line"><span style="color:#D73A49">@</span><span style="color:#005CC5">i</span></span> +<span class="line"><span style="color:#D73A49">.</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#D73A49">@</span><span style="color:#005CC5">z</span></span> +<span class="line"><span style="color:#D73A49">.</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#D73A49">@</span><span style="color:#005CC5">z</span></span> +<span class="line"><span style="color:#24292E">;;</span></span></code></pre> + </div> + <p> + むしろ、このことがわかっていたからこそ PHP 8.x での動作を要件に課したところがある。 + </p> + </section> + <section id="section--commentary--shorten-string-literals"> + <h3><a href="#section--commentary--shorten-string-literals">文字列リテラルの短縮</a></h3> + <p> + 実際に使った手法の説明に移る。 + </p> + <p> + ずばり、文字列同士のビット演算を使う。PHP では、文字列同士でビット演算 (<code>&</code>、<code>|</code>、<code>^</code>) をした場合、文字列の各バイトごとに指定したビット演算がなされ、それを結合したものが演算結果となる。 + </p> + <div class="codeblock"> + <pre class="shiki github-light" style="background-color:#f5f5f5;color:#24292e" tabindex="0"><code><span class="line"><span style="color:#24292E">$a </span><span style="color:#D73A49">=</span><span style="color:#032F62"> "12345"</span><span style="color:#24292E">;</span></span> +<span class="line"><span style="color:#24292E">$b </span><span style="color:#D73A49">=</span><span style="color:#032F62"> "world"</span><span style="color:#24292E">;</span></span> +<span class="line"></span> +<span class="line"><span style="color:#6A737D">// $a ^ $b は次のコードと同じ</span></span> +<span class="line"><span style="color:#24292E">$result </span><span style="color:#D73A49">=</span><span style="color:#032F62"> ''</span><span style="color:#24292E">;</span></span> +<span class="line"><span style="color:#D73A49">for</span><span style="color:#24292E"> ($i </span><span style="color:#D73A49">=</span><span style="color:#005CC5"> 0</span><span style="color:#24292E">; $i </span><span style="color:#D73A49"><</span><span style="color:#005CC5"> min</span><span style="color:#24292E">(</span><span style="color:#005CC5">strlen</span><span style="color:#24292E">($a), </span><span style="color:#005CC5">strlen</span><span style="color:#24292E">($b)); $i</span><span style="color:#D73A49">++</span><span style="color:#24292E">) {</span></span> +<span class="line"><span style="color:#24292E">$result </span><span style="color:#D73A49">.=</span><span style="color:#24292E"> $a[$i] </span><span style="color:#D73A49">^</span><span style="color:#24292E"> $b[$i];</span></span> +<span class="line"><span style="color:#24292E">}</span></span> +<span class="line"></span> +<span class="line"><span style="color:#005CC5">echo</span><span style="color:#24292E"> $result;</span></span> +<span class="line"><span style="color:#6A737D">// => F]AXQ</span></span></code></pre> + </div> + <p> + これを踏まえ、次のコードを見てみよう。 + </p> + <div class="codeblock"> + <pre class="shiki github-light" style="background-color:#f5f5f5;color:#24292e" tabindex="0"><code><span class="line"><span style="color:#24292E">$x </span><span style="color:#D73A49">=</span><span style="color:#032F62"> "x</span><span style="color:#005CC5">\n</span><span style="color:#032F62">Om</span><span style="color:#005CC5">\n</span><span style="color:#032F62">"</span><span style="color:#24292E">;</span></span> +<span class="line"><span style="color:#24292E">$y </span><span style="color:#D73A49">=</span><span style="color:#032F62"> "</span><span style="color:#005CC5">\n</span><span style="color:#032F62">k!</span><span style="color:#005CC5">\n</span><span style="color:#032F62">o"</span><span style="color:#24292E">;</span></span> +<span class="line"><span style="color:#24292E">$r </span><span style="color:#D73A49">=</span><span style="color:#24292E"> $x </span><span style="color:#D73A49">^</span><span style="color:#24292E"> $y;</span></span> +<span class="line"><span style="color:#005CC5">echo</span><span style="color:#032F62"> "</span><span style="color:#24292E">$r</span><span style="color:#005CC5">\n</span><span style="color:#032F62">"</span><span style="color:#24292E">;</span></span></code></pre> + </div> + <p> + 実行すると、<code>range</code> が表示される。さて、PHP では文字列リテラル中に生の改行を直接書いてもよいのだった (「主な障害」の節を参照のこと)。書きかえてみよう。 + </p> + <div class="codeblock"> + <pre class="shiki github-light" style="background-color:#f5f5f5;color:#24292e" tabindex="0"><code><span class="line"><span style="color:#24292E">$x</span></span> +<span class="line"><span style="color:#D73A49">=</span><span style="color:#032F62">'x</span></span> +<span class="line"><span style="color:#032F62">Om</span></span> +<span class="line"><span style="color:#032F62">'</span><span style="color:#24292E">;</span></span> +<span class="line"><span style="color:#24292E">$y</span></span> +<span class="line"><span style="color:#D73A49">=</span><span style="color:#032F62">'</span></span> +<span class="line"><span style="color:#032F62">k!</span></span> +<span class="line"><span style="color:#032F62">o'</span></span> +<span class="line"><span style="color:#24292E">;</span></span> +<span class="line"></span> +<span class="line"><span style="color:#24292E">$r </span><span style="color:#D73A49">=</span><span style="color:#24292E"> $x </span><span style="color:#D73A49">^</span><span style="color:#24292E"> $y;</span></span> +<span class="line"><span style="color:#005CC5">echo</span><span style="color:#032F62"> "</span><span style="color:#24292E">$r</span><span style="color:#005CC5">\n</span><span style="color:#032F62">"</span><span style="color:#24292E">;</span></span></code></pre> + </div> + <p> + さらに <code>#</code> を使って適当に調整すると、次のようになる。 + </p> + <div class="codeblock"> + <pre class="shiki github-light" style="background-color:#f5f5f5;color:#24292e" tabindex="0"><code><span class="line"><span style="color:#24292E">$x</span></span> +<span class="line"><span style="color:#D73A49">=</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#032F62">'x</span></span> +<span class="line"><span style="color:#032F62">Om</span></span> +<span class="line"><span style="color:#032F62">'</span><span style="color:#24292E">;</span></span> +<span class="line"><span style="color:#24292E">$y</span></span> +<span class="line"><span style="color:#D73A49">=</span><span style="color:#032F62">'</span></span> +<span class="line"><span style="color:#032F62">k!</span></span> +<span class="line"><span style="color:#032F62">o'</span></span> +<span class="line"><span style="color:#24292E">;</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#24292E">$r</span></span> +<span class="line"><span style="color:#D73A49">=</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#24292E">$x</span></span> +<span class="line"><span style="color:#D73A49">^</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#24292E">$y</span></span> +<span class="line"><span style="color:#24292E">;</span><span style="color:#6A737D">#</span></span> +<span class="line"></span> +<span class="line"><span style="color:#005CC5">echo</span><span style="color:#032F62"> "</span><span style="color:#24292E">$r</span><span style="color:#005CC5">\n</span><span style="color:#032F62">"</span><span style="color:#24292E">;</span></span></code></pre> + </div> + <p> + 1行あたり2文字で、<code>range</code> という文字列を生成することに成功した。他の必要な文字列にも、同様の処理をほどこす。 + </p> + <p> + 備考: <code>Buzz</code> 中にある小文字の <code>u</code> は、このロジックだと non-printable な文字になってしまう。ここまでのテクニックを駆使すれば回避するのはそう難しくないので、考えてみてほしい。 + </p> + </section> + </section> + <section id="section--stretched-fizzbuzz"> + <h2><a href="#section--stretched-fizzbuzz">完成系</a></h2> + <p> + 完成したものがこちら。 + </p> + <div class="codeblock"> + <pre class="shiki github-light" style="background-color:#f5f5f5;color:#24292e" tabindex="0"><code><span class="line"><span style="color:#D73A49"><?</span><span style="color:#005CC5">php</span></span> +<span class="line"></span> +<span class="line"><span style="color:#24292E">$x</span></span> +<span class="line"><span style="color:#D73A49">=</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#032F62">'i</span></span> +<span class="line"><span style="color:#032F62">S'</span></span> +<span class="line"><span style="color:#24292E">;;</span></span> +<span class="line"><span style="color:#24292E">$y</span></span> +<span class="line"><span style="color:#D73A49">=</span><span style="color:#032F62">'</span></span> +<span class="line"><span style="color:#032F62">b!</span></span> +<span class="line"><span style="color:#032F62">'</span><span style="color:#24292E">;</span></span> +<span class="line"><span style="color:#24292E">$c</span></span> +<span class="line"><span style="color:#D73A49">=</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#24292E">$x</span></span> +<span class="line"><span style="color:#D73A49">^</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#24292E">$y</span></span> +<span class="line"><span style="color:#24292E">;</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#24292E">$x</span></span> +<span class="line"><span style="color:#D73A49">=</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#032F62">'x</span></span> +<span class="line"><span style="color:#032F62">Om</span></span> +<span class="line"><span style="color:#032F62">'</span><span style="color:#24292E">;</span></span> +<span class="line"><span style="color:#24292E">$y</span></span> +<span class="line"><span style="color:#D73A49">=</span><span style="color:#032F62">'</span></span> +<span class="line"><span style="color:#032F62">k!</span></span> +<span class="line"><span style="color:#032F62">o'</span></span> +<span class="line"><span style="color:#24292E">;</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#24292E">$r</span></span> +<span class="line"><span style="color:#D73A49">=</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#24292E">$x</span></span> +<span class="line"><span style="color:#D73A49">^</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#24292E">$y</span></span> +<span class="line"><span style="color:#24292E">;</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#24292E">$x</span></span> +<span class="line"><span style="color:#D73A49">=</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#032F62">'k</span></span> +<span class="line"><span style="color:#032F62">Sk</span></span> +<span class="line"><span style="color:#032F62">~}</span></span> +<span class="line"><span style="color:#032F62">Ma</span></span> +<span class="line"><span style="color:#032F62">'</span><span style="color:#24292E">;</span></span> +<span class="line"><span style="color:#24292E">$y</span></span> +<span class="line"><span style="color:#D73A49">=</span><span style="color:#032F62">'</span></span> +<span class="line"><span style="color:#032F62">x!</span></span> +<span class="line"><span style="color:#032F62">s!</span></span> +<span class="line"><span style="color:#032F62">k!</span></span> +<span class="line"><span style="color:#032F62">'</span><span style="color:#24292E">;</span></span> +<span class="line"><span style="color:#24292E">$w</span></span> +<span class="line"><span style="color:#D73A49">=</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#24292E">$x</span></span> +<span class="line"><span style="color:#D73A49">^</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#24292E">$y</span></span> +<span class="line"><span style="color:#24292E">;</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#24292E">$x</span></span> +<span class="line"><span style="color:#D73A49">=</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#032F62">'z</span></span> +<span class="line"><span style="color:#032F62">Hd</span></span> +<span class="line"><span style="color:#032F62">G'</span></span> +<span class="line"><span style="color:#24292E">;</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#24292E">$y</span></span> +<span class="line"><span style="color:#D73A49">=</span><span style="color:#032F62">'</span></span> +<span class="line"><span style="color:#032F62">x!</span></span> +<span class="line"><span style="color:#032F62">~!</span></span> +<span class="line"><span style="color:#032F62">'</span><span style="color:#24292E">;</span></span> +<span class="line"><span style="color:#24292E">$p</span></span> +<span class="line"><span style="color:#D73A49">=</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#24292E">$x</span></span> +<span class="line"><span style="color:#D73A49">^</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#24292E">$y</span></span> +<span class="line"><span style="color:#24292E">;</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#24292E">$x</span></span> +<span class="line"><span style="color:#D73A49">=</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#032F62">'L</span></span> +<span class="line"><span style="color:#032F62">[p</span></span> +<span class="line"><span style="color:#032F62">'</span><span style="color:#24292E">;</span></span> +<span class="line"><span style="color:#24292E">$y</span></span> +<span class="line"><span style="color:#D73A49">=</span><span style="color:#032F62">'</span></span> +<span class="line"><span style="color:#032F62">c!</span></span> +<span class="line"><span style="color:#032F62">'</span><span style="color:#24292E">;</span></span> +<span class="line"><span style="color:#24292E">$f</span></span> +<span class="line"><span style="color:#D73A49">=</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#24292E">$x</span></span> +<span class="line"><span style="color:#D73A49">^</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#24292E">$y</span></span> +<span class="line"><span style="color:#24292E">;</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#24292E">$x</span></span> +<span class="line"><span style="color:#D73A49">=</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#032F62">'H</span></span> +<span class="line"><span style="color:#032F62">[p</span></span> +<span class="line"><span style="color:#032F62">'</span><span style="color:#24292E">;</span></span> +<span class="line"><span style="color:#24292E">$y</span></span> +<span class="line"><span style="color:#D73A49">=</span><span style="color:#032F62">'</span></span> +<span class="line"><span style="color:#032F62">_!</span></span> +<span class="line"><span style="color:#032F62">'</span><span style="color:#24292E">;</span></span> +<span class="line"><span style="color:#24292E">$b</span></span> +<span class="line"><span style="color:#D73A49">=</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#24292E">$x</span></span> +<span class="line"><span style="color:#D73A49">^</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#24292E">$y</span></span> +<span class="line"><span style="color:#24292E">;</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#24292E">$b</span></span> +<span class="line"><span style="color:#24292E">[</span><span style="color:#005CC5">1</span></span> +<span class="line"><span style="color:#24292E">]</span><span style="color:#D73A49">=</span></span> +<span class="line"><span style="color:#24292E">$c</span></span> +<span class="line"><span style="color:#24292E">(</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#005CC5">13</span></span> +<span class="line"><span style="color:#D73A49">*</span><span style="color:#005CC5">9</span></span> +<span class="line"><span style="color:#24292E">);</span></span> +<span class="line"><span style="color:#24292E">$s</span></span> +<span class="line"><span style="color:#D73A49">=</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#24292E">$r</span></span> +<span class="line"><span style="color:#24292E">(</span><span style="color:#005CC5">1</span></span> +<span class="line"><span style="color:#24292E">,(</span></span> +<span class="line"><span style="color:#005CC5">10</span></span> +<span class="line"><span style="color:#D73A49">**</span></span> +<span class="line"><span style="color:#005CC5">2</span><span style="color:#24292E">)</span></span> +<span class="line"><span style="color:#24292E">);</span></span> +<span class="line"><span style="color:#24292E">$w</span></span> +<span class="line"><span style="color:#24292E">(</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#24292E">$s</span></span> +<span class="line"><span style="color:#24292E">,</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#005CC5">fn</span></span> +<span class="line"><span style="color:#24292E">(</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#24292E">$i</span></span> +<span class="line"><span style="color:#24292E">)</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#D73A49">=></span></span> +<span class="line"><span style="color:#24292E">$p</span></span> +<span class="line"><span style="color:#24292E">((</span></span> +<span class="line"><span style="color:#24292E">(</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#24292E">$i</span></span> +<span class="line"><span style="color:#D73A49">%</span><span style="color:#005CC5">3</span></span> +<span class="line"><span style="color:#D73A49">?</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#032F62">''</span></span> +<span class="line"><span style="color:#D73A49">:</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#24292E">$f</span></span> +<span class="line"><span style="color:#24292E">)</span><span style="color:#D73A49">.</span></span> +<span class="line"><span style="color:#24292E">(</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#24292E">$i</span></span> +<span class="line"><span style="color:#D73A49">%</span><span style="color:#005CC5">5</span></span> +<span class="line"><span style="color:#D73A49">?</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#032F62">''</span></span> +<span class="line"><span style="color:#D73A49">:</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#24292E">$b</span></span> +<span class="line"><span style="color:#24292E">)</span><span style="color:#D73A49">?</span></span> +<span class="line"><span style="color:#D73A49">:</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#24292E">$i</span></span> +<span class="line"><span style="color:#24292E">)</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#D73A49">.</span><span style="color:#032F62">'</span></span> +<span class="line"><span style="color:#032F62">'</span><span style="color:#24292E">)</span></span> +<span class="line"><span style="color:#24292E">);</span></span></code></pre> + </div> + </section> + <section id="section--outro"> + <h2><a href="#section--outro">感想など</a></h2> + <p> + PHP は、スクリプト言語の中だとシンタックスシュガーが少ない (体感)。この挑戦は不可能に思われたが、PHP マニュアルとにらめっこしていたらなんとかなった。 + </p> + <p> + みんなもプログラムを細長くしよう。 + </p> + </section> + <section id="section--alternative-solution"> + <h2><a href="#section--alternative-solution">余談2: 別解</a></h2> + <p> + PHP では、バッククォートを使ってシェルを呼び出せる。これは <code>shell_exec</code> 関数と等価である。さて、PHP ではバックスラッシュによる行継続が使えないと書いたが、シェルでは使える (当然だが、呼び出されるシェルに依存する。Bash なら大丈夫だろう。知らんけど)。 + </p> + <div class="codeblock"> + <pre class="shiki github-light" style="background-color:#f5f5f5;color:#24292e" tabindex="0"><code><span class="line"><span style="color:#D73A49"><?</span><span style="color:#005CC5">php</span></span> +<span class="line"></span> +<span class="line"><span style="color:#005CC5">printf</span><span style="color:#24292E">(</span><span style="color:#032F62">`</span></span> +<span class="line"><span style="color:#032F62">e\</span></span> +<span class="line"><span style="color:#032F62">c\</span></span> +<span class="line"><span style="color:#032F62">h\</span></span> +<span class="line"><span style="color:#032F62">o\</span></span> +<span class="line"><span style="color:#032F62">\</span></span> +<span class="line"><span style="color:#032F62">1\</span></span> +<span class="line"><span style="color:#032F62">2\</span></span> +<span class="line"><span style="color:#032F62">3\</span></span> +<span class="line"><span style="color:#032F62">`</span><span style="color:#24292E">);</span></span></code></pre> + </div> + <p> + なお、ここでは簡単のため出力に <code>printf</code> をそのまま使っているが、実際には <code>printf</code> という文字列を合成して可変関数で呼び出す。 + </p> + <p> + ただし、これでは + </p> + <blockquote> + <ul> + <li> + スペースやタブを使用しないこと + </li> + </ul> + </blockquote> + <p> + に違反してしまう。スペースが使えないと引数とコマンドを区切れない。これは困った。 + </p> + <p> + もうこれ以上は不可能だと思っていたのだが、この記事の執筆中に解決する方法を思いついたので載せておく。 + </p> + <div class="codeblock"> + <pre class="shiki github-light" style="background-color:#f5f5f5;color:#24292e" tabindex="0"><code><span class="line"><span style="color:#D73A49"><?</span><span style="color:#005CC5">php</span></span> +<span class="line"></span> +<span class="line"><span style="color:#24292E">$c </span><span style="color:#D73A49">=</span><span style="color:#032F62"> 'chr'</span><span style="color:#24292E">;</span></span> +<span class="line"></span> +<span class="line"><span style="color:#24292E">${</span></span> +<span class="line"><span style="color:#032F62">'_</span></span> +<span class="line"><span style="color:#032F62">'</span><span style="color:#24292E">}</span></span> +<span class="line"><span style="color:#D73A49">=</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#24292E">$c</span></span> +<span class="line"><span style="color:#24292E">(</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#005CC5">32</span></span> +<span class="line"><span style="color:#24292E">)</span><span style="color:#D73A49">.</span></span> +<span class="line"><span style="color:#24292E">$c</span></span> +<span class="line"><span style="color:#24292E">(</span><span style="color:#6A737D">#</span></span> +<span class="line"><span style="color:#005CC5">92</span></span> +<span class="line"><span style="color:#24292E">);</span></span> +<span class="line"></span> +<span class="line"><span style="color:#005CC5">printf</span><span style="color:#24292E">(</span><span style="color:#032F62">`</span></span> +<span class="line"><span style="color:#032F62">e\</span></span> +<span class="line"><span style="color:#032F62">c\</span></span> +<span class="line"><span style="color:#032F62">h\</span></span> +<span class="line"><span style="color:#032F62">o\</span></span> +<span class="line"><span style="color:#032F62">${</span></span> +<span class="line"><span style="color:#032F62">'_</span></span> +<span class="line"><span style="color:#032F62">'}</span></span> +<span class="line"><span style="color:#032F62">1\</span></span> +<span class="line"><span style="color:#032F62">2\</span></span> +<span class="line"><span style="color:#032F62">3\</span></span> +<span class="line"><span style="color:#032F62">`</span><span style="color:#24292E">);</span></span></code></pre> + </div> + <p> + 先程と同じく、<code>chr</code> や <code>printf</code> を生成する部分は長くなるので省いた。 + </p> + <div class="codeblock"> + <pre class="shiki github-light" style="background-color:#f5f5f5;color:#24292e" tabindex="0"><code><span class="line"><span>${</span></span> +<span class="line"><span>'_</span></span> +<span class="line"><span>'}</span></span></code></pre> + </div> + <p> + は変数で、中にはスペースとエスケープが入っている (<code>chr(32) . chr(92)</code>)。シェルに渡されている文字列は次のようになる。 + </p> + <div class="codeblock"> + <pre class="shiki github-light" style="background-color:#f5f5f5;color:#24292e" tabindex="0"><code><span class="line"><span>e\</span></span> +<span class="line"><span>c\</span></span> +<span class="line"><span>h\</span></span> +<span class="line"><span>o\</span></span> +<span class="line"><span>\</span></span> +<span class="line"><span>1\</span></span> +<span class="line"><span>2\</span></span> +<span class="line"><span>3\</span></span></code></pre> + </div> + <p> + これは、前掲したコマンドと同じだ。かくして、スペースを陽に書かずにシェルをおおよそ自由に扱えるようになった。Fizzbuzz のワンライナーくらいすぐ書けるだろうから、あとはなんとかなるだろう (試してないけど)。 + </p> + <p> + ということでこれは別解ということにしておく。 + </p> + <p> + ちなみに、PHP 8.2 からは、この記法で Warning が出るようになるようだ。 + </p> + <div class="codeblock"> + <pre class="shiki github-light" style="background-color:#f5f5f5;color:#24292e" tabindex="0"><code><span class="line"><span>${</span></span> +<span class="line"><span>'_</span></span> +<span class="line"><span>'}</span></span></code></pre> + </div> + <p> + 最新版で警告が出るというのも美しくないので、私としては本編の解法を推す。 + </p> + </section> + </div> + </article> + </main> + <footer class="footer"> + © 2021 nsfisis + </footer> + </body> +</html> |
