這些是寫給初級PHP程式員或者入門不久的同學的,老鳥可以飄過,歡迎補充和評論;接受合理意見與批評。
這些PHP的概念,有些剛開始比較難懂,很難理解,我把他們都列出來,希望能協助一些人,在前進的路上少點荊棘。
1. variable variables(變數的變數)
variable_variables.php
View Code
$a = 'hello';
$hello = 'hello everyone';
echo $$a.'
';
$b = 'John';
$c = 'Mary';
$e = 'Joe';
$students = array('b','c','e');
echo ${$students[1]};
/*
foreach($students as $seat){
echo $$seat.'
';
}
$$var[1]
${$var[1]} for #1
*/
$a = 'hello';
將hello 賦值給 變數 $a, 於是 $$a = ${hello} = $hello = 'hello everyone';
如果對於 $$students[1], 這樣會產生混亂,php的解譯器可能無法理解,‘[’ 雖然有較高運算子,但結果可能無法輸出。
好的寫法是:${$students[1]} = ‘Mary’;
2. array's function(數組函數)
array_functions.php
View Code
echo '
shift & unshift
';
$numbers = array(1,2,3,4,5,6);
print_r($numbers);
echo '
';
// shifts first elemnt out of an array
// the index will reset
$a = array_shift($numbers);
echo 'a: '.$a.'
';
print_r($numbers);
// push element to the front of array
// returns the count of array and reset array index
$b = array_unshift($numbers, 'first');
echo '
b: '.$b.'
';
print_r($numbers);
echo '
';
echo '
pop & push
';
// pop the last element out of array
$c = array_pop($numbers);
print_r($numbers);
echo '
';
// push the element to the last of array
$d = array_push($numbers, 'last');
echo 'd: '.$d.'
';
print_r($numbers);
3. dates and times (時間和日期)
有3種方法可以建立一個unix time(從1970/1/1 到現在的秒數)
time(); 返回當前的時間戳記
mktime($hr, $min, $sec, $month, $day, $year); mktime(6,30,0,5,22,2012) 返回2012 5/22 6:30:00 的時間戳記
strtotime($string); strtotime("+1 day") 返回明天這個時候的時間戳記 更多 'last Monday' 'lasy Year'
----------------------------------------------------------
checkdate($month, $day, $year); 驗證一個日期是否為真 checkdate(5,32,2012) ? 'true' : 'false'; // return false
得到了時間戳記後,我們需要對它進行轉化為可讀的,如2012/5/22
我們有2種方法 date($format, $timestamp) ; strftime($format [,$timestamp])
推薦用第2種,strftime("%Y-%m-%d %H:%M:%S"); // return 2012-05-22 15:46:40
5. server variables (伺服器和執行環境資訊)
$_SERVER
server_variables.php
View Code
echo 'SERVER details:
';
echo 'SERVER_NAME: '.$_SERVER['SERVER_NAME'].'
';
echo 'SERVER_ADD: '.$_SERVER['SERVER_ADDR'].'
';
echo 'SERVER_PORT: '.$_SERVER['SERVER_PORT'].'
';
echo 'DOCUMENT_ROOT: '.$_SERVER['DOCUMENT_ROOT'].'
';
echo '
';
echo 'Page details:
';
echo 'REMOTE_ADDR: '.$_SERVER['REMOTE_ADDR'].'
';
echo 'REMORT_PORT: '.$_SERVER['REMOTE_PORT'].'
';
echo 'REQUEST_URI: '.$_SERVER['REQUEST_URI'].'
';
echo 'QUERY_STRING: '.$_SERVER['QUERY_STRING'].'
';
echo 'REQUEST_METHOD: '.$_SERVER['REQUEST_METHOD'].'
';
echo 'REQUEST_TIME: '.$_SERVER['REQUEST_TIME'].'
';
echo 'HTTP_USER_AGENT: '.$_SERVER['HTTP_USER_AGENT'].'
';
echo '
';
6.variable_scope(變數的範圍 global static)
static_variables.php
View Code
function test()
{
$a = 0;
echo $a;
$a++;
}
test();
echo '
';
test();
echo '
';
test();
echo '
';
echo '
';
function test1()
{
static $a = 0;
echo $a;
$a++;
}
test1();
echo '
';
test1();
echo '
';
test1();
echo '
';
test() 函數中的變數 $a 沒有儲存 $a++ 的結果 , 重複調用test() 並沒有使 $a 的值增加
而test1() 函數中 變數 $a 申明了 staic $a = 0, 為靜態變數。
引用:A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope.
一個靜態變數 只能存在於本地的函數範圍內 也就是test1() 函數體內, 但是當程式離開這個test1() 範圍時,靜態變數不會失去它的值,也就是 $a 變數會增加 1; 當重新調用 test1() 時,$a = 1;
global_variables.php
View Code
$a = 1;
$b = 2;
function Sum()
{
global $a, $b;
$b = $a + $b;
}
Sum();
echo $b;
echo '
';
$a = 1;
$b = 2;
function Sum1()
{
$GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
}
Sum1();
echo $b;
引用:In PHP global variables must be declared global inside a function if they are going to be used in that function
如果這些變數將在函數中使用,全域變數必須在使用的那個函數中進行定義。 這樣可以避免很多麻煩。
7.reference(引用)
variable_reference.php
View Code
$a = 'arist';
$b = $a;
$b = 'ming';
echo "My name is:{$a}. But my mother call me {$b}.
";
echo '
';
$a = 'arist';
$b = &$a;
$b = 'ming';
echo "My name is:{$a}. And my mother call me {$b}.
";
這個概念可以這樣理解,我媽叫我明明,但是我的領導會叫我小言;不管是明明或者是小言,都是我。
'&' 而這個就是不同的人叫我們的別名的方法 即引用,相當於 $a = {我,或者記憶體中的值} , $b = {領導,媽媽,或者變數}
通過 & , $b指向了$a 在記憶體中唯一也是相同的值。 所以不管你領導叫你什麼,或者你媽叫你什麼,你都是你。只是稱呼不同。
所以通過引用後, 我們改變$b的值,同時也改變了$a的值。
8. pass reference variable to function(傳遞引用參數給函數)
function ref_test(&$var){
return $var *= 2;
}
$a = 10;
ref_test($a);
echo $a;
當我們按引用傳遞參數給函數時,我們傳遞地不是變數的副本(copy) ,而是真實的值,
所以當我們調用函數ref_test($a)的時候已經改變了 $a 的值, 所以最後 $a = 20;
9. reference function return value(引用函數的傳回值)
reference_function_return_value.php
function &increment(){
static $var = 0;
$var++;
return $var;
}
$a =& increment(); // 1
increment(); // 2
$a++; //3
increment(); // 4
echo "a: {$a}";
首先申明一個引用函數,在函數體內,申明一個靜態變數 $var, 可以儲存增加的值;
$a =& increment(); 這條語句是 變數$a 引用 函數increment() 的傳回值,
和前面的引用變數一樣, 你可以把increment()函數, 看作是一個變數; 這樣就變為 $a = & $b;
所以increment() 和 $a 都指向同一個值,改變任何一個,都能改變同一個值。
摘自 warcraft
http://www.bkjia.com/PHPjc/478187.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/478187.htmlTechArticle這些是寫給初級PHP程式員或者入門不久的同學的,老鳥可以飄過,歡迎補充和評論;接受合理意見與批評。 這些PHP的概念,有些剛開始比較...