Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PHP 基礎 Part 3.

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.
Avatar for bblurock bblurock
December 17, 2014

PHP 基礎 Part 3.

Today's lecture will cover the usage of PHP built-in functions and custom defined function. In addition to basic Object-Oriented Programming in PHP.

Avatar for bblurock

bblurock

December 17, 2014
Tweet

More Decks by bblurock

Other Decks in Programming

Transcript

  1. Built-in Function strlen() substr() strtoupper() strtolower() strpos() String 相關 length

    of string substring string to upper case string to lower case position of string
  2. Built-in Function array() array_push() count() sort() 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
  3. Built-in Function round() rand() Math 相關 Round the number into

    integer Random a number by given min and max
  4. Syntax function name() { // Your code goes here }

    function 宣告後,即代表我們可以使⽤用。 但如果要讓 function 被執⾏行。 需要 call function
  5. Local Scope function name() { $myAge = 20; echo $myData;

    } // Error echo $myData; function 裡宣告的變數,屬於 local scope。 意思是當 function 執⾏行結束,變數也會跟著被釋放。
  6. Object-Oriented object 現實世界中的每個東⻄西,每⼀一個存在 都是⼀一種物件 物件 可以⽤用各種 屬性 以及 功能 來表達

    ⼈人 屬性:名字, ⾝身⾼高, 體重 功能:⾛走路,說話,思考 狗 屬性:名字, 顏⾊色, 體重 功能:跑,叫
  7. Class <?php class dog { public $name; public function bark()

    { echo “woof”; } } ?> property method class name
  8. Class Icon Created by Dimitry Sunseifer from the Noun Project

    class 只是⼀一個物件的藍圖、描述。 我們必須使⽤用這個物件,把物件 new 出來。
  9. Class usage <?php require(‘dog.php’); $pet = new dog(); $pet->name =

    ‘wanwan’; echo “My pet {$pet->name} says: ”; $pet->bark(); ?> new 使⽤用⽅方式
  10. Class usage <?php require(‘dog.php’); $pet = new dog(); $pet->name =

    ‘wanwan’; echo “My pet {$pet->name} says: ”; $pet->bark(); ?> new 使⽤用⽅方式
  11. Class usage $this class 內呼叫變數或⽅方法 <?php class dog { public

    $name; public function bark() { echo $this->name .“ says ‘woof’”; } } ?>
  12. Constructor __constructor() new 出物件時,會⽴立即被執⾏行的⽅方法 <?php class dog { public $name;

    public __constructor($name) { $this->name = $name; } public function bark() { echo $this->name .“ says ‘woof’”; } } ?>
  13. 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 總數