Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
02 - PHP I - OpenWebSchool
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
openwebschool
August 12, 2012
Programming
270
3
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
02 - PHP I - OpenWebSchool
openwebschool
August 12, 2012
More Decks by openwebschool
See All by openwebschool
11 - CodeIgniter - OpenWebSchool
openwebschool
1
370
09 - Node.JS - OpenWebSchool
openwebschool
1
400
07 - Javascript - OpenWebSchool
openwebschool
3
350
08 - js frontend & jQuery - OpenWebSchool
openwebschool
3
290
05 - MySQL - OpenWebSchool
openwebschool
1
260
06 - PHP & MySQL - OpenWebSchool
openwebschool
1
300
03 - PHP II - OpenWebSchool
openwebschool
2
400
04 - CSS - OpenWebSchool
openwebschool
4
360
01 - W3 intro - OpenWebSchool
openwebschool
3
260
Other Decks in Programming
See All in Programming
技術的負債を組織課題として解く-増えすぎたマイクロサービスとの戦い-
reimaru
1
1.7k
{ Android | Kotlin } Gradle Plugin in 2026
ryunen344
1
310
AIは賢い。でも実行環境は? CLIおじさんがAI時代に伝えたいこと ~ CLIおじさんがAI時代に伝えたいこと ~
curekoshimizu
1
240
AgentCore CLI で進化した AWS での AI エージェントの作り方 : 必要な機能を必要な時に
icoxfog417
PRO
3
340
Omarchy Tokyo やると聞いて UMPC 買ってセットアップしてきた
mtsmfm
0
140
iOSDC Japan 2026 - Swiftで作って学ぼう!データベース自作入門
kaseken
2
160
LLMは4年分のCompose移行を再現できるのか?実プロダクト279件のXMLで探る自動化の境界線
makun
0
870
海上で動くGoサーバー: goroutineとchannelでさばく航行データストリーム
atsuki_seo
0
450
AHC070解法紹介
eijirou
0
110
Are APIs Still Relevant in the AI Era?
soyuka
0
150
【高い買い物LT会】初任給で話題の国産フィジカルAIを買った話
akagami
PRO
0
160
数年滞っていたダークモード対応をおよそ2週間で完了させる
chigichan24
0
710
Featured
See All Featured
How GitHub (no longer) Works
holman
316
150k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
490
Amusing Abliteration
ianozsvald
1
300
Building Flexible Design Systems
yeseniaperezcruz
330
41k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.2k
The SEO identity crisis: Don't let AI make you average
varn
0
560
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
500
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.2k
Optimising Largest Contentful Paint
csswizardry
37
4k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.6k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
330
Paper Plane
katiecoart
PRO
4
53k
Transcript
PHP - part I Ensky / 林宏昱
Client – Server Recall HTTP Request HTTP Response + BODY(HTML)
Server Implement 1. 建立連線 (socket) ,等 client 進來 2. Client
進來 -> 分析 HTTP Request , 找出 URL 、 Host 、 Cookie 等等資訊 3. 根據上述資訊開始產生所需資料 4. 將產出的資料丟回 Client
Server Implement 1. 建立連線 (socket) ,等 client 進來 2. Client
進來 -> 分析 HTTP Request , 找出 URL 、 Host 、 Cookie 等等資訊 3. 根據上述資訊開始產生所需資料 4. 將產出的資料丟回 Client 拆出來!
Server Implement 1. 建立連線 (socket) ,等 client 進來 2. Client
進來 -> 分析 HTTP Request , 找出 URL 、 Host 、 Cookie 等等資訊 3. 根據上述資訊開始產生所需資料 4. 將產出的資料丟回 Client Web Server CGI
Server Implement Web server CGI HTTP Request stdin + env
stdout HTTP Response + BODY
CGI Implement include <iostream> using namespace std; int main ()
{ cout “<!doctype html>”; cout “<html>”; cout “ <head>”; ... 以下略 }
Any better choice?
We Save Your Time!
我今天要講的是…
HELLO WORLD! <?php echo “hello world!”; ?> OR hello world!
PHP is a programming language PHP 是某個人用 C 寫 CGI
寫到快吐血, 憤而寫出的程式語言 既然是程式語言,所有 C++ 、 JAVA 、 Python 、 … ,他們能做到的事情, PHP 基本上 都辦得到 你可以用它來寫 Web server 、 BBS 抓魚機器人、 Hadoop 程式、 NP 作業 …XD
PHP is a Interpreted language No need to compile, PHP
will compile then execute. 不用 compile 的意思是他會在「每次」 request 進來的時候 compile ,無論你有沒有改過那個 檔案。 – 很慢 所以我們通常會安裝一些快取 OP code 的外掛
PHP 的型態 • 基本型態如下 – Boolean ( True / False
) – Integer – Float – String (“abc” ‘cde’) • 複雜型態如下 – Array – Object
PHP 是個寬鬆型態的語言 • 變數在使用前不需宣告他的型態 $a = “this is a string”
• 變數會自動轉換型態 $a = “1”; //String $b = $a + 1; //Integer
PHP 是個寬鬆型態的語言 • 自動轉型好規好,有他的問題在 var_dump(“” == 0); // bool(true) var_dump(“0”
== 0); // bool(true) var_dump(“0” == “”); // bool(false) • 因此很多的時候我們會需要 「連型態一起判斷」的判斷式 === – var_dump(“” === 0); // bool(false) • 強制轉型的方法和 c++ 一樣,在此不多提
Variable Scope in PHP C++ 裡面的 scope for ( int
i = 1; i <= 5; i++ ) { do something… } cout << i << endl; // 這裡會錯,他會說 i 在這個 scope 裡面
Variable Scope in PHP PHP 裡面的 scope • in Global
最外面的變數都是 global 變數 • In function 在 function 內的變數都是 local 變數,沒有內 層 scope 。
Variable Scope in PHP 要取用 global 變數有兩種方法, 假設現在有 $a, $b
在 global 裡面 function I_want_to_use_global_var () { global $a; $a = ‘x’; $GLOBALS[‘b’] = ‘y’; }
Operator in PHP • 大家都會的 +, -, *, /, %,
++, -- <, <=, >, >=, ==, ===, !=, !== &&, || 其中, && 也可以寫成 AND, || 也可以寫成 OR • 字串連接用「 . 」 “this is a “ . “string”
Operator in PHP • 變數和字串的連接有幾種方式 $a = 123; $b =
“this is a number: ” . $a; $b = “this is a number: {$a}”; • 我個人比較偏好前一種,因為可以放運算 式。
String in PHP PHP 中,字串可以用單引號或雙引號包起來, 但兩者在 PHP 中意義不同 • 單引號包起來的字串,寫什麼就是什麼
$a = 123; echo ‘ $a is 123\n haha ’; // $a is 123\n haha • 雙引號包起來的字串,會幫你轉換變數、換行符號 等等 echo “ $a is 123\n haha ” // 123 is 123 // haha
Function in PHP • PHP 的 function 很直覺使用 function is_even
($n) { return $n % 2 == 0; } echo is_even(1); // 0 • PHP 的 function 也可以是個值 $is_even = function ($n) { return $n % 2 == 0; } echo $is_even(2); // 1
Function in PHP • PHP function 的參數可以有預設值 function print_something ($str=‘a’)
{ echo $str; } print_something(‘123’); // 123 print_something(); // a
Take a break
Array in PHP • 可以像你平常用的 array $scores = [60, 59,
70]; print_r($scores); /* Array ( [0] => 60 [1] => 59 [2] => 70 ) */ echo $scores[1]; // 59
Array in PHP • 可以當 queue 或 stack 來用,超爽 $scores
= [60, 100]; $scores[] = 71; // or, use array_push() // $scores = [60, 100, 71] array_unshift($scores, 80); // $scores = [80, 60, 100, 71] $first = array_shift($scores); // $first = 80, $scores = [60, 100, 71]
Array in PHP • 也可以當 hash table 來用,超爽 (dictionary in
python) $stu = [ “name” => “ensky”, “height” => “180”, “weight” => “65” ]; echo $stu[“name”]; // ensky
Array in PHP 有超多好用的 function 可以使用 • array_rand – 從
array 中隨機挑一個元素出來 • array_slice – 切割陣列 • array_unique – 把陣列中重複的元素去掉 • shuffle – 把陣列隨機排序 還有好多排序 function ,穩定排序、照 key 排序、 照 value 排序等等等 ….. • http://www.php.net/manual/en/ref.array.php
foreach in PHP • PHP 其他的流程控制都跟 c++ 很像,在此不 多提 (for,
while, do…while, switch, if, else …) 。 • 介紹一個比較特別的 operator foreach • 簡單來說,就是從一個陣列中, 把東西一個一個依序拿出來
foreach in PHP $scores = [60, 59, 58]; foreach ($scores
as $score) { echo $score . “ ”; } // will output 60 59 58 但我想知道他是第幾個元素,怎麼辦哩?
foreach in PHP $scores = [60, 59, 58]; foreach ($scores
as $index => $score) { echo $index . “:” . $score . “ ”; } // will output 0:60 1:59 2:58
foreach in PHP 同理,可以適用在 dictionary 的情況下 $person = [ “name”
=> “ensky”, “height” => 180 ] foreach ( $person as $key => $val ) { echo “{$key} => {$val}\n”; } // name => ensky // height => 180
include / require in PHP 在 C++ 的時代,我們會把一份 code 拆成
*.h 檔和 *.cpp 檔案,其中 *.h 每次 compile 都會一起 compile 進去。 而 *.cpp 則是預先 compile 完畢,再用 linker 將他 們連結在一起。 Header file 裡面只包含「定義」。 Source file 裡面是程式碼本身。
include / require in PHP 而 PHP 呢,因為變數、 class 不需要先編譯才會
執行,因此我們就不需要拆成 header file 和 source file ,直接全部 include 進來即可。 <?php include “funcs.php”; // OR require “funcs.php”;
include / require in PHP 然而,有幾個 issue 要注意 1. PHP
的 include 有分兩種,一種是 include , 另一種是 require ,差別在於,若該檔案找 不到, include 不會噴 error ,而 require 會。 一般情況下,你不會期待 include 一個檔案, 然後他找不到之後還繼續跑吧,所以我們 一般情況下會用 require 。
include / require in PHP 2. 目錄問題: 一般在 include 的時候,你很難確定到底你是
從哪裡開始 include 的,比方說: // index.php <?php require “func/func.php”; // in func/func.php require “haha.php”; // include func/haha.php 但此時你的路徑是 index.php 那層 不是 func 資料夾內,因此 haha.php 會找不到
include / require in PHP __DIR__ 是個神奇常數 (Magic constants) 他會指向檔案本身的目錄,例如說我的檔案
放在 /var/www/data/func/func.php 那麼 __DIR__ 的值就是 /var/www/data/func ref: http://php.net/manual/en/language.constants.predefined.php
include / require in PHP 因此我可以將剛剛的 code 改寫成 <?php require
__DIR__ . “/haha.php”; 如此一來,即使是 PATH 是在 index.php 執行的, 也不會找不到檔案。
include / require in PHP 3. 重複定義問題 如果 PHP 先宣告了某個
function ,之後再宣告 一次,就會出現重複定義錯誤 而使用 require 的話也是一樣,如果已經 require 過一個檔案,之後再 require 一次,則 會有重複定義問題。
include / require in PHP 此時,我們會用 require_once 來解決 <?php require_once
“funcs.php”; // 第二次不會作用, PHP 會自己判斷是否 require 過了 require_once “funcs.php”; 所以我其實最常用 require_once 。
Homework • 到 http://www.php.net/manual/en/langref.php 自修完 class 之前我沒講的部份 • 到 http://www.php.net/manual/en/book.array.php
去看看 array 有哪些 function 可以用
Homework • 實作題 II :計算機 算出 postfix 運算式的結果 ex: 345*+67*+89*+1+
= 138 Requirement: • 題目存在 $str 變數裡 • 答案直接 echo 出來 • 每個數字只有 1 位數 參考 function: • str_split • is_numeric • array_pop