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
02 - PHP I - OpenWebSchool
Search
openwebschool
August 12, 2012
Programming
3
270
02 - PHP I - OpenWebSchool
openwebschool
August 12, 2012
Tweet
Share
More Decks by openwebschool
See All by openwebschool
11 - CodeIgniter - OpenWebSchool
openwebschool
1
350
09 - Node.JS - OpenWebSchool
openwebschool
1
390
07 - Javascript - OpenWebSchool
openwebschool
3
340
08 - js frontend & jQuery - OpenWebSchool
openwebschool
3
280
05 - MySQL - OpenWebSchool
openwebschool
1
250
06 - PHP & MySQL - OpenWebSchool
openwebschool
1
280
03 - PHP II - OpenWebSchool
openwebschool
2
390
04 - CSS - OpenWebSchool
openwebschool
4
360
01 - W3 intro - OpenWebSchool
openwebschool
3
250
Other Decks in Programming
See All in Programming
Feature Flags Suck! - KubeCon Atlanta 2025
phodgson
0
140
The Missing Link in Angular's Signal Story: Resource API and httpResource
manfredsteyer
PRO
0
130
PyCon mini 東海 2025「個人ではじめるマルチAIエージェント入門 〜LangChain × LangGraphでアイデアを形にするステップ〜」
komofr
3
1k
スタートアップを支える技術戦略と組織づくり
pospome
4
1.5k
Java_プロセスのメモリ監視の落とし穴_NMT_で見抜けない_glibc_キャッシュ問題_.pdf
ntt_dsol_java
0
210
AIを駆使して新しい技術を効率的に理解する方法
nogu66
1
630
チーム開発の “地ならし"
konifar
7
4.9k
Flutterアプリ運用の現場で役立った監視Tips 5選
ostk0069
1
460
AIと協働し、イベントソーシングとアクターモデルで作る後悔しないアーキテクチャ Regret-Free Architecture with AI, Event Sourcing, and Actors
tomohisa
2
950
Rails Girls Sapporo 2ndの裏側―準備の日々から見えた、私が得たもの / SAPPORO ENGINEER BASE #11
lemonade_37
2
170
Verilator + Rust + gRPC と Efinix の RISC-V でAIアクセラレータをAIで作ってる話 RTLを語る会(18) 2025/11/08
ryuz88
0
360
Private APIの呼び出し方
kishikawakatsumi
3
880
Featured
See All Featured
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
24
1.6k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
34
2.3k
Large-scale JavaScript Application Architecture
addyosmani
514
110k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
132
19k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
249
1.3M
A better future with KSS
kneath
239
18k
Mobile First: as difficult as doing things right
swwweet
225
10k
The World Runs on Bad Software
bkeepers
PRO
72
12k
A Modern Web Designer's Workflow
chriscoyier
697
190k
GitHub's CSS Performance
jonrohan
1032
470k
How to train your dragon (web standard)
notwaldorf
97
6.4k
The Language of Interfaces
destraynor
162
25k
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