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
openwebschool
August 12, 2012
Programming
280
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
380
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
270
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
難しいけど、読めた。- OSSの入口に立った話。
sts11142
0
150
設計次第でAIコードの読む量は減らせる / designing-for-code-reading
minodriven
34
16k
外から家のPCの電源入れたい!をPHPで解決する
kawashimo0
1
130
速習iPhone Duo対応
yuukiw00w
2
980
GemmaをJevのように使ってみる / Use Gemma like Jev
kishida
5
770
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
640
contenteditable と日本語入力に向き合う
colorful12
0
190
Kotlinの15年とServer-Side Kotlin
n_takehata
1
100
個人開発者よ、Honoを使え(おじさんver)
tanahiro2010
2
250
Androidだけじゃない、DroidKaigiで広がった私の技術スタック
akkeylab
0
130
動作中のプログラムの中身をリアルタイムに覗く / Realtime Debugger for CSharp with Roslyn
prota
1
1.8k
[2026-09-26]空論ジェネリックプロセス~テスト資産とAIで紡ぐ、再現可能なパフォーマンスチューニングの話~
tosite
0
270
Featured
See All Featured
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.8k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
The SEO Collaboration Effect
kristinabergwall1
1
590
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
440
A designer walks into a library…
pauljervisheath
211
25k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Utilizing Notion as your number one productivity tool
mfonobong
4
620
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
2
410
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
74
43k
The Cult of Friendly URLs
andyhume
79
7k
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
68
58k
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