SlideShare a Scribd company logo
1 of 23
Download to read offline
PHP 初階 - 課程三
Benson Lu
2014
Overview
function
Object-Oriented Programming
Built-in Function
strlen()
substr()
strtoupper()
strtolower()
strpos()
String 相關
length of string
substring
string to upper case
string to lower case
position of string
Built-in Function
array()
array_push()
count()
sort()
array_merge()
join()
Array 相關
declare array
push an element into array
count the elements in array
sort the elements
merge two array
output and separate into string
Built-in Function
round()
rand()
Math 相關
Round the number into integer
Random a number by given min
and max
Built-in Function
print_r()
var_dump()
die;
exit();
Debug 相關
print on screen
dump the type and its content
Die
exit
Exercise
1. 創⼀一個 array,push 三個名字進去
2. 將名稱排序印出來
3. Random 選出 Winner
4. 把 Winner 的名字⽤用英⽂文⼤大寫印出來
Note
有的要丟參數,有的不⽤用。
Note
有的有回傳值,有的沒有。
有的要丟參數,有的不⽤用。
Note
有的有回傳值,有的沒有。
有的要丟參數,有的不⽤用。
這中間,⼀一去⼀一回,⼀一定有東⻄西被完成了!
MAGIC!
Syntax
function name()
{
// Your code goes here
}
function 宣告後,即代表我們可以使⽤用。
但如果要讓 function 被執⾏行。
需要 call function
Local Scope
function name()
{
$myAge = 20;
echo $myData;
}
// Error
echo $myData;
function 裡宣告的變數,屬於 local scope。
意思是當 function 執⾏行結束,變數也會跟著被釋放。
Exercise
1. helloWorld()
2. greetings()
one params, two params
3. 加 減 乘 除
Object-Oriented
object
現實世界中的每個東⻄西,每⼀一個存在 都是⼀一種物件
物件
可以⽤用各種 屬性 以及 功能 來表達
⼈人 屬性:名字, ⾝身⾼高, 體重
功能:⾛走路,說話,思考
狗 屬性:名字, 顏⾊色, 體重
功能:跑,叫
Object-Oriented
class
類別
類別就是,我們在程式中⽤用來 表⽰示物件 的⽅方式
其中包含 properties 以及 methods
Class
<?php
class dog
{
public $name;
public function bark()
{
echo “woof”;
}
}
?>
property
method
class name
Class
Icon Created by Dimitry Sunseifer
from the Noun Project
class 只是⼀一個物件的藍圖、描述。
我們必須使⽤用這個物件,把物件 new 出來。
Class
usage
<?php
require(‘dog.php’);
$pet = new dog();
$pet->name = ‘wanwan’;
echo “My pet {$pet->name} says: ”;
$pet->bark();
?>
new 使⽤用⽅方式
Class
usage
<?php
require(‘dog.php’);
$pet = new dog();
$pet->name = ‘wanwan’;
echo “My pet {$pet->name} says: ”;
$pet->bark();
?>
new 使⽤用⽅方式
Class
usage
$this
class 內呼叫變數或⽅方法
<?php
class dog
{
public $name;
public function bark()
{
echo $this->name .“ says ‘woof’”;
}
}
?>
Constructor
__constructor()
new 出物件時,會⽴立即被執⾏行的⽅方法
<?php
class dog
{
public $name;
public __constructor($name)
{
$this->name = $name;
}
public function bark()
{
echo $this->name .“ says ‘woof’”;
}
}
?>
Exercise
1. 寫⼀一個 profiles 的物件
2. 包含⼀一個 array 存⼈人名 (string)
3. 寫⼀一個 __constructor 可以傳⼊入預設 array
4. 寫⼀一個 method 可以新增 profile
5. 寫⼀一個 method 印出所有 profile, ⽤用以下格式
Current Profile
———————
1. Benson
2. Sally
3. QQ
6. 寫⼀一個 method 印出 profile 總數
Thank You

More Related Content

What's hot (20)

Ppt 78-100
Ppt 78-100Ppt 78-100
Ppt 78-100
 
Sym py edu
Sym py eduSym py edu
Sym py edu
 
Ppt 1-50
Ppt 1-50Ppt 1-50
Ppt 1-50
 
Ppt 138-142
Ppt 138-142Ppt 138-142
Ppt 138-142
 
Math basic - v01
Math   basic - v01Math   basic - v01
Math basic - v01
 
Ch9 範例
Ch9 範例Ch9 範例
Ch9 範例
 
Ch7 教學
Ch7 教學Ch7 教學
Ch7 教學
 
Ppt 26-50
Ppt 26-50Ppt 26-50
Ppt 26-50
 
Ppt 120-126
Ppt 120-126Ppt 120-126
Ppt 120-126
 
Ch4 教學
Ch4 教學Ch4 教學
Ch4 教學
 
