SlideShare a Scribd company logo
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

PDF
Ch10 範例
PDF
PDF
Ch9 教學
PDF
Appendix B 範例
PDF
Ch10 教學
PDF
Appendix B 教學
PDF
Num py basic(1) - v01
PDF
Ppt 120-126
Ch10 範例
Ch9 教學
Appendix B 範例
Ch10 教學
Appendix B 教學
Num py basic(1) - v01
Ppt 120-126

What's hot (20)

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

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

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

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