跳轉到內容

軟體工程師手冊/語言詞典/PHP

來自華夏公益教科書

PHP,代表“PHP:超文字預處理器”,是一種開源指令碼語言。它可以嵌入到 HTML 中,類似於 JSP 的工作方式。

PHP 的分類有點困難。建立者將其定義為一種 web 開發指令碼語言,而它確實也是。雖然它主要以過程式為主,但也逐漸轉向面嚮物件語言(PHP 5)。所以,PHP 實際上可以是你想要它成為的樣子。需要注意的是,它是一種解釋型語言,可以被編譯成一些東西。 PHP.net 的介紹頁面

執行入口點

[編輯 | 編輯原始碼]

PHP 通常透過 web 伺服器訪問,處理包含一些 php 程式碼的 html 頁面,或是一個 php 檔案。

在 html 檔案中

 <html>
 <body>
 Hello World! <br>
 Today is 
 <?
    $date = date('D M dS');
    echo $date;
 ?>
 </body>
 </html>

或者你可以透過將整個檔案包含在 <? 和  ?> 之間來建立一個完整的 php 檔案。一般的趨勢是將這些檔案命名為 .php 檔案,用於渲染 html 等內容的檔案,以及 .inc 檔案,用於庫檔案。

通用語法

[編輯 | 編輯原始碼]

變數前面包含一個 '$',並且不需要宣告。行末以分號結尾,與 C 語言類似。與大多數 web 開發指令碼語言一樣,也有一些全域性變數,例如 $_SESSION 和 $_POST、$_GET 以及 $_REQUEST。

一個通用的賦值方式如下所示

 $a = 'bar';
 $b = 4;
// this is an inline comment.  Everything after the // is a comment.

塊註釋由起始的 /* 和結束的 */ 指定,可以跨越多行。

 /*
  * this is a block comment 
  */

PHP 包含 phpdoc - 可以在函式宣告之前的註釋中建立 html 手冊。它們看起來像這樣

 /**
  * Function description
  *
  * @param  <datatype>   <$name of var>   <description of var>
  * 
  * @return <datatype>
  */

這裡還可以使用一些其他的 @ 型別。需要生成 phpdoc 的來源。更多內容將陸續推出。

變數宣告

[編輯 | 編輯原始碼]

宣告 i 為一個整數

$i = 1;

或者

$str = 'string';

不需要宣告資料型別,所以可以透過將其賦值給其他內容來改變變數的資料型別

$foo = 1;
$foo = 'string';

這裡的一個障礙是,你需要知道如何連線字串以及新增值。

要連線字串,可以使用 "." 或 ".="。如果你想新增值,則使用 "+" 或 "+="。

如果你這樣做

$foo = 'string';
$foo += ' is cool';

你將得到一個數字而不是 "string is cool",因為你新增的是字串,而不是連線字串。

方法宣告/實現

[編輯 | 編輯原始碼]

函式宣告

 function foo( $param1, $param2 ) {
     return 'hello';
 }

你可以像這樣設定預設引數

 function foo( $param1, $param2=100 ) {
     return 'hello';
 }

你不需要宣告任何返回值。

當你在類中宣告一個函式時,它看起來與上面顯示的相同,唯一的區別是它的物理位置在類邊界內。

 class Foo { 
  function bar () {
      return 'something';
  }
 }

作用域

[編輯 | 編輯原始碼]

所有變數在 PHP 中只有一個作用域。如果它在函式中,則它具有區域性作用域。如果它在函式之外,則它具有全域性作用域。

你可以透過兩種方式建立全域性變數。第一種方法是在函式之外放置它。第二種方法是使用這種方式宣告它

global $foo;

這使得它可以在任何地方訪問。

要在函式內部訪問全域性值,你需要宣告你正在使用它,否則 php 會認為它與全域性變數完全不同。

YES

 $foo = 2;
 function foo() {
     global $foo;
     $bar = 1;
     return $foo + $bar;
 }

NO

 $foo = 3;
 function foo() {
     $bar = 1;
     return $foo + $bar;
 }

PHP 將在函式內部建立 $foo,並且不會將其與全域性 $foo 關聯起來。“NO”示例中的兩個 $foo 是完全不同的實體。

$bar 是區域性變數,其作用域僅限於該函式。

PHP 有關於作用域的優秀 文件 可供使用。

條件語句

[編輯 | 編輯原始碼]

有典型的 if/else 語句

 if( $a == $b ) {
     $c = $d;
     // can put more statements here
 } else if ( $b == $c ) {
     $c = $e;
     // can put more statements here
 } else {
     $c = $f;
     // can put more statements here
 }

只要條件之後只有一條語句,就可以刪除大括號

 if( $a == $b ) 
     $c = $d; // only one statement allowed
 else 
     $c = $f; // only one statement allowed

另一種有效的 if 語句編寫方式如下所示

$bar = (($foo == true) ? 'test' : 'not test');

前兩種更易於閱讀。

還有 case/switch 條件語句

 switch ($foo) {
     case 'foo':
        echo 'bar';
        break;// breaks out of the switch case, to prevent us from running 
              // the stuff below this. 
     case 'bar':
        echo 'foo';
        break; // breaks out of the switch case, to prevent us from running 
               // the stuff below this. 
     default: 
        echo 'empty';
 }

請訪問 php 的結構頁面 以獲取更多資訊以及條件語句的其他編寫方式。

迴圈語句

[編輯 | 編輯原始碼]

PHP 包含傳統的迴圈 - for 迴圈、while 迴圈和 do while 迴圈。還有一個 foreach 迴圈。

For

 // prints out the numbers from 0 to 4, as soon as $i equals 5 it stops. 
 for(
     echo $i;
 }

PHP 有一個特殊的 foreach 迴圈,用於處理陣列

 foreach( $fooArray as $key=>$value ) {
     // starting at the begining of the array it will give the key (index) 
     // and value for each item.
 }

 // you can also just say:
 foreach( $fooArray as $value ) {
     // you don't need to use $key
 }

While

 // this counts backwards from 100 printing out each number like this 
 // "100...99...98..." and so on until it reaches 0, then stops
 $i = 100;
 while( $i > 0 ) {
     echo "$i...";
     $i--;
 }
or
 // keeps checking the $cond variable and stops when it equals false
 // if $cond == false from the start, the stuff in the loop will never execute. 
 while( $cond != true ) {
     // do something
 }


Do while

 // this does the same thing as the above "for" loop, only it always executes 
 // at least once before it checks if the condition ($i <5) has been met, 
 // or is still good. 
 $i = 0;
 do {
     $i++
     echo "$i...";
 while( $i < 5 );

請訪問 php 的迴圈頁面 以獲取更多關於迴圈以及其他編寫方式的資訊。

輸出語句

[編輯 | 編輯原始碼]

以下是如何在 PHP 中打印出“Hello World!”。

echo 'Hello World!';

當你想在字串中包含 PHP 變數時,你需要使用雙引號,因為 PHP 不會解析單引號之間的資料,它只會將其打印出來。

$foo = 'bar';
echo "foo$foo"; 
// prints out foobar;

當字串中沒有變數時,最好使用單引號,因為它可以節省處理時間。

錯誤處理/恢復

[編輯 | 編輯原始碼]

<描述錯誤處理和恢復。根據需要提供示例。>

PHP 使用陣列作為原生資料型別。

 // example 1
 $ages = array();
 $ages['Bobby'] = 12;
 $ages['Rob'] = 12;

 // example 2
 $foo = array();
 $foo[0] = 8;
 $foo[1] = 9;
 $foo[] = 10; // puts 10 in $foo[2]

 // example 3
 $bar[0] = 12; // don't need to specifically declare $bar, it's just nice to. 
 $bar['total'] = 3;
 $bar[] = 13; // put's 13 in $bar[1]

PHP 中的陣列非常強大且實用。它們可以像雜湊表一樣使用,如第一個和第三個示例中所示 - 您可以使用索引和字串訪問陣列。您不需要顯式宣告陣列,雖然這是一個好習慣。此外,您也可以像示例 2 中一樣,只寫兩個方括號,例如 $foo[],表示您希望將資料插入到下一個索引中。

演算法

[編輯 | 編輯原始碼]

<列出該語言本機提供的演算法或演算法列表的參考。列出在語言中沒有提供演算法的情況下如何合併演算法。或者,如果不可用,請描述一下。>

垃圾回收

[編輯 | 編輯原始碼]

<描述垃圾回收是自動的還是手動的。>

物理結構

[編輯 | 編輯原始碼]

<描述檔案、庫和部分通常如何劃分和排列。>

<請包含從其他語言切換到這種語言更容易的提示。>

網路參考

[編輯 | 編輯原始碼]

有一個很棒的 PHP 庫在 php.net。這裡有手冊以及庫頁面 - 所以對於任何有一定程式設計經驗的使用者來說都很合適。您肯定需要此頁面作為庫的參考。

書籍和文章

[編輯 | 編輯原始碼]

<列出可能有所幫助的其他書籍和文章。請包括這些參考適合哪些級別的讀者。(初學者/中級/高階)>

華夏公益教科書