Ch11 教學
Ch11 教學Ch11 教學
Ch11 教學
 
Ch6 教學
Ch6 教學Ch6 教學
Ch6 教學
 
Ch2 教學
Ch2 教學Ch2 教學
Ch2 教學
 
Ch12 範例
Ch12 範例Ch12 範例
Ch12 範例
 
C程式-函式與巨集
C程式-函式與巨集C程式-函式與巨集
C程式-函式與巨集
 
Python入門:5大概念初心者必備 2021/11/18
Python入門:5大概念初心者必備 2021/11/18Python入門:5大概念初心者必備 2021/11/18
Python入門:5大概念初心者必備 2021/11/18
 
C程式-陣列與指標
C程式-陣列與指標C程式-陣列與指標
C程式-陣列與指標
 
C語言結構與串列
C語言結構與串列 C語言結構與串列
C語言結構與串列
 
Ch1 教學
Ch1 教學Ch1 教學
Ch1 教學
 
Ppt 167-173
Ppt 167-173Ppt 167-173
Ppt 167-173
 

Similar to PHP 初階課程 Part. 3 - Functions and brief intro to Object-Oriented PHP

Python入門:5大概念初心者必備
Python入門:5大概念初心者必備Python入門:5大概念初心者必備
Python入門:5大概念初心者必備Derek Lee
 
Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...
Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...
Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...彼得潘 Pan
 
Python学习笔记
Python学习笔记Python学习笔记
Python学习笔记Lingfei Kong
 
第3章算法与控制语句
第3章算法与控制语句第3章算法与控制语句
第3章算法与控制语句summerfeng
 
07 陣列與字串
07 陣列與字串07 陣列與字串
07 陣列與字串shademoon
 
第01章 绪论(java版)
第01章  绪论(java版)第01章  绪论(java版)
第01章 绪论(java版)Yan Li
 
ncuma_函式.pptx
ncuma_函式.pptxncuma_函式.pptx
ncuma_函式.pptxNCU MCL
 
JavaScript 快速複習 2017Q1
JavaScript 快速複習 2017Q1JavaScript 快速複習 2017Q1
JavaScript 快速複習 2017Q1Sheng-Han Su
 
第六章 函數與巨集
第六章 函數與巨集第六章 函數與巨集
第六章 函數與巨集shademoon
 
1 C入門教學
1  C入門教學1  C入門教學
1 C入門教學Sita Liu
 
Excel函數進階班(北市政府公訓處) 2
Excel函數進階班(北市政府公訓處) 2Excel函數進階班(北市政府公訓處) 2
Excel函數進階班(北市政府公訓處) 2terry28853669
 

Similar to PHP 初階課程 Part. 3 - Functions and brief intro to Object-Oriented PHP (20)

Python入門:5大概念初心者必備
Python入門:5大概念初心者必備Python入門:5大概念初心者必備
Python入門:5大概念初心者必備
 
Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...
Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...
Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...
 
Arduino程式快速入門
Arduino程式快速入門Arduino程式快速入門
Arduino程式快速入門
 
第5章数组
第5章数组第5章数组
第5章数组
 
Ch12
Ch12Ch12
Ch12
 
Python学习笔记
Python学习笔记Python学习笔记
Python学习笔记
 
第3章算法与控制语句
第3章算法与控制语句第3章算法与控制语句
第3章算法与控制语句
 
07 陣列與字串
07 陣列與字串07 陣列與字串
07 陣列與字串
 
第6章指针
第6章指针第6章指针
第6章指针
 
第01章 绪论(java版)
第01章  绪论(java版)第01章  绪论(java版)
第01章 绪论(java版)
 
第4章函数
第4章函数第4章函数
第4章函数
 
R intro 20140716-basic
R intro 20140716-basicR intro 20140716-basic
R intro 20140716-basic
 
Ch11
Ch11Ch11
Ch11
 
C語言標準輸出入函式
C語言標準輸出入函式C語言標準輸出入函式
C語言標準輸出入函式
 
Scala+RDD
Scala+RDDScala+RDD
Scala+RDD
 
ncuma_函式.pptx
ncuma_函式.pptxncuma_函式.pptx
ncuma_函式.pptx
 
JavaScript 快速複習 2017Q1
JavaScript 快速複習 2017Q1JavaScript 快速複習 2017Q1
JavaScript 快速複習 2017Q1
 
第六章 函數與巨集
第六章 函數與巨集第六章 函數與巨集
第六章 函數與巨集
 
1 C入門教學
1  C入門教學1  C入門教學
1 C入門教學
 
Excel函數進階班(北市政府公訓處) 2
Excel函數進階班(北市政府公訓處) 2Excel函數進階班(北市政府公訓處) 2
Excel函數進階班(北市政府公訓處) 2
 

PHP 初階課程 Part. 3 - Functions and brief intro to Object-Oriented PHP