[HP]Stringの正しい使い方
他の人が作成したコードを見ていると、たまに似たようなコードが見えます.
この部分ではPHPがどんな言語なのか感嘆します.
PHPDoc.types.string
PSR-2
PSR−2は、
しかし、このように
だから私の主張を支持してくれる資料を探しているうちに、適当な資料を見つけました.
パフォーマンスのホットスポット
PHPDoc.types.string.ジョンのコメント
実行環境
では、
この投稿の著者は,テストで分かる点を整理した. Always use double-quoted strings for concatenation.
常に をマージ Put your variables in
変数は、 Feel free to use single-quoted strings for TOTALLY literal strings such as array keys/values, variable values, etc, since they are a TINY bit faster when you want literal non-parsed strings. But I had to do 1 billion iterations to find a 1.55% measurable difference. So the only real reason I'd consider using single-quoted strings for my literals is for code cleanliness, to make it super clear that the string is literal.
キー/値、変数値などの純粋な文字列を使用する場合は、小引用符を使用することが望ましい.しかし1.55%の測定可能な差を見つけるためには10億回繰り返す必要がある.だから私が文字に引用符をつけた文字列を使う唯一の本当の原因はコードをきれいにするためです. 文字列がアルファベット順に並んでいることを確認します. If you think another method such as sprintf() or
sprintf()や これらの理由から、php stringを
echo "print $var";
これは目まぐるしいコードで、実行できます.この部分ではPHPがどんな言語なのか感嘆します.
Psy Shell v0.10.8 (PHP 8.0.8 — cli) by Justin Hileman
>>> $var = "이게 출력이 된다고요?";
=> "이게 출력이 된다고요?"
>>> echo "print $var";
print 이게 출력이 된다고요?⏎
これらのコードはどのように見ても可読性が低下し,後で問題になる可能性が高いため,このように使用すべきではない理由を調べた.PHPDoc.types.string
"
内で記号($)に遭遇すると、解析器は、変数を形成するために貪欲に大量のリソースを取得する.PSR-2
'
または"
?同じ動作です.'
を使用することをお勧めします.中のHTML構文または変数には"
を使用します..
を前後に使用してください.PSR−2は、
.
を使用して文字列のマージを行う必要がある.しかし、このように
.
の結合度を利用すると毒性が低下すると思います.だから私の主張を支持してくれる資料を探しているうちに、適当な資料を見つけました.
パフォーマンスのホットスポット
PHPDoc.types.string.ジョンのコメント
実行環境
php -v
PHP 7.0.12 (cli) (built: Oct 14 2016 09:56:59) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
100 million iterations (a test intended to see which one of the two {var} and {var} double-quote styles is faster)$outstr = 'literal' . $n . $data . $int . $data . $float . $n;
67048ms (38.2% slower)
$outstr = "literal$n$data$int$data$float$n";
49058ms (1.15% slower)
$outstr = "literal{$n}{$data}{$int}{$data}{$float}{$n}"
49221ms (1.49% slower)
$outstr = "literal${n}${data}${int}${data}${float}${n}"
48500ms (fastest; the differences are small but this held true across multiple runs of the test, and this was always the fastest variable encapsulation style)
1億回のテスト結果から,文字列と変数を併用した場合,"${var}"
の方がより良好な性能を示した..
の組合せ手法を用いて最低性能を示した.では、
"${var}"
は"{$var}"
や"$var"
より使いやすいのではないでしょうか.このような問題が発生する可能性があります.この投稿の著者は,テストで分かる点を整理した.
常に
"
を使用して文字列"This is a {$variable} notation"
, because it's the fastest method which still allows complex expansions like "This {$var['foo']} is {$obj->awesome()}!"
.You cannot do that with the "${var}"
style.変数は、
"This is a {$variable} notation"
のような複雑な拡張が必要な場合に最も速い方法であるため、"This {$var['foo']} is {$obj->awesome()}!"
とともに使用される."${var}"
というスタイルではそうはできません.キー/値、変数値などの純粋な文字列を使用する場合は、小引用符を使用することが望ましい.しかし1.55%の測定可能な差を見つけるためには10億回繰り返す必要がある.だから私が文字に引用符をつけた文字列を使う唯一の本当の原因はコードをきれいにするためです.
'this'.$var.'style'
is more readable, and you don't care about maximizing performance, then feel free to use whatever concatenation method you prefer!sprintf()や
'this'.$var. 'style'
などの他の方法では、接続を読みやすくしたり、接続を最大のパフォーマンスにしたくない場合は、接続方法を自由に選択できます."{$var} print"
で使用するのが最善の方法であるべきである.Reference
この問題について([HP]Stringの正しい使い方), 我々は、より多くの情報をここで見つけました https://velog.io/@wonjun3991/PHP-String의-올바른-활용법テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol