PHP的基本常識小結_php基礎
May 16, 2016 am 09:00 AM這些PHP的概念,有些剛開始比較難懂,很難理解,我把他們都列出來,希望能幫助一些人,在前進的路上少點荊棘。
1. variable variables(變數的變數)
variable_variables.php
$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
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 '
';
print_r($numbers);
echo '
';
echo '
pop & push
';// pop the last element out of array
$c = array_popnumbers); 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
echo 'SERVER details:
'; echo 'SERVER_NAME: '.$_SERVER['SERVER_NAME'].'
';
echo 'SERVER_ADD: '.$_SERVER['SERVER_ADDR'].'
';
echo 'DOCUMENT_ROOT: '.$_SERVER['DOCUMENT_ROOT'].'phpcnltcnltbr /phpcnphpcn';
echo 'Page details:
';
echo 'REMOTE_ADDR: '.$_SERVER['REMOTE_ADDR'].'
';
echo 'REQUEST_URI: '.$_SERVER['REQUEST_URI'].'
echoSTRINGRY_STRING: '. '].'
';
echo 'REQUEST_METHOD: '.$_SERVER['REQUEST_METHOD'].'phpcnltltbrbr />';
echo 'REQUEST_TIVER: '.phpcnbr /phpcngtQUESTcn';
echo 'REQUEST_TIVER: '.phpcnbr /phpcnRES
';
echo 'HTTP_USER_AGENT: '.$_SERVER['HTTP_USER_AGENT'].'
';
echo '
';
echo '
6.variable_scope(變數的作用域 global static)
static_variables.php
function test()
{
$a = 0;
echo $a;
$a ;
}
test();
echo '
';
test();
echo '
';
test();
echo '
test();
echo '
echo '
';
function test1()
{
static $a = 0;
echo $a;
$a ;
echo $a;
$a ;
}
test1();
echo '
';
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
$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
$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 都指向同一個值,改變任何一個,都能改變同一個值。
更多詳細資訊
物件OOP
1.Fatal error: Using $this when not in object context
這個錯誤剛學 OOP 肯定容易出現,因為有個概念你沒有真正理解。類的可訪問性(accessible),也可以說是作用域, 你還可以認為是1個中國人在國外,他不屬於哪個文化,他不講外語(可能他知道點);但是他無法通過自己跟老外溝通,因為他們不是在一個共同國度出生。
那麼錯誤是如何發生的呢?看下面的範例:
class Trones{
static public $fire = "I am fire.";
public $water = "I am water";
}
static function getWater( ) {
return $self::water ; // wrong
}
}
/*
Fatal error: Using $this conennot inbsing $ BR> */
//echo Trones::getFire( ) ;
//echo Trones::getWater( ) ;
// correct
echo Trones::Fire( );
echo "
" ;
$trones = new Trones ;
$trones->fire ; // Notice: Undefined property: Trones::$fire (base on defferent error settingerror simple is seterror echo Trones::$fire ;
這個錯誤很經典, 也很實用,先看 static 的定義:
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though static.
翻譯:定義一個類別的屬性或方法為靜態時,可以使他們在不需要初始化一個類別時就能直接存取 。一個被定義為了靜態的屬性不能被類別的物件用物件操作符存取* -> *,(可以透過靜態的方法存取)。
範例說明:
7行 10行 犯了同一個錯誤,第一個是用物件運算子來存取靜態變數。你看看定義,$this 是一個偽變數 相當於 object,一個實例。你用物件操作符 -> 存取就會報錯。
同樣你也不能用 靜態運算子 :: 來存取一個公共變數 。正確的存取應該是 14行 25行,一個是在類別的定義裡存取(self:: === Trones::),一個是在類別的外部存取。
對於繼承類,以上的規則同樣適合。
2.Fatal error: Call to private method
最近有部連續劇很好看,叫權利的遊戲,我們假設有 3方人馬, 7個國王, 平民, 龍女。他們三方人馬在下面爭奪最終的勝利, 也就是王冠。
下面的故事還有一個標題:類別的可見性(visibility) 你如果知道最終的答案,解釋部分你可以略過了。
class Trones {
protected $fire = " fire ";
public $water = " water " ;
static private $trones = "Trones";
protected function getFire( ) {
$this-phpcngtcnfire; return __METHOD__." 使用".$this->getFire()." 取得".self::getTrones( ) ;
}
static public function getWater( ) {
static public function getWater( ) {
turn
static private function getTrones( ) {
return self::$trones ;
}
}
return __METHOD__." 取得".self::getTrones( );
}
}
ThePeople People ext }
}
ThePeople People ext class People ext
return __METHOD__." 取得".self::getTrones( );
}
}
echo Kings::TheSevenKing( ) ;
正確答案是:七國徵戰內鬥,平民死傷無數,龍女想乘機漁翁得利;可惜最終誰也得到沒有皇冠和勝利。哈哈。
當靜態聆聽私密,結合產生複雜,也產生美;就像抽象的人,像我們大學老師講的數學課;(不過網易的公開數學課很好)
如果想要龍女獲得最後的勝利,你只要幫她一把將13行的$this->getFire()這部分去掉就可以了。同樣的道理你無法在一個靜態函數裡使用任何物件操作符。
怎麼讓人民獲得王冠呢?你去奮鬥吧!
如果你不建立大型的框架和網站這些概念比如Interface Implement抽象。。你還是不知道的好。

熱門文章

熱門文章

熱門文章標籤

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)