SlideShare a Scribd company logo
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,   Web
   找出URL、Host、Cookie等等資訊         Server
3. 根據上述資訊開始產生所需資料
                                  CGI
4. 將產出的資料丟回Client
Server Implement

             HTTP Request


HTTP         Web server     stdin + env
Response
+ BODY
               stdout         CGI
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 123n haha ’;
// $a is 123n haha

• 雙引號包起來的字串,會幫你轉換變數、換行符號
  等等
echo “ $a is 123n 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

More Related Content

What's hot

部分PHP问题总结[转贴]
部分PHP问题总结[转贴]部分PHP问题总结[转贴]
部分PHP问题总结[转贴]wensheng wei
 
深入淺出 Web 容器 - Tomcat 原始碼分析
深入淺出 Web 容器  - Tomcat 原始碼分析深入淺出 Web 容器  - Tomcat 原始碼分析
深入淺出 Web 容器 - Tomcat 原始碼分析
Justin Lin
 
新北市教師工作坊 -- Bash script programming 介紹
新北市教師工作坊 -- Bash script programming 介紹新北市教師工作坊 -- Bash script programming 介紹
新北市教師工作坊 -- Bash script programming 介紹
fweng322
 
Ruby程式語言入門導覽
Ruby程式語言入門導覽Ruby程式語言入門導覽
Ruby程式語言入門導覽
Mu-Fan Teng
 
Erlang Practice
Erlang PracticeErlang Practice
Erlang Practicelitaocheng
 
Patterns in Zend Framework
Patterns in Zend FrameworkPatterns in Zend Framework
Patterns in Zend FrameworkJace Ju
 
OpenWebSchool - 03 - PHP Part II
OpenWebSchool - 03 - PHP Part IIOpenWebSchool - 03 - PHP Part II
OpenWebSchool - 03 - PHP Part IIHung-yu Lin
 
Perl 6 news at 2010-06
Perl 6 news at 2010-06Perl 6 news at 2010-06
Perl 6 news at 2010-06March Liu
 
Introduction to Parse JavaScript SDK
Introduction to Parse JavaScript SDKIntroduction to Parse JavaScript SDK
Introduction to Parse JavaScript SDK維佋 唐
 
