Basic
常數 constant
define("GREETING", "Welcome to W3Schools.com!");
echo GREETING;
switch
// switch + case + break + default
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
Loop
While loop
// 先assign一個變數 然後再去做事
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
Do while loop
// 無論發生啥事 會先做do的內容 然後再跑迴圈
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
For loop
// 用分號隔開
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
Array
Sorting 排序方法
sort() - sort arrays in ascending order
rsort() - sort arrays in descending order
asort() - sort associative arrays in ascending order, according to the value
ksort() - sort associative arrays in ascending order, according to the key
arsort() - sort associative arrays in descending order, according to the value
krsort() - sort associative arrays in descending order, according to the key
/*
a 為 value
k 為 key
r 是 降序
- 是 升序
*/
Superglobals
// 列出常用的
$GLOBALS
$_SERVER // 如果要取得server相關的資料 比如說ip/host server/之類的話可以用
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION
PHP advanced
Date
date(format,timestamp)
// format Required. Specifies the format of the timestamp
// timestamp Optional. Specifies a timestamp. Default is the current date and time
include and require
// require 會在錯誤時停掉 但 include不會
// require will produce a fatal error (E_COMPILE_ERROR) and stop the script
// include will only produce a warning (E_WARNING) and the script will continue
<html>
<body>
<h1>Welcome to my home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>
<?php include 'footer.php';?>
</body>
</html>
file handling
PHP Read File – fread()
PHP Open File – fopen()
PHP Close File – fclose()
PHP Write to File – fwrite()
Cookie
setcookie(name, value, expire, path, domain, secure, httponly);
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
session
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>
</body>
</html>
json
PHP – json_encode()
$age = array("Peter"=>35, "Ben"=>37, "Joe"=>43);
echo json_encode($age); // 因為json 其實就是一個string 所以可以直接echo出來
PHP – json_decode()
$jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';
var_dump(json_decode($jsonobj, true));
取得 decoded的json 方法
// 第一種
$jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';
$obj = json_decode($jsonobj);
echo $obj->Peter;
echo $obj->Ben;
echo $obj->Joe;
// 第二種
$jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';
$arr = json_decode($jsonobj, true);
echo $arr["Peter"];
echo $arr["Ben"];
echo $arr["Joe"];
OOP
Classes/Objects
class 是一個類 而new出來就變一個一個的objects
class Fruit {
// Properties 屬性
public $name;
public $color;
// Methods 方法
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
$apple = new Fruit();
$banana = new Fruit();
$apple->set_name('Apple');
$banana->set_name('Banana');
echo $apple->get_name();
echo "<br>";
echo $banana->get_name();
constructor 在new這個class出來的時候自動call
class Fruit {
public $name;
public $color;
function __construct($name) { // 這個_construct會把new object出來時的參數帶過來
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
$apple = new Fruit("Apple");
echo $apple->get_name();
?
destructor 會結束的時候自動call這個function; 作用: 減少code量
class Fruit {
public $name;
public $color;
function __construct($name) {
$this->name = $name;
}
function __destruct() {
echo "The fruit is {$this->name}.";
}
}
$apple = new Fruit("Apple");
Access Modifiers: Visibility 三種->public/protected/private
- public - the property or method can be accessed from everywhere. This is default
到處都可call, 預設是這個
- protected - the property or method can be accessed within the class and by classes derived from that class
只有在該class和繼承的class可call (new 出來的object不算; 同理 繼承的class所new出來的object也不行)
- private - the property or method can ONLY be accessed within the class
只有該class可call 不能被繼承
Inheritance 繼承
class Fruit {
public $name;
public $color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
protected function intro() {
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}
class Strawberry extends Fruit { // 繼承就是用extends
public function message() {
echo "Am I a fruit or a berry? ";
}
$this->intro(); // 這樣子就可以使用protected的function
}
// Try to call all three methods from outside class
$strawberry = new Strawberry("Strawberry", "red"); // OK. __construct() is public
$strawberry->message(); // OK. message() is public
$strawberry->intro(); // ERROR. intro() is protected