1. 基本文法
PHP 的指令碼塊以 <?php 開始,以 ?> 結束,我們可以把 PHP 的指令碼塊放置在文檔中的任何位置
PHP每個代碼都必須以分號結束
2. 變數
在 PHP 中,不需要在設定變數之前聲明該變數,不必向 PHP 聲明該變數的資料類型,根據變數被設定的方式,PHP 會自動地把變數轉換為正確的資料類型。
eg:
$a = 10;$b = 5;$b .= $a;echo $b;
3. 運算子
PHP 支援所有基本運算子:
+ - * / % ++ --
= += -= *= /= .= %=
== != > < >= <=
&& || !
eg:
$a = 10;$b = 5;$b .= $a;echo $b;
4. 數組
PHP有三種數群組類型:
數值數組:帶有數字 ID 鍵的數組
關聯陣列:數組中的每個 ID 鍵關聯一個值
多維陣列:包含一個或多個數組的數組
數字數組:
數值數組儲存的每個元素都帶有一個數字 ID 鍵,建立方式以下兩種:
$type1 = array(1, 2, 3);$type2[0] = 1;$type2[1] = 2;$type2[2] = 3;echo "array1[2]:" . $type1[2] . " array2[2]: " . $type2[2];
關聯陣列:
關聯陣列,它的每個 ID 鍵都關聯一個值。建立方式以下兩種:
$name_age1 = array("zhou"=>20,"huang"=>30,"yan"=>40);$name_age2["zhou"]=20;$name_age2["huang"]=30;$name_age2["yan"]=40;echo "name_age1['yan']:" . $name_age1['yan'] . " name_age2['yan']: " . $name_age2['yan'];echo " <br>";
多維陣列:
在多維陣列中,主數組中的每個元素也是一個數組。在子數組中的每個元素也可以是數組,以此類推
eg:
$student = array( "zhou"=>array ( 1, 2, 3 ), "huang"=>array ( 1, 2, 3 ));echo "student['zhou'][2]:" . $student['zhou'][2];echo " <br>";
5. if..elseif..else 、switch
PHP 的if、swithch語句和c版的基本類似,上例子:
$var = 2;if ($var == 1) echo "if_test: 1"; elseif ($var == 2) echo "if_test: 2"; else echo "if_test: other"; echo " <br>";switch ($var){case 1: echo "switch_test: 1"; break;case 2: echo "switch_test: 2"; break;default: echo "swith_test: 3";};echo " <br>";
6. 迴圈
while : 只要指定的條件成立,則迴圈執行代碼塊
do...while : 首先執行一次代碼塊,然後在指定的條件成立時重複這個迴圈
for : 迴圈執行代碼塊指定的次數
foreach : 根據數組中每個元素來迴圈代碼塊
eg:
$name_age2["zhou"]=20;$name_age2["huang"]=30;$name_age2["yan"]=40;//do...while$i=0;do{ $i++; echo "The number is " . $i . "<br />";}while ($i<5);//foreachforeach ($name_age2 as $value){ echo "Value: " . $value . "<br />";}
7. 函數
php函數和c函數類似,有參數、傳回值
一個函數使用例子:
//functionfunction add($x,$y){ $total = $x + $y; return $total;}echo "100 + 10 = " . add(100,10);
完整代碼:
test.php
<html><body><?php/** php note*/// php note//variable$txt = "hello php";echo $txt;echo " <br>";//operator$a = 10;$b = 5;$b .= $a;echo $b;echo " <br>";//array$type1 = array(1, 2, 3);$type2[0] = 1;$type2[1] = 2;$type2[2] = 3;echo "array1[2]:" . $type1[2] . " array2[2]: " . $type2[2];echo " <br>"; //array$name_age1 = array("zhou"=>20,"huang"=>30,"yan"=>40);$name_age2["zhou"]=20;$name_age2["huang"]=30;$name_age2["yan"]=40;echo "name_age1['yan']:" . $name_age1['yan'] . " name_age2['yan']: " . $name_age2['yan'];echo " <br>"; //array$student = array( "zhou"=>array ( 1, 2, 3 ), "huang"=>array ( 1, 2, 3 ));echo "student['zhou'][2]:" . $student['zhou'][2];echo " <br>";//if$var = 2;if ($var == 1) echo "if_test: 1"; elseif ($var == 2) echo "if_test: 2"; else echo "if_test: other"; echo " <br>";//switchswitch ($var){case 1: echo "switch_test: 1"; break;case 2: echo "switch_test: 2"; break;default: echo "swith_test: 3";};echo " <br>";//do...while$i=0;do{ $i++; echo "The number is " . $i . "<br />";}while ($i<5);//foreachforeach ($name_age2 as $value){ echo "Value: " . $value . "<br />";}//functionfunction add($x,$y){ $total = $x + $y; return $total;}echo "100 + 10 = " . add(100,10);?></body></html>
測試回合:
瀏覽器輸入:http://192.168.21.133/test.php
輸出:
hello php
510
array1[2]:3 array2[2]: 3
name_age1['yan']:40 name_age2['yan']: 40
student['zhou'][2]:3
if_test: 2
switch_test: 2
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
Value: 20
Value: 30
Value: 40
100 + 10 = 110