Pro Yearly is on sale from $80 to $50! »

PHPCON_TOKYO_2019_Bigginer.pdf

 PHPCON_TOKYO_2019_Bigginer.pdf

PHPカンファレンス 2019 初心者セッション
2019/12/1 @大田区産業プラザPiO
スライドの一部に古いデータが入っていました。すいません。

267cdf0d184f715349459a0af9e6b0ff?s=128

Hideo Kashioka

December 01, 2019
Tweet

Transcript

  1. PHPΧϯϑΝϨϯε2019 PHPॳ৺ऀηογϣϯ #phpcon #Track5 ೔ຊPHPϢʔβձ ༗ݶձࣾΞϦ΢ʔϓ ദԬɹलஉ

  2. ΞδΣϯμ ࣗݾ঺հ PHPͷจ๏ ࣮ફฤ ͓·͚

  3. ࣗݾ঺հ ༗ݶձࣾΞϦ΢ʔϓɹऔక໾ࣾ௕ WEBγεςϜ։ൃɺνʔϜϏϧσΟϯά DevOps,CIͷಋೖίϯαϧςΟϯά εΫϥϜϚελʔ

  4. ର৅ͱͳΔօ༷ PHPॻ͍ͨ͜ͱແ͍ ೖ໳ॻಡΜ͚ͩͲɺ్தͰ࠳ં ଞͷݴޠͰೖ໳ఔ౓

  5. ҠಈλΠϜ

  6. PHPͱ͸

  7. PHPͱ͸ PHP is a popular general-purpose scripting language that is

    especially suited to web development. Fast, flexible and pragmatic, PHP powers everything from your blog to the most popular websites in the world. http://php.net/ΑΓ
  8. PHPͱ͸ WEB։ൃʹదͨ͠ݴޠ

  9. PHPͷྺ࢙ 1994೥ʹRasmus͕ΦϯϥΠϯͰެ։͍ͯ͠Δܦྺॻ ͷΞΫηεཤྺΛௐ΂ΔͨΊʹ࢖ΘΕ͍ͯ·ͨ͠ ൴͸ͦͷεΫϦϓτ܈ΛʮPersonal Home Page Toolsʯͱ໊෇͚·ͨ͠ɻ http://php.net/manual/ja/history.php.php

  10. PHPͷsoftware

  11. EC-CUBE͸גࣜձࣾΠʔγʔΩϡʔϒͷ঎ඪͰ͢

  12. PHPͷ؀ڥ

  13. LAMP LAPP LEMP Linux Apache MySQL PHP Linux Apache Postgre

    PHP Linux Nginx MySQL PHP ݺͼํ͸ OS+Web Server+Database+Script WAMPͱ͔LAMRɺMERNͱ͔΋ݕࡧͰ͸͋Γ·ͨ͠ɻ
  14. ͲΜͳ؀ڥͰ͸͡ΊΔ͔

  15. ϗεςΟϯά Ϩϯλϧαʔό

  16. AWS Azure GCP ͦͷଞ

  17. ϩʔΧϧ XAMPP MAMP

  18. aws cloud9 ͔ͭͯ͸ಠཱͨ͠αʔϏεɻ ݱࡏ͸AWSʹ౷߹͞Εͨɻ ౦ژϦʔδϣϯ͸αϙʔτ͞Ε͍ͯͳ͍ AWSͷ஌͕ࣝඞཁ ϦϞʔταʔό΁΋઀ଓͰ͖Δ http://c9.io

  19. Docker https://www.docker.com/ Windows,MacରԠ

  20. Docker https://hub.docker.com/_/php/

  21. docker compose ෳ਺ͷίϯςφΛ؅ཧͰ͖Δɻ https://docs.docker.com/compose/gettingstarted/

  22. DockerʹΑΔ։ൃ؀ڥ docker-composeΛ࢖༻͢Δ৔߹ dockerͷΠϯετʔϧ docker-compose.ymlͷ࡞੒ dockerͷىಈ

  23. docker-compose.yml version: '3' services: php: image: php:7.4.0-fpm-alpine3.10 expose: - '8080'

    ports: - '8080:80' volumes: - ./phpapps:/home/www-data working_dir: /home/www-data command: php -S 0.0.0.0:80 docker Πϝʔδ໊ ެ։ϙʔτ ϗετଆͱίϯς φͷϙʔτ ϘϦϡʔϜͷϚ΢ϯτ ίϚϯυͷ࣮ߦ
 ϏϧτΠϯαʔό
  24. σΟϨΫτϦߏ੒ docker-compose.yml apps —>>͕͜͜dockerͱڞ༗͞Ε͍ͯΔʢϏϧτΠ ϯαʔόͷσΟϨΫτϦʣ ϩʔΧϧͷϑΝΠϧΛमਖ਼͢ΔͱdockerଆͰ࣮ߦՄ ೳ

  25. ىಈํ๏ ର৅σΟϨΫτϦʹҠಈͯ͠ docker-compose up -d ։࢝ docker-compose stopऴྃ

  26. ࣮ߦͯ͠Έ·͠ΐ͏ɻ index.phpΛ࡞੒ͯ֬͠ೝͯ͠Έ·͠ΐ͏ http://localhost:8888

  27. None
  28. ؀ڥ͸͙͢ʹͰ͖·͢ɻ ࢼͯ͠Έ·͠ΐ͏ɻ ※ձ৔಺Ͱͷμ΢ϯϩʔυ͸͝ԕྀ͍ͩ͘͞

  29. PHPͷ֓ཁ

  30. PHPͷجຊ PHPͷจ๏͸௚ײతͰ͢ɻ ଞͷݴޠΛ΍͍ͬͯͳ͍ํ͕ށ࿭͍·ͤΜɻ WEBߏஙʹ࠷దͳݴޠͰ͢ɻ ࣌ʹ࣮຿Ͱ࢖͏ʹ͸৭ʑग़͖ͯ·͢ ※ݸਓͷײ૝Ͱ͢

  31. PHPλά <?php ?>ɹPHPϞʔυ; HTMLͷλάͷΑ͏ͳΠϝʔδ;

  32. <html> <body> <?php echo "͜Μʹͪ͸"; ?> </body> </html> Ұ൪͔ΜͨΜͳαϯϓϧ http://localhost:8888/1st.php

  33. <html> <body> <?php echo "͜Μʹͪ͸"; ?> </body> </html> Ұ൪͔ΜͨΜͳαϯϓϧ http://localhost:8888/1st.php

  34. ֦ுࢠ ϑΝΠϧ໊.php

  35. ߏจ ม਺͸$Ͱ࢝·Δ; จ຤͸;ͰऴΘΔ ܕએݴͳͲ͸ݫີʹߦΘͳͯ͘OK͕ͩޙʑ ໘౗…… ؆୯ͳαϯϓϧΛݟͯΈ·͠ΐ͏

  36. <?php $a = '͜Μʹͪ͸'; echo $a;

  37. PHP͔ΒHTML΋ग़ྗ

  38. <?php echo "<html>"; echo "<body>"; echo "͜Ε͸PHPͰ͢"; echo "</body>"; echo

    "</html>"; http://localhost:8888/sample0.php
  39. $a = “A” . “B”; $b = 1 + 2;

    μϒϧίʔςʔγϣϯ͸ด͡ͳͯ͘OK ଍ͨ͠Γ݁߹ͨ͠Γ
  40. $a = “A”; $b = “B”; $c = $a .

    $b; ม਺஋͕਺ࣈͳΒԋࢉ΋Մೳ ม਺Ͱ΋݁߹
  41. $a = "phpcon"; $b = 2017; echo $a . ($b

    + 2); http://localhost:8888/sample2.php
  42. PHPͷؔ਺ if foreach strlen htmlspecialchars nl2br mb_convert_encording

  43. if ʙ else (endif) ৚݅൑ఆΛߦ͏ ৚݅Λຬͨ͢ຬͨ͞ͳ͍ͷ൑அΛߦ͏

  44. <?php if($a) { echo '৚݅Λຬͨ͢'; } else { echo '৚݅Λຬͨ͞ͳ͍';

    }
  45. <div> <?php if($a): ?> <div id='a'>ຬͨ͢</div> <?php else: ?> <div

    id=''>ຬͨ͞ͳ͍</div> <?php endif; ?> </div>
  46. ͲΜͳ࣌ʹ࢖͏͔ ೖྗ஋͕༧૝ͱҧ͏ औಘͨ͠஋ʹΑͬͯॲཧΛ੾Γ෼͚Δ

  47. HTML૊ΈࠐΈܕͷαϯϓϧ WordPressͷ৔߹ wp-content/themes/twentynineteen/ header.php

  48. ܁Γฦ͠ॲཧ

  49. foreach <?php $numbers = array('2','3','4','5','6'); foreach($numbers as $val) { echo

    $val; } http://localhost:8080/sample5.php
  50. htmlspecialchars ˍ"'<>ͱ͍ͬͨHTMLͰҙຯ͋ΔจࣈΛhtmlͷදࣔܗࣜʹม ׵͢Δ = javascriptɺෆਖ਼ͳsql࣮ߦ౳ʹΑΔηΩϡϦςΟʔϦεΫΛ ճආ͢Δɻ ೖྗ͞Εͨσʔλ͸ग़ྗ͢Δ௚લʹඞͣhtmlspecialchars Λ௨͢ ϑϨʔϜϫʔΫΛ࢖͍ͬͯͯ΋֬ೝ͠·͠ΐ͏ɻ

  51. htmlspecialchars <?php $new ="<a href='test'>Test</a>"; echo $new; <?php $new ="<a

    href='test'>Test</a>"; echo htmlspecialchars($new,ENT_QUOTES); http://localhost:8888/sample3.php HTML͕ղऍ͞ΕΔ
  52. ϑΝΠϧͷಡΈࠐΈ htmlͷྔ࢈ʹ΋ศར

  53. require_once <?php require_once('header.html'); require_once('body.php'); require_once('footer.html'); http://localhost:8888/require.php

  54. ஋Λऔಘ͢Δ ೖྗϑΥʔϜ͔Βೖྗ͞Εͨ஋ $_POST http://localhost:8888/get.php?id=123456 $_GET Կ͕ೖͬͯ͘Δ͔Θ͔Βͳ͍ͷͰར༻·Ͱʹ஫ҙ͢Δʂ όϦσʔγϣϯ΍DBઐ༻ͷΤεέʔϓॲཧɺϓϦϖΞʔυε ςʔτϝϯτͳͲ

  55. ஋ͷऔಘ <?php echo $_GET['id']; echo $_POST['id']; ࣮ࡍ͸htmlspecialchars()ͨ͠Γ

  56. ஋ͷ֬ೝ var_dump() http://localhost:8888/get.php?id=1

  57. ؔ਺ ܁Γฦ͞ΕΔॲཧΛڞ௨Խ function ؔ਺໊([Ҿ਺,,,]) { } ͳΜͲ΋ग़ͯ͘ΔॲཧΛؔ਺Խ͓ͯ͘͠ɻ

  58. ؔ਺αϯϓϧ function hsp($str) { return htmlspecialchars($str,ENT_QUOTES); } function add1($num =

    0) { return $num + 1; }
  59. ͜Ε͘Β͍Θ͔Ε͹OK ࣍͸Ϋϥεͷѻ͍ͳͲΛษڧ͠·͠ΐ͏ PHPͷΦϯϥΠϯϚχϡΞϧͷॆ࣮౓͕ੌ͍ PHPͷྺ࢙ ݴޠϦϑΝϨϯε

  60. ࣍ͷҰา

  61. PHPΞϓϦέʔγϣϯΛ Πϯετʔϧͯ͠ΈΔ WordPress Drupal EC-Cube Xoops

  62. ϑϨʔϜϫʔΫΛ ࢖ͬͯΈΑ͏ Laraval CakePHP Symphony

  63. ࣮ફฤ CSVϑΝΠϧΛಡΈࠐΜͰදࣔ͢ΔϓϩάϥϜΛ࡞Δ

  64. ࡐྉ ։ൃ؀ڥ ͓޷͖ͳΤσΟλ php.net ϚχϡΞϧ CSVϑΝΠϧ

  65. CSVΛಡΈࠐΉ ؔ਺໊͕Θ͔Βͳ͚Ε͹ͱΓ͋͑ͣݕࡧ

  66. fgetcsvͬͯͷ͕ݟ͔ͭͬͨ http://php.net/manual/ja/function.fgetcsv.php ͕Μ͹ͬͯಡΉͱΘ͔Δ Θ͔Βͳ͚Ε͹αϯϓϧΛ

  67. php.net ΑΓ

  68. php.net ΑΓ

  69. ϚχϡΞϧͰௐ΂ͯΈΑ͏ Θ͔Βͳ͍ؔ਺Λӈ্ʹೖΕͯΈΔɻ

  70. ྫΛಡΜͰΈΔ fopen — ϑΝΠϧ·ͨ͸ URL ΛΦʔϓϯ͢Δ whileϧʔϓ͸ɺPHPͰ࠷΋؆୯ͳλΠϓͷϧʔϓͰ͢ɻ count — ม਺ʹؚ·ΕΔ͢΂ͯͷཁૉɺ

    ͋Δ͍͸Φϒ δΣΫτʹؚ·ΕΔԿ͔ͷ਺Λ਺͑Δ fclose — Φʔϓϯ͞ΕͨϑΝΠϧϙΠϯλΛΫϩʔζ͢Δ ͜ΕΛ౿·͑ͯ
  71. ͳΜͱͳ͘Θ͔ͬͨ

  72. αϯϓϧΛ࢖ͬͯΈΔ ಈ͔ͯ͠ΈΔ http://localhost:8080/sample1/list.php Warning: fopen(test.csv): failed to open stream: No

    such file or directory in /home/www-data/sample1/ list.php on line 3 Τϥʔ͕ͰΔͷͰcsvϑΝΠϧΛஔ͍ͯΈ·͠ΐ͏ɻ
  73. CSVσʔλΛ࡞Δ "෦԰","λΠτϧ","։࢝࣌ࠁ","࣌ؒ" "ക","LaravelͰTDDΛମݧ͠Α͏","11:00","60" "ക","PHP,Go,ElasticsearchʹΑΔɺ@cosmeΛ5ഒ଎͘͢ΔऔΓ૊Έ","13:00","25" "ക","ΞʔΩςΫνϟઃܭͱUXઃܭ͸ಉ͡ͳͷ͔!!??","13:35","25" "ക","Laravel × ϨΠϠʔυΞʔΩςΫνϟΛ࣮ફͯ͠ಘΒΕͨ஌ݟͱ൓ল","14:00","25" "᳉","ಠཱͨ͠ίΞϨΠϠύλʔϯʹΑΔ PHP

    ΞϓϦέʔγϣϯͷ࣮૷","11:00","60" "᳉","VOYAGEͷΤϯδχΞධՁ੍౓ͬͯͲΜͳײ͡ͳͷ͔25෼Ͱϛχ࠶ԋ","13:00","25" "᳉","PHPΛબͿཧ༝","13:35","25" test.csv
  74. ϑΝΠϧΛஔ͍࣮ͯߦͯ͠Έ·͠ΐ͏

  75. None
  76. ςʔϒϧͰදࣔͯ͠Έ·͠ΐ ͏ɻ

  77. ςʔϒϧͷߏ଄ ಉ͡Α͏ͳ෦෼ΛϧʔϓͰදࣔͤ͞Δ

  78. None
  79. ৭ʑௐ੔ͯ͠ΈΑ͏ දࣔ͢Δ߲໨Λબ୒͢Δ ද߲ࣔ໨ʹ͸htmlspecialchars()Λ͔͚Δ ߜΓࠐΈΛೖΕΔ http://localhost:8080/sample2/list.php

  80. αϯϓϧιʔε

  81. <h1>ίϯϕϯγϣϯϗʔϧηογϣϯ</h1> ߜΓࠐΈɿ<a href="list2.php?room=᳉">᳉</a> <a href="list2.php?room=ക">ക</a> <table border=1> <?php if (($handle

    = fopen("timetable2.csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { if ($_GET['room'] === $data[0] ) { echo "<tr>"; echo "<td>". hsc($data[0])."</td>\n"; echo "<td>". hsc($data[1])."</td>\n"; echo "<td>". hsc($data[2])."</td>\n"; echo "<td>". hsc($data[3])."෼</td>\n"; echo "</tr>"; } } fclose($handle); } ?> </table> <?php function hsc($str) { return htmlspecialchars($str,ENT_QUOTES); } ?>
  82. None
  83. ϚχϡΞϧͱ஥ྑ͘ͳΖ͏

  84. ࠙਌ձ ଞͷ։ൃऀ΍εϐʔΧʔͱ௚઀࿩͢νϟϯε!

  85. ͓קΊॻ੶ ʮ͍ͪ͹Μ΍͍͞͠PHPͷڭຊʯࢲ͕ॻ͍͍ͯ·͢ɻ ͍·ԿΛ࡞͍ͬͯΔͷ͔Λߟ͑Δ

  86. ·ͱΊ PHPͷ؀ڥΛ࡞ͬͯΈ·͠ΐ͏ɻ PHPΞϓϦέʔγϣϯΛΠϯετʔϧͯ͠Έ·͠ΐ͏ ؆୯ͳϓϩάϥϜΛॻ͍ͯΈ·͠ΐ͏ɻ ϑϨʔϜϫʔΫͷνϡʔτϦΞϧΛͯ͠Έ·͠ΐ͏ɻ ΞϓϦͷΧελϚΠζΛͯ͠Έ·͠ΐ͏ɻ

  87. ͥͻ͓ࢼ͍ͩ͘͠͞ʂ

  88. ͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ɻ ΧϯϑΝϨϯεΛָ͠ΜͰ͍ͩ͘͞ʂ ໌೔ͷ։ൃΧϯϑΝϨϯε ΞεΧϯτʔΫφΠτɹ ΞεΧϯτʔΫφΠτͰݕࡧʂ 12/6։࠵༧ఆʂ

  89. PHPॳ৺ऀηογϣϯ ༗ݶձࣾΞϦ΢ʔϓ ദԬɹलஉ twitter,facebook @kashioka

  90. None