變數名規則:字母和下劃線開頭,後面跟上任意的字母、下劃線和數字組成。
作用域:
$var = 110;
function test(){
var_dump($var); //區域性變數,只在函式體中生效
}
test(); //NULL
可變變數:
$var = 'abc';
$temp = 'var';
echo $$temp; //abc
echo ${$temp}; //abc 可讀性更好
超全域性變數(預定義變數):
print_r($GLOBALS);//$GLOBALS包含所有超全域性變數
Array
(
[_GET] => Array
(
[status] => 3
)
[_POST] => Array
(
[uid] => 2
)
[_COOKIE] => Array
(
[system_lang] => chinese
[city_id] => 7
[login_name] => xiaoming
[PHPSESSID] => lssctprsd5amgev013676sph12
)
[_FILES] => Array
(
)
[_SERVER] => Array
(
[HTTP_ACCEPT_LANGUAGE] => zh-CN,zh;q=0.9
[HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36
[HTTP_UPGRADE_INSECURE_REQUESTS] => 1
[HTTP_CONNECTION] => keep-alive
[HTTP_HOST] => www.test.com
[REDIRECT_STATUS] => 200
[SERVER_NAME] => www.test.com
[SERVER_PORT] => 80
[SERVER_ADDR] => 127.0.2.1
[REMOTE_PORT] => 51413
[REMOTE_ADDR] => 127.0.0.1
[SERVER_SOFTWARE] => nginx/1.18.0
[GATEWAY_INTERFACE] => CGI/1.1
[REQUEST_SCHEME] => http
[SERVER_PROTOCOL] => HTTP/1.1
[DOCUMENT_ROOT] => D:/workCoder/pro1
[DOCUMENT_URI] => /test.php
[REQUEST_URI] => /test.php?status=3
[SCRIPT_NAME] => /merchant.php
[CONTENT_LENGTH] =>
[CONTENT_TYPE] =>
[REQUEST_METHOD] => GET
[QUERY_STRING] => status=3
[SCRIPT_FILENAME] => D:/workCoder/pro1/test.php
[PATH_INFO] =>
[FCGI_ROLE] => RESPONDER
[PHP_SELF] => /test.php
[REQUEST_TIME_FLOAT] => 1611018257.2737
[REQUEST_TIME] => 1611018257
[original_get] => Array
(
[status] => 3
)
[original_post] => Array
(
)
)
[_REQUEST] => Array
(
[status] => 3
)
[GLOBALS] => Array
*RECURSION*
[_ENV] => Array
(
)
[_SESSION] => Array
(
[admin_verify] => d01eeca8b24321cd2fe89dd85b9beb51
[admin_id] => 0
[admin_name] => admin
)
)
定義:
define('CAT','貓');
define('CATFAMILY',array(
'獅子','老虎','獵豹'
));
const APPLE = '蘋果';
const FRUITS = array(
'雪梨','橙子','西瓜'
);
print_r(CAT); //貓
print_r(CATFAMILY); //Array ( [0] => 獅子 [1] => 老虎 [2] => 獵豹 )
print_r(APPLE); //蘋果
print_r(FRUITS); //Array ( [0] => 雪梨 [1] => 橙子 [2] => 西瓜 )
define('APPLE','香蕉'); //Constant CAT already defined 不能重複定義
作用域:執行指令碼中全域性使用
魔術常量:
<?php
namespace index;
function animal(){
echo __FUNCTION__;
}
trait canteen{
public function food(){
echo __TRAIT__;
}
}
class school{
public function __construct(){
echo __CLASS__;
}
public function teacher(){
echo __METHOD__;
}
use canteen;
}
echo __NAMESPACE__; // index
echo __LINE__; // 27
echo __FILE__; // /Users/xiaoming/pro2/index.php
echo __DIR__; // /Users/xiaoming/pro2
animal(); // animal
$schoolObj = new school(); // index\school
$schoolObj -> teacher(); // index\school::teacher
$schoolObj -> food(); // index\canteen
本作品採用《CC 協議》,轉載必須註明作者和本文連結