Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
初心者必見!ダブルクォーテーションとシングルクォーテーションの違い言えますか?
1. CONFIDENTIAL All Contents Copyright and Copy 2013; evisu0414.com All rights reserved. 1
初心者必見!
あなたはダブルクォーテーションと
シングルクォーテーションの違い
言えますか?
担当:ヱビス
2. CONFIDENTIAL All Contents Copyright and Copy 2012; Tendrlinks .inc All rights reserved. 2
name: ヱビス(evisu0414)
job:Webプログラマ兼講師
hp:http://evisu0414.com
twitter:@evisu0414
3. CONFIDENTIAL All Contents Copyright and Copy 2012; Tendrlinks .inc All rights reserved. 3
知っているようで知らない
「ダブルクォーテーション」
と
「シングルクォーテーション」
違いがあるって知ってました?
4. CONFIDENTIAL All Contents Copyright and Copy 2012; Tendrlinks .inc All rights reserved. 4
違いなんてないよ( ・´ー・`)
5. CONFIDENTIAL All Contents Copyright and Copy 2012; Tendrlinks .inc All rights reserved. 5
検証してみよう その1
■ダブルクォーテーションを使った場合
<?php
echo “Hello PHP !!”;
?>
■表示結果
> Hello PHP !!
6. CONFIDENTIAL All Contents Copyright and Copy 2012; Tendrlinks .inc All rights reserved. 6
検証してみよう その2
■シングルクォーテーションを使った場合
<?php
echo ‘Hello PHP !!’;
?>
■表示結果
> Hello PHP !!
7. CONFIDENTIAL All Contents Copyright and Copy 2012; Tendrlinks .inc All rights reserved. 7
結果一緒じゃん!( ・´ー・`)
8. CONFIDENTIAL All Contents Copyright and Copy 2012; Tendrlinks .inc All rights reserved. 8
別のデータで検証してみよう
9. CONFIDENTIAL All Contents Copyright and Copy 2012; Tendrlinks .inc All rights reserved. 9
検証してみよう その3
■ダブルクォーテーションを使った場合
<?php
$hello = “Hello PHP !!”;
echo “$hello”;
?>
■表示結果
> Hello PHP !!
10. CONFIDENTIAL All Contents Copyright and Copy 2012; Tendrlinks .inc All rights reserved. 10
検証してみよう その4
■シングルクォーテーションを使った場合
<?php
$hello = “Hello PHP !!”;
echo ‘$hello’;
?>
■表示結果
> $hello
11. CONFIDENTIAL All Contents Copyright and Copy 2012; Tendrlinks .inc All rights reserved. 11
えぇ!結果が違う(´・ω・`)
12. CONFIDENTIAL All Contents Copyright and Copy 2012; Tendrlinks .inc All rights reserved. 12
何が違うの?
・ダブルクォーテーション
PHPの変数が展開できる
変数の内容を表示することができる
・シングルクォーテーション
PHPの変数名をそのまま表示してしまう
変数の内容を表示することができない
13. CONFIDENTIAL All Contents Copyright and Copy 2012; Tendrlinks .inc All rights reserved. 13
何が違うの?
・ダブルクォーテーション
表示される際に、PHP側でいろいろな処理が動き
変数などの内容を解析したのちに表示している
→ 処理数が多いため、少し遅い
・シングルクォーテーション
囲まれた文字列を、そのまま表示している
→ 処理数が少ないため、少し速い
14. CONFIDENTIAL All Contents Copyright and Copy 2012; Tendrlinks .inc All rights reserved. 14
使い分けポイント
・ダブルクォーテーション
文字列内で変数を使用する場合
例)変数を含む文字列、explode関数の引数など
・シングルクォーテーション
文字列内で変数を使用しない場合
例)配列の添字名や決め打ちの文字列など
15. CONFIDENTIAL All Contents Copyright and Copy 2012; Tendrlinks .inc All rights reserved. 15
explode関数で
ダブルクォーテーション?
16. CONFIDENTIAL All Contents Copyright and Copy 2012; Tendrlinks .inc All rights reserved. 16
こんな罠があります
■サンプルコード
<?php
$hello = “Hello PHP!!
Hello PHP!!!!”;
$array = explode(‘¥r¥n’, $hello);
var_dump($array);
?>
※改行コードはWindowsで使用される「¥r¥n」が
使用されているものとします
17. CONFIDENTIAL All Contents Copyright and Copy 2012; Tendrlinks .inc All rights reserved. 17
罠発動!
■実行結果
array(1) {
[0]=>
string(21) "Hello PHP!
Hello PHP!"
}
本来であれば、expload関数は「改行コード」で分割
を行うのですが、うまくいかない(´・ω・`)
18. CONFIDENTIAL All Contents Copyright and Copy 2012; Tendrlinks .inc All rights reserved. 18
どゆこと?(・_・)
19. CONFIDENTIAL All Contents Copyright and Copy 2012; Tendrlinks .inc All rights reserved. 19
文字コードが認識されない!
「シングルクォーテーション」は改行コードも
「¥r¥n」という文字列の羅列として表示してしまい
改行コードとしての効力を失ってしまいます
その他にも。preg系関数の「正規表現」も注意が
必要な項目の1つです
20. CONFIDENTIAL All Contents Copyright and Copy 2012; Tendrlinks .inc All rights reserved. 20
まとめ
何気なく使っている「ダブルクォーテーション」や
「シングルクォーテーション」も、使用方法を正確
に理解しておかないと泥沼に陥る可能性がありま
す!
「あれ?正常に動いていない」と思ったら、
この辺りを疑ってみるのも解決の糸口になるかもし
れませんね
21. CONFIDENTIAL All Contents Copyright and Copy 2012; Tendrlinks .inc All rights reserved. 21
ご清聴ありがとうございました
m(__)m