基本變量
$
開(kāi)頭,緊跟一個(gè)字母或者下劃線,之后再跟不限數(shù)量的字母、數(shù)字、下劃線;正則表達(dá)為:
[a-zA-Z_\x7f-\xff][0-9a-zA-Z_\x7f-\xff]*
最好初始化變量,未初始化的變量在使用的時(shí)候根據(jù)上下文取初始值。如boolean環(huán)境為false; int 為0;float 為0.0;string 為""; array為空數(shù)組;對(duì)象為空對(duì)象;
<?php
// Unset AND unreferenced (no use context) variable; outputs NULL
var_dump($unset_var);
// Boolean usage; outputs 'false' (See ternary operators for more on this syntax)
echo($unset_bool ? "true\n" : "false\n");
// String usage; outputs 'string(3) "abc"'
$unset_str .= 'abc';
var_dump($unset_str);
// Integer usage; outputs 'int(25)'
$unset_int += 25; // 0 + 25 => 25
var_dump($unset_int);
// Float/double usage; outputs 'float(1.25)'
$unset_float += 1.25;
var_dump($unset_float);
// Array usage; outputs array(1) { [3]=> string(3) "def" }
$unset_arr[3] = "def"; // array() + array(3 => "def") => array(3 => "def")
var_dump($unset_arr);
// Object usage; creates new stdClass object (see http://www.php.net/manual/en/reserved.classes.php)
// Outputs: object(stdClass)#1 (1) { ["foo"]=> string(3) "bar" }
$unset_obj->foo = 'bar';
var_dump($unset_obj);
?>
預(yù)定義變量
下面羅列大寫(xiě)字母的為超全局變量,在任何地方都可以訪問(wèn);不是大寫(xiě)的是一般的全局變量,在函數(shù)內(nèi)部不能訪問(wèn)。
$GLOBALS 訪問(wèn)文件中的全局變量
function test(){
$foo = "i am fool";
echo "access global {$GLOBALS['foo']}\n"; //access global global foo
echo "access local {$foo}\n"; //access local i am fool
}
$foo = " global foo";
$_GET 用戶GET方法請(qǐng)求的數(shù)組
$_POST 用戶POST方法請(qǐng)求的數(shù)組
$_REQUEST GET 和 POST 方法請(qǐng)求的數(shù)組
$_FILES POST方法上傳的文件,僅此一個(gè)復(fù)數(shù)形式
$_SERVER 包括了整個(gè)請(qǐng)求的信息
用戶請(qǐng)求相關(guān),(個(gè)人覺(jué)的是瀏覽器發(fā)送的請(qǐng)求頭中的信息)
HTTP_HOST 用戶請(qǐng)求的主機(jī)名(域名或者是IP,在瀏覽器中輸入的)
HTTP_ACCEPT 用戶可以接受的文件類(lèi)型
HTTP_ACCEPT_ENCODING 用戶可接收的的壓縮編碼格式
HTTP_ACCEPT_LANGUAGE 用戶可以理解的語(yǔ)言
HTTP_USER_AGENT 請(qǐng)求客戶端信息
HTTP_COOKIE 請(qǐng)求攜帶的cookie
//用戶請(qǐng)求
REQUEST_METHOD 請(qǐng)求使用的方法
REQUEST_SCHEME 請(qǐng)求的協(xié)議 http還是https
REQUEST_URI 請(qǐng)求地址中端口號(hào)后面所有的字符串,包括參數(shù)
QUERY_STRING GET方法中的參數(shù)
REMOTE_ADDR 請(qǐng)求側(cè)的IP,不是私有地址,感覺(jué)應(yīng)該是出口IP地址
REMOTE_PORT 同上,應(yīng)該是出口端口號(hào)
//服務(wù)器相關(guān)
SERVER_SOFTWARE 服務(wù)器的信息
SERVER_ADDR 服務(wù)器的IP地址
SERVER_PORT 服務(wù)器端的端口號(hào),一般為80
SCRIPT_FILENAME 放問(wèn)的文件的在服務(wù)器的絕對(duì)路徑
DOCUMENT_ROOT 域名所指向的根路徑
下面是完整參考
array(33) {
["UNIQUE_ID"]=>
string(27) "WMardfubhCEAZytbSVp3sAAAAAE"
["HTTP_HOST"]=>
string(14) "h5app.7dyk.com"
["HTTP_CONNECTION"]=>
string(10) "keep-alive"
["HTTP_CACHE_CONTROL"]=>
string(9) "max-age=0"
["HTTP_UPGRADE_INSECURE_REQUESTS"]=>
string(1) "1"
["HTTP_USER_AGENT"]=>
string(108) "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"
["HTTP_ACCEPT"]=>
string(74) "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
["HTTP_ACCEPT_ENCODING"]=>
string(19) "gzip, deflate, sdch"
["HTTP_ACCEPT_LANGUAGE"]=>
string(14) "zh-CN,zh;q=0.8"
["HTTP_COOKIE"]=>
string(123) "CNZZDATA1259969345=692633714-1478100153-%7C1479091446; _ga=GA1.2.678844300.1478100268; PHPSESSID=ri7l2mcvoc3ottu14dde1mv4e5"
["PATH"]=>
string(49) "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
["SERVER_SIGNATURE"]=>
string(0) ""
["SERVER_SOFTWARE"]=>
string(52) "Apache/2.4.6 (CentOS) OpenSSL/1.0.1e-fips PHP/5.4.16"
["SERVER_NAME"]=>
string(14) "h5app.7dyk.com"
["SERVER_ADDR"]=>
string(14) "101.200.85.218"
["SERVER_PORT"]=>
string(2) "80"
["REMOTE_ADDR"]=>
string(13) "114.255.40.10"
["DOCUMENT_ROOT"]=>
string(13) "/var/www/html"
["REQUEST_SCHEME"]=>
string(4) "http"
["CONTEXT_PREFIX"]=>
string(0) ""
["CONTEXT_DOCUMENT_ROOT"]=>
string(13) "/var/www/html"
["SERVER_ADMIN"]=>
string(16) "wuwenqi@7dyk.com"
["SCRIPT_FILENAME"]=>
string(26) "/var/www/html/ama/test.php"
["REMOTE_PORT"]=>
string(5) "30255"
["GATEWAY_INTERFACE"]=>
string(7) "CGI/1.1"
["SERVER_PROTOCOL"]=>
string(8) "HTTP/1.1"
["REQUEST_METHOD"]=>
string(3) "GET"
["QUERY_STRING"]=>
string(8) "name=123"
["REQUEST_URI"]=>
string(22) "/ama/test.php?name=123"
["SCRIPT_NAME"]=>
string(13) "/ama/test.php"
["PHP_SELF"]=>
string(13) "/ama/test.php"
["REQUEST_TIME_FLOAT"]=>
float(1489415029.184)
["REQUEST_TIME"]=>
int(1489415029)
}
$_SESSION PHP默認(rèn)的session機(jī)制中,session數(shù)據(jù)
$_COOKIE 用戶請(qǐng)求攜帶的coockie信息
php://input 消息體中原始數(shù)據(jù)
<?php $postdata = file_get_contents("php://input"); ?>
php://input is not available with enctype="multipart/form-data"
$http_response_headers : 返回消息頭,與get_headers(string $url)功能一樣
<?php
function get_contents() {
file_get_contents("http://example.com");
var_dump($http_response_header);
}
array(9) {
[0]=>
string(15) "HTTP/1.1 200 OK"
[1]=>
string(35) "Date: Sat, 12 Apr 2008 17:30:38 GMT"
[2]=>
string(29) "Server: Apache/2.2.3 (CentOS)"
[3]=>
string(44) "Last-Modified: Tue, 15 Nov 2005 13:24:10 GMT"
[4]=>
string(27) "ETag: "280100-1b6-80bfd280""
[5]=>
string(20) "Accept-Ranges: bytes"
[6]=>
string(19) "Content-Length: 438"
[7]=>
string(17) "Connection: close"
[8]=>
string(38) "Content-Type: text/html; charset=UTF-8"
}
argv 命令行模式,參數(shù)的數(shù)組, argv[0]為 腳本名
argc 命令行模式,參數(shù)的個(gè)數(shù),最小值位1,因?yàn)槟_本名被認(rèn)為是第一個(gè)參數(shù)
變量作用域
全局變量
在PHP文件最外層的變量擁有全局作用域,同樣作用于include和require文件
<?php
$a = 1;
include 'b.inc'; //b文件中也可以訪問(wèn)$a
?>
函數(shù)局部作用域
函數(shù)中的變量的作用域僅在函數(shù)中,比較嚴(yán)格,內(nèi)部不能訪問(wèn)外部的全局變量,如果想要訪問(wèn),需要使用 global 關(guān)鍵字或者是使用超全局變量 $GLOBALS;
<?php
$a = 1;
$b = 2;
function Sum()
{
global $a, $b;
$b = $a + $b; //這兩句可以替換為 $GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
}
Sum();
echo $b; //輸出3
?>
static 變量
static 僅能存在于函數(shù)局部作用域中,變量只在編譯的時(shí)候初始化一次,且函數(shù)執(zhí)行后的值仍然保留。static 和 global 都是通過(guò)引用實(shí)現(xiàn)的。
變量的變量
一個(gè)變量的值作為另一個(gè)變量的名稱(chēng);用在變量的聲明,也可用在對(duì)象的屬性中,為消除語(yǔ)義分歧,使用{}
<?php
//You can even add more Dollar Signs
$Bar = "a";
$Foo = "Bar";
$World = "Foo";
$Hello = "World";
$a = "Hello";
$a; //Returns Hello
$$a; //Returns World
$$$a; //Returns Foo
$$$$a; //Returns Bar
$$$$$a; //Returns a
$$$$$$a; //Returns Hello
$$$$$$$a; //Returns World
//... and so on ...//
?>
常量的定義
類(lèi)外部定義使用 define("MAX_NUMBER", 10); 5.3以后可以也可以使用const定義,名稱(chēng)區(qū)分大小寫(xiě)。
類(lèi)內(nèi)部定義使用 const MAX_NUMBER = 10;
class test{
const MAX_NUMBER = 10;
function print(){
echo self::MAX_NUMBER; //類(lèi)內(nèi)部函數(shù)調(diào)用
}
}
echo Test::MAX_NUMBER; 外部訪問(wèn)類(lèi)的常量
get_defined_constants(boolean categorize)
//獲取到所有定義的常量,一般category為true,得到的是多維數(shù)組,用戶定義的在 $constants['user']
分類(lèi)中。
<?php
define("MY_CONSTANT", 1);
print_r(get_defined_constants(true));
?>
輸出:
Array
(
[Core] => Array
(
[E_ERROR] => 1
[E_WARNING] => 2
[E_PARSE] => 4
[E_NOTICE] => 8
[E_CORE_ERROR] => 16
[E_CORE_WARNING] => 32
[E_COMPILE_ERROR] => 64
[E_COMPILE_WARNING] => 128
[E_USER_ERROR] => 256
[E_USER_WARNING] => 512
[E_USER_NOTICE] => 1024
[E_ALL] => 2047
[TRUE] => 1
)
[pcre] => Array
(
[PREG_PATTERN_ORDER] => 1
[PREG_SET_ORDER] => 2
[PREG_OFFSET_CAPTURE] => 256
[PREG_SPLIT_NO_EMPTY] => 1
[PREG_SPLIT_DELIM_CAPTURE] => 2
[PREG_SPLIT_OFFSET_CAPTURE] => 4
[PREG_GREP_INVERT] => 1
)
[user] => Array
(
[MY_CONSTANT] => 1
)
)
魔法變量
魔法變量在編譯時(shí)候初始化(即使是根據(jù)代碼中的位置決定的),而一般的常量在運(yùn)行時(shí)初始化,下表為9個(gè)魔法變量,不區(qū)分大小寫(xiě):
名稱(chēng) | 描述 |
---|---|
__LINE__ |
The current line number of the file. |
__FILE__ |
The full path and filename of the file with symlinks resolved. If used inside an include, the name of the included file is returned. |
__DIR__ |
The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(FILE). This directory name does not have a trailing slash unless it is the root directory. |
__FUNCTION__ |
The function name. |
__CLASS__ |
The class name. The class name includes the namespace it was declared in (e.g. Foo\Bar). Note that as of PHP 5.4 CLASS works also in traits. When used in a trait method, CLASS is the name of the class the trait is used in. |
__TRAIT__ |
The trait name. The trait name includes the namespace it was declared in (e.g. Foo\Bar). |
__METHOD__ |
The class method name. |
__NAMESPACE__ |
The name of the current namespace. |
ClassName::class |
The fully qualified class name. See also ::class. 5.5版本以后才可以使用。 |