The Evolution of Async Programming (GZ TechParty C#)
The Evolution of Async Programming (GZ TechParty C#)The Evolution of Async Programming (GZ TechParty C#)
The Evolution of Async Programming (GZ TechParty C#)jeffz
 
Perl在nginx里的应用
Perl在nginx里的应用Perl在nginx里的应用
Perl在nginx里的应用
琛琳 饶
 
Javascript Training
Javascript TrainingJavascript Training
Javascript Training
beijing.josh
 
Python 于 webgame 的应用
Python 于 webgame 的应用Python 于 webgame 的应用
Python 于 webgame 的应用
勇浩 赖
 
Phpconf 2011 introduction_to_codeigniter
Phpconf 2011 introduction_to_codeigniterPhpconf 2011 introduction_to_codeigniter
Phpconf 2011 introduction_to_codeigniter
Bo-Yi Wu
 
PHPUnit 入門介紹
PHPUnit 入門介紹PHPUnit 入門介紹
PHPUnit 入門介紹
Jace Ju
 
Bash入门基础篇
Bash入门基础篇Bash入门基础篇
Bash入门基础篇Zhiyao Pan
 
页游开发中的 Python 组件与模式
页游开发中的 Python 组件与模式页游开发中的 Python 组件与模式
页游开发中的 Python 组件与模式
勇浩 赖
 
OpenEJB - 另一個選擇
OpenEJB - 另一個選擇OpenEJB - 另一個選擇
OpenEJB - 另一個選擇
Justin Lin
 

What's hot (20)

部分PHP问题总结[转贴]
部分PHP问题总结[转贴]部分PHP问题总结[转贴]
部分PHP问题总结[转贴]
 
深入淺出 Web 容器 - Tomcat 原始碼分析
深入淺出 Web 容器  - Tomcat 原始碼分析深入淺出 Web 容器  - Tomcat 原始碼分析
深入淺出 Web 容器 - Tomcat 原始碼分析
 
新北市教師工作坊 -- Bash script programming 介紹
新北市教師工作坊 -- Bash script programming 介紹新北市教師工作坊 -- Bash script programming 介紹
新北市教師工作坊 -- Bash script programming 介紹
 
Ooredis
OoredisOoredis
Ooredis
 
Ruby程式語言入門導覽
Ruby程式語言入門導覽Ruby程式語言入門導覽
Ruby程式語言入門導覽
 
Erlang Practice
Erlang PracticeErlang Practice
Erlang Practice
 
Patterns in Zend Framework
Patterns in Zend FrameworkPatterns in Zend Framework
Patterns in Zend Framework
 
OpenWebSchool - 03 - PHP Part II
OpenWebSchool - 03 - PHP Part IIOpenWebSchool - 03 - PHP Part II
OpenWebSchool - 03 - PHP Part II
 
Perl 6 news at 2010-06
Perl 6 news at 2010-06Perl 6 news at 2010-06
Perl 6 news at 2010-06
 
Introduction to Parse JavaScript SDK
Introduction to Parse JavaScript SDKIntroduction to Parse JavaScript SDK
Introduction to Parse JavaScript SDK
 
The Evolution of Async Programming (GZ TechParty C#)
The Evolution of Async Programming (GZ TechParty C#)The Evolution of Async Programming (GZ TechParty C#)
The Evolution of Async Programming (GZ TechParty C#)
 
Perl在nginx里的应用
Perl在nginx里的应用Perl在nginx里的应用
Perl在nginx里的应用
 
Php
PhpPhp
Php
 
Javascript Training
Javascript TrainingJavascript Training
Javascript Training
 
Python 于 webgame 的应用
Python 于 webgame 的应用Python 于 webgame 的应用
Python 于 webgame 的应用
 
Phpconf 2011 introduction_to_codeigniter
Phpconf 2011 introduction_to_codeigniterPhpconf 2011 introduction_to_codeigniter
Phpconf 2011 introduction_to_codeigniter
 
PHPUnit 入門介紹
PHPUnit 入門介紹PHPUnit 入門介紹
PHPUnit 入門介紹
 
Bash入门基础篇
Bash入门基础篇Bash入门基础篇
Bash入门基础篇
 
页游开发中的 Python 组件与模式
页游开发中的 Python 组件与模式页游开发中的 Python 组件与模式
页游开发中的 Python 组件与模式
 
OpenEJB - 另一個選擇
OpenEJB - 另一個選擇OpenEJB - 另一個選擇
OpenEJB - 另一個選擇
 

Similar to OpenWebSchool - 02 - PHP Part I

PHP 語法基礎與物件導向
PHP 語法基礎與物件導向PHP 語法基礎與物件導向
PHP 語法基礎與物件導向
Shengyou Fan
 
[PHP 也有 Day #64] PHP 升級指南
[PHP 也有 Day #64] PHP 升級指南[PHP 也有 Day #64] PHP 升級指南
[PHP 也有 Day #64] PHP 升級指南
Shengyou Fan
 
Migrations 與 Schema 操作
Migrations 與 Schema 操作Migrations 與 Schema 操作
Migrations 與 Schema 操作
Shengyou Fan
 
OpenWebSchool - 01 - WWW Intro
OpenWebSchool - 01 - WWW IntroOpenWebSchool - 01 - WWW Intro
OpenWebSchool - 01 - WWW IntroHung-yu Lin
 
Learning python in the motion picture industry by will zhou
Learning python in the motion picture industry   by will zhouLearning python in the motion picture industry   by will zhou
Learning python in the motion picture industry by will zhou
Will Zhou
 
Migrations 與 Schema操作
Migrations 與 Schema操作Migrations 與 Schema操作
Migrations 與 Schema操作
Shengyou Fan
 
[COSCUP 2022] 讓黑畫面再次偉大 - 用 PHP 寫 CLI 工具
[COSCUP 2022] 讓黑畫面再次偉大 - 用 PHP 寫 CLI 工具[COSCUP 2022] 讓黑畫面再次偉大 - 用 PHP 寫 CLI 工具
[COSCUP 2022] 讓黑畫面再次偉大 - 用 PHP 寫 CLI 工具
Shengyou Fan
 
JavaScript 80+ Programming and Optimization Skills
JavaScript 80+ Programming and Optimization SkillsJavaScript 80+ Programming and Optimization Skills
JavaScript 80+ Programming and Optimization Skills
Ho Kim
 
Python 入门
Python 入门Python 入门
Python 入门kuco945
 
第三方内容开发最佳实践
第三方内容开发最佳实践第三方内容开发最佳实践
第三方内容开发最佳实践taobao.com
 
DevOpsDays Taipei 2018 - Puppet 古早味、新感受:改造老牌企業進入自動化時代
DevOpsDays Taipei 2018 - Puppet 古早味、新感受:改造老牌企業進入自動化時代DevOpsDays Taipei 2018 - Puppet 古早味、新感受:改造老牌企業進入自動化時代
DevOpsDays Taipei 2018 - Puppet 古早味、新感受:改造老牌企業進入自動化時代
scott liao
 
Schema & Migration操作
Schema & Migration操作Schema & Migration操作
Schema & Migration操作
Shengyou Fan
 
Puppet安装测试
Puppet安装测试Puppet安装测试
Puppet安装测试Yiwei Ma
 
模块一-Go语言特性.pdf
模块一-Go语言特性.pdf模块一-Go语言特性.pdf
模块一-Go语言特性.pdf
czzz1
 
[2]futurewad樹莓派研習會 141127
[2]futurewad樹莓派研習會 141127[2]futurewad樹莓派研習會 141127
[2]futurewad樹莓派研習會 141127
CAVEDU Education
 
Phalcon the fastest php framework 阿土伯
Phalcon   the fastest php framework 阿土伯Phalcon   the fastest php framework 阿土伯
Phalcon the fastest php framework 阿土伯Hash Lin
 
Phalcon phpconftw2012
Phalcon phpconftw2012Phalcon phpconftw2012
Phalcon phpconftw2012
Rack Lin
 
[Modern Web 2016] 讓你的 PHP 開發流程再次潮起來
[Modern Web 2016] 讓你的 PHP 開發流程再次潮起來[Modern Web 2016] 讓你的 PHP 開發流程再次潮起來
[Modern Web 2016] 讓你的 PHP 開發流程再次潮起來
Shengyou Fan
 
课题一:PHP5.3、PHP5.4的特性介绍与深度挖掘
课题一:PHP5.3、PHP5.4的特性介绍与深度挖掘课题一:PHP5.3、PHP5.4的特性介绍与深度挖掘
课题一:PHP5.3、PHP5.4的特性介绍与深度挖掘Liu Allen
 
Php for fe
Php for fePhp for fe
Php for fe
jay li
 

Similar to OpenWebSchool - 02 - PHP Part I (20)

PHP 語法基礎與物件導向
PHP 語法基礎與物件導向PHP 語法基礎與物件導向
PHP 語法基礎與物件導向
 
[PHP 也有 Day #64] PHP 升級指南
[PHP 也有 Day #64] PHP 升級指南[PHP 也有 Day #64] PHP 升級指南
[PHP 也有 Day #64] PHP 升級指南
 
Migrations 與 Schema 操作
Migrations 與 Schema 操作Migrations 與 Schema 操作
Migrations 與 Schema 操作
 
OpenWebSchool - 01 - WWW Intro
OpenWebSchool - 01 - WWW IntroOpenWebSchool - 01 - WWW Intro
OpenWebSchool - 01 - WWW Intro
 
Learning python in the motion picture industry by will zhou
Learning python in the motion picture industry   by will zhouLearning python in the motion picture industry   by will zhou
Learning python in the motion picture industry by will zhou
 
Migrations 與 Schema操作
Migrations 與 Schema操作Migrations 與 Schema操作
Migrations 與 Schema操作
 
[COSCUP 2022] 讓黑畫面再次偉大 - 用 PHP 寫 CLI 工具
[COSCUP 2022] 讓黑畫面再次偉大 - 用 PHP 寫 CLI 工具[COSCUP 2022] 讓黑畫面再次偉大 - 用 PHP 寫 CLI 工具
[COSCUP 2022] 讓黑畫面再次偉大 - 用 PHP 寫 CLI 工具
 
JavaScript 80+ Programming and Optimization Skills
JavaScript 80+ Programming and Optimization SkillsJavaScript 80+ Programming and Optimization Skills
JavaScript 80+ Programming and Optimization Skills
 
Python 入门
Python 入门Python 入门
Python 入门
 
第三方内容开发最佳实践
第三方内容开发最佳实践第三方内容开发最佳实践
第三方内容开发最佳实践
 
DevOpsDays Taipei 2018 - Puppet 古早味、新感受:改造老牌企業進入自動化時代
DevOpsDays Taipei 2018 - Puppet 古早味、新感受:改造老牌企業進入自動化時代DevOpsDays Taipei 2018 - Puppet 古早味、新感受:改造老牌企業進入自動化時代
DevOpsDays Taipei 2018 - Puppet 古早味、新感受:改造老牌企業進入自動化時代
 
Schema & Migration操作
Schema & Migration操作Schema & Migration操作
Schema & Migration操作
 
Puppet安装测试
Puppet安装测试Puppet安装测试
Puppet安装测试
 
模块一-Go语言特性.pdf
模块一-Go语言特性.pdf模块一-Go语言特性.pdf
模块一-Go语言特性.pdf
 
[2]futurewad樹莓派研習會 141127
[2]futurewad樹莓派研習會 141127[2]futurewad樹莓派研習會 141127
[2]futurewad樹莓派研習會 141127
 
Phalcon the fastest php framework 阿土伯
Phalcon   the fastest php framework 阿土伯Phalcon   the fastest php framework 阿土伯
Phalcon the fastest php framework 阿土伯
 
Phalcon phpconftw2012
Phalcon phpconftw2012Phalcon phpconftw2012
Phalcon phpconftw2012
 
[Modern Web 2016] 讓你的 PHP 開發流程再次潮起來
[Modern Web 2016] 讓你的 PHP 開發流程再次潮起來[Modern Web 2016] 讓你的 PHP 開發流程再次潮起來
[Modern Web 2016] 讓你的 PHP 開發流程再次潮起來
 
课题一:PHP5.3、PHP5.4的特性介绍与深度挖掘
课题一:PHP5.3、PHP5.4的特性介绍与深度挖掘课题一:PHP5.3、PHP5.4的特性介绍与深度挖掘
课题一:PHP5.3、PHP5.4的特性介绍与深度挖掘
 
Php for fe
Php for fePhp for fe
Php for fe
 

More from Hung-yu Lin

2014 database - course 2 - php
2014 database - course 2 - php2014 database - course 2 - php
2014 database - course 2 - phpHung-yu Lin
 
2014 database - course 3 - PHP and MySQL
2014 database - course 3 - PHP and MySQL2014 database - course 3 - PHP and MySQL
2014 database - course 3 - PHP and MySQLHung-yu Lin
 
2014 database - course 1 - www introduction
2014 database - course 1 - www introduction2014 database - course 1 - www introduction
2014 database - course 1 - www introductionHung-yu Lin
 
OpenWebSchool - 11 - CodeIgniter
OpenWebSchool - 11 - CodeIgniterOpenWebSchool - 11 - CodeIgniter
OpenWebSchool - 11 - CodeIgniterHung-yu Lin
 
OpenWebSchool - 06 - PHP + MySQL
OpenWebSchool - 06 - PHP + MySQLOpenWebSchool - 06 - PHP + MySQL
OpenWebSchool - 06 - PHP + MySQLHung-yu Lin
 
OpenWebSchool - 05 - MySQL
OpenWebSchool - 05 - MySQLOpenWebSchool - 05 - MySQL
OpenWebSchool - 05 - MySQLHung-yu Lin
 
Dremel: interactive analysis of web-scale datasets
Dremel: interactive analysis of web-scale datasetsDremel: interactive analysis of web-scale datasets
Dremel: interactive analysis of web-scale datasetsHung-yu Lin
 
Google App Engine
Google App EngineGoogle App Engine
Google App Engine
Hung-yu Lin
 
Redis
RedisRedis

More from Hung-yu Lin (9)

2014 database - course 2 - php
2014 database - course 2 - php2014 database - course 2 - php
2014 database - course 2 - php
 
2014 database - course 3 - PHP and MySQL
2014 database - course 3 - PHP and MySQL2014 database - course 3 - PHP and MySQL
2014 database - course 3 - PHP and MySQL
 
2014 database - course 1 - www introduction
2014 database - course 1 - www introduction2014 database - course 1 - www introduction
2014 database - course 1 - www introduction
 
OpenWebSchool - 11 - CodeIgniter
OpenWebSchool - 11 - CodeIgniterOpenWebSchool - 11 - CodeIgniter
OpenWebSchool - 11 - CodeIgniter
 
OpenWebSchool - 06 - PHP + MySQL
OpenWebSchool - 06 - PHP + MySQLOpenWebSchool - 06 - PHP + MySQL
OpenWebSchool - 06 - PHP + MySQL
 
OpenWebSchool - 05 - MySQL
OpenWebSchool - 05 - MySQLOpenWebSchool - 05 - MySQL
OpenWebSchool - 05 - MySQL
 
Dremel: interactive analysis of web-scale datasets
Dremel: interactive analysis of web-scale datasetsDremel: interactive analysis of web-scale datasets
Dremel: interactive analysis of web-scale datasets
 
Google App Engine
Google App EngineGoogle App Engine
Google App Engine
 
Redis
RedisRedis
Redis
 

OpenWebSchool - 02 - PHP Part I

  • 1. PHP - part I Ensky / 林宏昱
  • 2. Client – Server Recall HTTP Request HTTP Response + BODY(HTML)
  • 3. Server Implement 1. 建立連線(socket),等client進來 2. Client進來 -> 分析HTTP Request, 找出URL、Host、Cookie等等資訊 3. 根據上述資訊開始產生所需資料 4. 將產出的資料丟回Client
  • 4. Server Implement 1. 建立連線(socket),等client進來 2. Client進來 -> 分析HTTP Request, 拆出來! 找出URL、Host、Cookie等等資訊 3. 根據上述資訊開始產生所需資料 4. 將產出的資料丟回Client
  • 5. Server Implement 1. 建立連線(socket),等client進來 2. Client進來 -> 分析HTTP Request, Web 找出URL、Host、Cookie等等資訊 Server 3. 根據上述資訊開始產生所需資料 CGI 4. 將產出的資料丟回Client
  • 6. Server Implement HTTP Request HTTP Web server stdin + env Response + BODY stdout CGI
  • 7. CGI Implement include <iostream> using namespace std; int main () { cout “<!doctype html>”; cout “<html>”; cout “ <head>”; ...以下略 }
  • 9. We Save Your Time!
  • 11. HELLO WORLD! <?php echo “hello world!”; ?> OR hello world!
  • 12. PHP is a programming language PHP是某個人用C寫CGI寫到快吐血, 憤而寫出的程式語言 既然是程式語言,所有C++、JAVA、 Python、…,他們能做到的事情,PHP基本上 都辦得到 你可以用它來寫Web server、BBS抓魚機器人、 Hadoop程式、NP作業…XD
  • 13. PHP is a Interpreted language No need to compile, PHP will compile then execute. 不用compile的意思是他會在「每次」request 進來的時候compile,無論你有沒有改過那個 檔案。 – 很慢 所以我們通常會安裝一些快取OP code的外掛
  • 14. PHP 的型態 • 基本型態如下 – Boolean ( True / False ) – Integer – Float – String (“abc” ‘cde’) • 複雜型態如下 – Array – Object
  • 15. PHP 是個寬鬆型態的語言 • 變數在使用前不需宣告他的型態 $a = “this is a string” • 變數會自動轉換型態 $a = “1”; //String $b = $a + 1; //Integer
  • 16. PHP 是個寬鬆型態的語言 • 自動轉型好規好,有他的問題在 var_dump(“” == 0); // bool(true) var_dump(“0” == 0); // bool(true) var_dump(“0” == “”); // bool(false) • 因此很多的時候我們會需要 「連型態一起判斷」的判斷式 === – var_dump(“” === 0); // bool(false) • 強制轉型的方法和c++一樣,在此不多提
  • 17. Variable Scope in PHP C++裡面的scope for ( int i = 1; i <= 5; i++ ) { do something… } cout << i << endl; //這裡會錯,他會說i在這個scope裡面
  • 18. Variable Scope in PHP PHP裡面的scope • in Global 最外面的變數都是global 變數 • In function 在function內的變數都是local變數,沒有內 層scope。
  • 19. Variable Scope in PHP 要取用global變數有兩種方法, 假設現在有$a, $b在global裡面 function I_want_to_use_global_var () { global $a; $a = ‘x’; $GLOBALS[‘b’] = ‘y’; }
  • 20. Operator in PHP • 大家都會的 +, -, *, /, %, ++, -- <, <=, >, >=, ==, ===, !=, !== &&, || 其中,&&也可以寫成AND, ||也可以寫成OR • 字串連接用「.」 “this is a “ . “string”
  • 21. Operator in PHP • 變數和字串的連接有幾種方式 $a = 123; $b = “this is a number: ” . $a; $b = “this is a number: {$a}”; • 我個人比較偏好前一種,因為可以放運算 式。
  • 22. String in PHP PHP中,字串可以用單引號或雙引號包起來, 但兩者在PHP中意義不同 • 單引號包起來的字串,寫什麼就是什麼 $a = 123; echo ‘ $a is 123n haha ’; // $a is 123n haha • 雙引號包起來的字串,會幫你轉換變數、換行符號 等等 echo “ $a is 123n haha ” // 123 is 123 // haha
  • 23. 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
  • 24. Function in PHP • PHP function 的參數可以有預設值 function print_something ($str=‘a’) { echo $str; } print_something(‘123’); // 123 print_something(); // a
  • 26. Array in PHP • 可以像你平常用的array $scores = [60, 59, 70]; print_r($scores); /* Array ( [0] => 60 [1] => 59 [2] => 70 ) */ echo $scores[1]; // 59
  • 27. 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]
  • 28. Array in PHP • 也可以當hash table來用,超爽 (dictionary in python) $stu = [ “name” => “ensky”, “height” => “180”, “weight” => “65” ]; echo $stu[“name”]; // ensky
  • 29. Array in PHP 有超多好用的function可以使用 • array_rand – 從array中隨機挑一個元素出來 • array_slice – 切割陣列 • array_unique – 把陣列中重複的元素去掉 • shuffle – 把陣列隨機排序 還有好多排序function,穩定排序、照key排序、 照value排序等等等….. • http://www.php.net/manual/en/ref.array.php
  • 30. foreach in PHP • PHP其他的流程控制都跟c++很像,在此不 多提(for, while, do…while, switch, if, else …)。 • 介紹一個比較特別的operator foreach • 簡單來說,就是從一個陣列中, 把東西一個一個依序拿出來
  • 31. foreach in PHP $scores = [60, 59, 58]; foreach ($scores as $score) { echo $score . “ ”; } // will output 60 59 58 但我想知道他是第幾個元素,怎麼辦哩?
  • 32. foreach in PHP $scores = [60, 59, 58]; foreach ($scores as $index => $score) { echo $index . “:” . $score . “ ”; } // will output 0:60 1:59 2:58
  • 33. foreach in PHP 同理,可以適用在dictionary的情況下 $person = [ “name” => “ensky”, “height” => 180 ] foreach ( $person as $key => $val ) { echo “{$key} => {$val}n”; } // name => ensky // height => 180
  • 34. include / require in PHP 在C++的時代,我們會把一份code拆成*.h檔和 *.cpp檔案,其中*.h每次compile都會一起 compile進去。 而*.cpp則是預先compile完畢,再用linker將他 們連結在一起。 Header file裡面只包含「定義」。 Source file裡面是程式碼本身。
  • 35. include / require in PHP 而PHP呢,因為變數、class不需要先編譯才會 執行,因此我們就不需要拆成header file和 source file,直接全部include進來即可。 <?php include “funcs.php”; // OR require “funcs.php”;
  • 36. include / require in PHP 然而,有幾個issue要注意 1. PHP的include有分兩種,一種是include, 另一種是require,差別在於,若該檔案找 不到,include不會噴error,而require會。 一般情況下,你不會期待include一個檔案, 然後他找不到之後還繼續跑吧,所以我們 一般情況下會用require。
  • 37. 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會找不到
  • 38. 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
  • 39. include / require in PHP 因此我可以將剛剛的code改寫成 <?php require __DIR__ . “/haha.php”; 如此一來,即使是PATH是在index.php執行的, 也不會找不到檔案。
  • 40. include / require in PHP 3. 重複定義問題 如果PHP先宣告了某個function,之後再宣告 一次,就會出現重複定義錯誤 而使用require的話也是一樣,如果已經 require過一個檔案,之後再require一次,則 會有重複定義問題。
  • 41. include / require in PHP 此時,我們會用require_once來解決 <?php require_once “funcs.php”; // 第二次不會作用,PHP會自己判斷是否require過了 require_once “funcs.php”; 所以我其實最常用require_once。
  • 42. Homework • 到http://www.php.net/manual/en/langref.php 自修完class之前我沒講的部份 • 到 http://www.php.net/manual/en/book.array.php 去看看array有哪些function可以用
  • 43. Homework • 實作題 II:計算機 算出postfix運算式的結果 ex: 345*+67*+89*+1+ = 138 Requirement: • 題目存在$str變數裡 • 答案直接echo出來 • 每個數字只有1位數 參考function: • str_split • is_numeric • array_pop