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
PHPCON_TOKYO_2019_Bigginer.pdf
kashioka
0
920
ここからはじめるDevOps
kashioka
0
94
DevOpsへの準備 便利なサービスで開発効率アップ -開発のためのSaaS利用の実際-
kashioka
0
250
どこからはじめるDevOps
kashioka
0
130
PHPカンファレンス2016 初心者セッション
kashioka
3
8.4k
Other Decks in Technology
See All in Technology
TypeScript、上達の瞬間
sadnessojisan
46
13k
Making your applications cross-environment - OSCG 2024 NA
salaboy
0
180
SSMRunbook作成の勘所_20241120
koichiotomo
2
130
個人でもIAM Identity Centerを使おう!(アクセス管理編)
ryder472
3
200
rootlessコンテナのすゝめ - 研究室サーバーでもできる安全なコンテナ管理
kitsuya0828
3
380
Taming you application's environments
salaboy
0
180
TanStack Routerに移行するのかい しないのかい、どっちなんだい! / Are you going to migrate to TanStack Router or not? Which one is it?
kaminashi
0
580
オープンソースAIとは何か? --「オープンソースAIの定義 v1.0」詳細解説
shujisado
6
670
AGIについてChatGPTに聞いてみた
blueb
0
130
Why does continuous profiling matter to developers? #appdevelopercon
salaboy
0
180
ExaDB-D dbaascli で出来ること
oracle4engineer
PRO
0
3.8k
Oracle Cloud Infrastructureデータベース・クラウド:各バージョンのサポート期間
oracle4engineer
PRO
28
12k
Featured
See All Featured
Into the Great Unknown - MozCon
thekraken
32
1.5k
It's Worth the Effort
3n
183
27k
A Tale of Four Properties
chriscoyier
156
23k
For a Future-Friendly Web
brad_frost
175
9.4k
Six Lessons from altMBA
skipperchong
27
3.5k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Designing for Performance
lara
604
68k
Code Reviewing Like a Champion
maltzj
520
39k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.3k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
120
Intergalactic Javascript Robots from Outer Space
tanoku
269
27k
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