[PHP] ABC.

摘要:PHP ABC.

Server Install : XAMPP  (include Apeach,MySQL,Php) http://www.apachefriends.org/zh_tw/xampp.html
.php file location : ./xampp/htdocs/
HTTPServer : htpp://127.0.0.1/
 
<?php
echo "hello!" ;
?>
 
PHP 所支援的資料型態 (data types) 有 8 種
1. boolean
2. integer
3. floating (double)
4. string
5. array
6. object
7. resource
8. NULL
 
型態轉換
$number = (int) $x;
 
Var 靜態變數
static $a = 1;
 
Var 陣列
$names[0] = "A"
$names[1] = "B"
 
Var 物件
<?php
class person {
function who() {
echo "I'm Jollen.";
}
}
$man = new person;
$man->who(); // 輸出 I'm Jollen.
?>
 
Forms Request
$_POST["username"];  // 等於 $HTTP_POST_VARS["username"];
$_GET["username"];   // 等於 $HTTP_GET_VARS["username"];
 
環境變數
$ENV 陣列來讀取環境變數
 
常數
define("PI", 3.14159);
□ __FILE__
目前正在被執行的檔案名稱。假如有一個檔案 (例如 test.php) 被 include 到另外一個檔案執行 (例如 index.php),則回報回來的檔名為 test.php,即被 include 的檔案。當我們在為 include 許多檔案的 PHP 4 程式除錯時,__FILE__ 常數就顯得特別好用。
 
□ __LINE__
回報正被執行的行位置,如果是被 include 的檔案,則和 __FILE__ 一樣,回報的是該被 include 的檔案正被執行的行位置。
 
□ PHP_VERSION
PHP 的版本常數。
 
□ PHP_OS
作業系統的名稱常數。
 
□ TRUE
布林值的 true。
 
□ FALSE
布林值的 false。
 
□ E_ERROR
程式因錯誤而中斷執行。這裡的錯誤指的是無法忽略而且影嚮 PHP 程式繼續執行的錯誤,並非 PHP 語法上的錯誤。
 
□ E_WARNING
PHP 執行時的小錯誤,這些錯誤屬警告訊息,並不影嚮程式的執行。
 
□ E_PARSE
PHP 的語法有錯。
 
□ E_NOTICE
發生不合法的狀況,但不影嚮程式的執行,例如計算的變數不存在時。
 
IF.....
Method1.
if (EXPRESSION) statement;
 
Method2
if (EXPRESSION) {
statements;  
...
} elseif (EXPRESSION2) {
statements;
...
} else {
statements;
...
}
 
Method3
if (EXPRESSION) :
statements;
...
elseif (EXPRESSION2) :
statements;
...
else :
statements;
...
endif;
 
Method4
$a = ($b< 1) ? "yes" : "no";
 
While.....
Method1
while (EXPRESSION) {
statements; 
...
}
 
Method2
while (expr) :
statements; 
...
endwhile;
 
Method3
do {
statement1;
...
} while (expr);
 
For....
for ($i = 0; $i <= 10 ; $i++) {
statements;
...
}
 
Foreach....
Method1
foreach ($arr as $value) {
    echo "Value: $value";
}
 
Method2
foreach ($arr as $key => $value) {
    echo " $key, $value ";
}
 
Method3
$arr[0][0] = "aaa";
$arr[0][1] = "bbb";
$arr[1][0] = "ccc";
$arr[1][1] = "ddd";
 
foreach($arr as $v1) {
    foreach ($v1 as $v2) {
        echo "$v2 ";
    }
}
 
Switch...
Method1
switch (EXPRESSION) {
case 1: statements;
        break;
case 2: statements;
        break;
default: statements;
          break;
}

Method2
switch (EXPRESSION) :
case 1: statements;
        break;
case 2: statements;
        break;
default: statements;
          break;
endswitch;
 
Switch...
Method1
// ........
 
Method2
 /* ........*/
 
Include File
Method1   (不可在判斷是或是迴圈子句中)
require("xxx.php");
 
require_once("xxx.php")
 
Method2
incluce("xxx.php");
 
incluce_once("xxx.php");

here docs syntax
$str = <<<EOD
I saw a dog yesterday.
It's very fat.
Too fat to walk.
I tried to help it.
But in vain.
Because it weights 100 KG.
EOD;
//EOD 為字串的開始與結果,可自定
 
function()....
Method1  //預設值
function add($x, $y=1) {
   return $x+$y;
}
 
Method2  //可變長度參數
function fn() {
   $numargs = func_num_args();
      echo "A: ". func_get_arg(0). "";
      echo "B: ". func_get_arg(1). "";
      echo "C: ". func_get_arg(2). "";
   }
   return $numargs;
}
$n = fn (1, 2,3);
echo $n;
//  result:
A=1
B=2
C=3
3
 
Method3  //可變長度參數 by foreach
function fn() {
   foreach(func_num_args() as $value){
        echo $value;
   }
}
$n = fn (1, 2,3);
echo $n;
//  result:
A=1
B=2
C=3
 
字串連接字元....(.)
$a = "hello " . "world";
 
URL encode...
$URL = rawurlencode( “http://abc.com/a=test....")
 
HTML <header>....
header("Content-type: text/html");  //same to : echo("Content-type: text/html");
header("Refresh: 10; URL=index.php");
 
Session....
1. 初始化 session:
session_start();
 
2. 註冊 session:
session_register("userID");
 
3. 清除 session:
session_destroy();
 
Cookie
$userinfo = $HTTP_COOKIE_VARS["userinfo"];
 
Connect MySQL...
// Create connection
$con=mysqli_connect("example.com","peter","abc123","my_db");
 
// Check connection
if (mysqli_connect_errno($con))
{
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
 
//close connection
mysqli_close($con);