Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
PHP初心者セッション2017
Search
Hideo Kashioka
October 08, 2017
Technology
1
3.9k
PHP初心者セッション2017
PHPを始めたばかりの人向けセッションです。今年は推奨環境をDockerにしています。
Hideo Kashioka
October 08, 2017
Tweet
Share
More Decks by Hideo Kashioka
See All by Hideo Kashioka
PHP初心者セッション〜ChatGPTでゼロから動くプログラムを生み出そう!〜
kashioka
0
65
PHPCON_TOKYO_2019_Bigginer.pdf
kashioka
0
940
ここからはじめるDevOps
kashioka
0
95
DevOpsへの準備 便利なサービスで開発効率アップ -開発のためのSaaS利用の実際-
kashioka
0
260
どこからはじめるDevOps
kashioka
0
130
PHPカンファレンス2016 初心者セッション
kashioka
3
8.4k
Other Decks in Technology
See All in Technology
Fabric 移行時の躓きポイントと対応策
ohata_ds
1
120
20240513 - 框裡框外_文學院學生如何在AI世代安身立命 @ 淡江大學
dpys
0
600
Oracle Cloud Infrastructure:2024年12月度サービス・アップデート
oracle4engineer
PRO
1
520
エンジニアリングマネージャー視点での、自律的なスケーリングを実現するFASTという選択肢 / RSGT2025
yoshikiiida
4
2.1k
実践! ソフトウェアエンジニアリングの価値の計測 ── Effort、Output、Outcome、Impact
nomuson
0
730
Visual StudioとかIDE関連小ネタ話
kosmosebi
1
120
20241228 - 成為最強魔法使!AI 實時生成比賽的策略 @ 2024 SD AI 年會
dpys
0
330
事業貢献を考えるための技術改善の目標設計と改善実績 / Targeted design of technical improvements to consider business contribution and improvement performance
oomatomo
0
250
Fearsome File Formats
ange
0
550
AIエージェントに脈アリかどうかを分析させてみた
sonoda_mj
2
130
PHPerのための計算量入門/Complexity101 for PHPer
hanhan1978
6
1.5k
サーバーなしでWordPress運用、できますよ。
sogaoh
PRO
0
170
Featured
See All Featured
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.9k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
29
940
Learning to Love Humans: Emotional Interface Design
aarron
274
40k
It's Worth the Effort
3n
183
28k
Thoughts on Productivity
jonyablonski
68
4.4k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
160
15k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
3
220
Measuring & Analyzing Core Web Vitals
bluesmoon
5
190
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
44
9.3k
The Invisible Side of Design
smashingmag
299
50k
Designing Experiences People Love
moore
139
23k
YesSQL, Process and Tooling at Scale
rocio
170
14k
Transcript
PHPΧϯϑΝϨϯε2017 PHPॳ৺ऀηογϣϯ ˌphpcon2017_4 ຊPHPϢʔβձ ༗ݶձࣾΞϦʔϓ ദԬɹलஉ
ΞδΣϯμ ࣗݾհ PHPͷจ๏ ࣮ફฤ ͓·͚
ࣗݾհ ༗ݶձࣾΞϦʔϓ WEBγεςϜ։ൃɾDevOpsͷಋೖίϯαϧςΟϯά
PHPͱ
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/ΑΓ
PHPͱ WEB։ൃʹదͨ͠ݴޠ
PHPͷྺ࢙ 1994ʹRasmus͕ΦϯϥΠϯͰެ։͍ͯ͠Δܦྺॻ ͷΞΫηεཤྺΛௐΔͨΊʹΘΕ͍ͯ·ͨ͠ ൴ͦͷεΫϦϓτ܈ΛʮPersonal Home Page Toolsʯͱ໊͚·ͨ͠ɻ http://php.net/manual/ja/history.php.php
PHPͷڥ
LAMP LAPP LEMP Linux Apache MySQL PHP Linux Apache Postgre
PHP Linux Nginx MariaDB PHP ݺͼํ OS+Web Server+Database+Script WAMPͱ͔LAMRͱ͔ݕࡧͰ͋Γ·ͨ͠ɻ
ͲΜͳڥͰ͡ΊΔ͔
ϗεςΟϯά Ϩϯλϧαʔό
AWS Microsoft Azure Google App Engine IBM Bluemix
Ծڥ Vmware VirtualBox Docker
खܰʹ͡ΊΔͳΒ XAMPP MAMP https://www.apachefriends.org/ https://www.mamp.info/
αʔόʔ͍Βͣ cloud9 http://c9.io https://ide.c9.io/kashioka/test
Docker https://www.docker.com/ Windows,MacରԠ
Docker https://hub.docker.com/_/php/
docker compose ෳͷίϯςφΛཧͰ͖Δɻ https://docs.docker.com/compose/gettingstarted/
ਪ։ൃڥ docker-composeΛ༻͢Δ߹ dockerͷΠϯετʔϧ docker-compose.ymlͷ࡞ dockerͷىಈ
docker-compose.yml version: '3' services: php: image: php:7.1-alpine expose: - '8080'
ports: - '8080:80' volumes: - ./apps:/home/www-data working_dir: /home/www-data command: php -S 0.0.0.0:80 docker Πϝʔδ໊ ެ։ϙʔτ ϗετଆͱίϯς φͷϙʔτ ϘϦϡʔϜͷϚϯτ ίϚϯυͷ࣮ߦ ϏϧτΠϯαʔό
σΟϨΫτϦ docker-compose.yml apps —>>͕͜͜dockerͱڞ༗͞Ε͍ͯΔʢϏϧτΠ ϯαʔόͷσΟϨΫτϦʣ ϩʔΧϧͷϑΝΠϧΛमਖ਼͢ΔͱdockerଆͰ࣮ߦՄ ೳ
ىಈํ๏ ରσΟϨΫτϦʹҠಈͯ͠ docker-compose up -d ։࢝ docker-compose stop ऴྃ
࣮ߦͯ͠ΈΔ
ڥ͙͢ʹͰ͖·͢ɻ ࢼͯ͠Έ·͠ΐ͏ɻ ※ձͰͷμϯϩʔυ͝ԕྀ͍ͩ͘͞
PHPͷ֓ཁ
PHPͷجຊ PHPͷจ๏ײతͰ͢ɻ ଞͷݴޠΛ͍ͬͯͳ͍ํ͕ށ͍·ͤΜɻ WEBߏஙʹ࠷దͳݴޠͰ͢ɻ ࣌ʹ࣮Ͱ͏ʹ৭ʑग़͖ͯ·͢ ※ݸਓͷײͰ͢
PHPλά <?php ?>ɹPHPϞʔυ; HTMLͷλάͷΑ͏ͳΠϝʔδ;
<html> <body> <?php echo "͜Μʹͪ"; ?> </body> </html> Ұ൪͔ΜͨΜͳαϯϓϧ http://localhost:8888/1st.php
֦ுࢠ ϑΝΠϧ໊.php
ߏจ ม$Ͱ࢝·Δ; จ;ͰऴΘΔ ܕએݴͳͲݫີʹߦΘͳͯ͘OK͕ͩޙʑ ໘…… ؆୯ͳαϯϓϧΛݟͯΈ·͠ΐ͏
<?php $a = '͜Μʹͪ'; echo $a;
PHP͔ΒHTMLग़ྗ
<?php echo "<html>"; echo "<body>"; echo "͜ΕPHPͰ͢"; echo "</body>"; echo
"</html>"; http://localhost:8888/sample0.php
݁߹ͨ͠Γܭࢉͨ͠Γ .(υοτʣΛͬͯจࣈྻͷ݁߹͕Ͱ͖ ·͢ɻ ͷԋࢉ+-*/ php7·ͰจࣈྻͷࣈͰ͠ࢉͰ ͖͍ͯͨɻ
<?php $a = 1; $b = 2; echo $a +
$b; echo '<br>'; echo $a . $b; http://localhost:8888/sample4.php
<?php $a = "phpcon"; $b = 2016; echo $a .
($b + 1); http://localhost:8888/sample2.php
PHPͷؔ εΫϦϓτݴޠͷؔ͠Έ͍͢ ͳΜͱͳؔ͘Λ͍֮͑ͯΕOK if foreach strlen htmlspecialchars nl2br mb_convert_encording
if ʙ else (endif) ݅ఆΛߦ͏ ݅Λຬͨ͢ຬͨ͞ͳ͍ͷஅΛߦ͏
<?php if($a) { echo '݅Λຬͨ͢'; } else { echo '݅Λຬͨ͞ͳ͍';
} http://localhost:8888/if.php
<div> <?php if($a): ?> <div id='a'>ຬͨ͢</div> <?php else: ?> <div
id=''>ຬͨ͞ͳ͍</div> <?php endif; ?> </div> http://localhost:8888/div.php
HTMLΈࠐΈܕͷαϯϓϧ Wordpressͷ߹ wp-content/themes/twentyseventeen/ header.php
foreach <?php $numbers = array('2','3','4','5','6'); foreach($numbers as $val) { echo
$val; } http://localhost:8888/sample5.php
htmlspecialchars ˍ"'<>ͱ͍ͬͨHTMLͰҙຯ͋ΔจࣈΛhtmlͷදࣔܗ ࣜʹม͢Δ = javascriptɺෆਖ਼ͳsql࣮ߦʹΑΔηΩϡϦςΟʔϦ εΫΛճආ͢Δɻ ೖྗ͞Εͨσʔλग़ྗ͢Δલʹඞͣ htmlspecialcharsΛ௨͢
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ͷྔ࢈ʹศར
require_once <?php require_once('header.html'); require_once('body.php'); require_once('footer.html'); http://localhost:8888/require.php
Λऔಘ͢Δ ೖྗϑΥʔϜ͔Βೖྗ͞Εͨ $_POST http://localhost:8888/get.php?id=123456 $_GET
ͷऔಘ <?php echo $_GET['id']; echo $_POST['id'];
ͷ֬ೝ var_dump()
ؔ ܁Γฦ͞ΕΔॲཧΛڞ௨Խ function ໊ؔ([Ҿ,,,]) { } ͳΜͲग़ͯ͘ΔॲཧΛؔԽ͓ͯ͘͠ɻ
ؔαϯϓϧ function hsp($str) { return htmlspecialchars($str,ENT_QUOTES); } function add1($num =
0) { return $num + 1; }
͜Ε͘Β͍Θ͔ΕOK PHPͷΦϯϥΠϯϚχϡΞϧͷॆ࣮͕ੌ͍ PHPͷྺ࢙ ݴޠϦϑΝϨϯε
αϯϓϧΞϓϦέʔγϣϯ
࣮ફฤ λΠϜςʔϒϧΛͭ͘Δ
CSVσʔλΛ࡞Δ ϓϩάϥϜͰѻ͏ͨΊʹCSV(ΧϯϚ۠Γ)TSV(λ ϒ۠Γ)ͱ͍ͬͨσʔλ͕खؒΛল͚Δ ExcelͰ࡞Մೳ
Tips MacͷExcelͷ߹վߦίʔυͷม͕ඞཁɻ Ұ୴file_get_contentsͳͲͰશ෦ಡΈࠐΜͰਖ਼نදݱͰ͔͑Δ ͳͲ ViίϚϯυϥΠϯͰมͯ͠ྑ͍ :1,$ s/^M/¥r/g ͳͲ^Mctl+vͷޙʹctr+mͰೖྗ
CSVΛಡΈࠐΉ ໊͕ؔΘ͔Βͳ͚ΕͱΓ͋͑ͣݕࡧ
fgetcsvͬͯͷ͕ݟ͔ͭͬͨ http://php.net/manual/ja/function.fgetcsv.php ͕ΜͬͯಡΉͱΘ͔Δ Θ͔Βͳ͚ΕαϯϓϧΛ
αϯϓϧΛͬͯΈΔ ಈ͔ͯ͠ΈΔ http://localhost:8888/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Λࠩ͠ସ͑ΔͱͲ͏ͳΔ͔
ϚχϡΞϧͰௐͯΈΑ͏ Θ͔Βͳ͍ؔΛӈ্ʹೖΕͯΈΔɻ
ௐͯΈΔͱ fopen — ϑΝΠϧ·ͨ URL ΛΦʔϓϯ͢Δ whileϧʔϓɺPHPͰ࠷؆୯ͳλΠϓͷϧʔϓͰ͢ɻ count — มʹؚ·ΕΔͯ͢ͷཁૉɺ
͋Δ͍Φϒ δΣΫτʹؚ·ΕΔԿ͔ͷΛ͑Δ fclose — Φʔϓϯ͞ΕͨϑΝΠϧϙΠϯλΛΫϩʔζ͢Δ ͜ΕΛ౿·͑ͯ
ͳΜͱͳ͘Θ͔ͬͨ
ςʔϒϧͰදࣔͯ͠Έ·͠ΐ͏ɻ
ςʔϒϧͷߏ ಉ͡Α͏ͳ෦ΛϧʔϓͰදࣔͤ͞Δ
None
None
৭ʑௐͯ͠ΈΑ͏ ߲Λ̐ͭදࣔ͢Δ ද߲ࣔʹhtmlspecialchars()Λ͔͚Δ http://localhost:8888/sample2/list.php
αϯϓϧιʔε
<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); } ?>
ϚχϡΞϧͱྑ͘ͳΖ͏
֤Ͱ։࠵͞Ε͍ͯΔPHPΧ ϯϑΝϨϯε
࠙ձ ଞͷ։ൃऀεϐʔΧʔͱ͢νϟϯε!
͓קΊॻ੶ ʮ͍ͪΜ͍͞͠PHPͷڭຊʯࢲ͕ॻ͍͍ͯ·͢ɻ ͍·ԿΛ࡞͍ͬͯΔͷ͔Λߟ͑Δ
·ͱΊ খ͍͞ͷͰ͍͍ͷͰॻ͍ͯݟ·͠ΐ͏ɻ ϚχϡΞϧ͕ศརͰ͢ɻ
ͱʹ͔͘ॻ͍ͯΈΔʂ Ҏ্
͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ɻ ΧϯϑΝϨϯεΛָ͠ΜͰ͍ͩ͘͞ʂ ໌ͷ։ൃΧϯϑΝϨϯε ˌ͔͋͢Μɹ 20184݄։࠵༧ఆʂ
PHPॳ৺ऀηογϣϯ ༗ݶձࣾΞϦʔϓ ദԬɹलஉ twitter,facebook @kashioka
None