想要學好PHP,怎麼能夠不瞭解基本的語法知識呢?下面是小編為大家蒐集整理出來的有關於PHP程式設計入門的基本語法知識點總結,一起來學習一下吧!
一、何為php
PHP,即“PHP: Hypertext Preprocessor”,是一種被廣泛應用的開源通用指令碼語言,尤其適用於 Web 開發並可嵌入 HTML 中去。它的語法利用了 C、Java 和 Perl,易於學習。該語言的主要目標是允許 web 開發人員快速編寫動態生成的 web 頁面,但 PHP 的用途遠不只於此。
簡單來說,就是php是一種指令碼語言,可以做很多事情。①伺服器端指令碼 ②命令列指令碼 ③編寫桌面程式
二、開始php
(1)下載php直譯器,其實win下面,最簡單的還是wamp這個軟體,下載下來什麼都有了...
(2)win下面貌似還需要, 這個連結庫,vc2012執行庫,安裝即可
(3)ide,無恥的使用了 phpStorm,等哥有錢了一定給你補回來, so...
12345678 | User: newasp License: ===== LICENSE BEGIN ===== 14617-12042010 00001xrVkhnPuM!Bd!vYtgydcusnqt mM!hZWoGg"DprWxZCBwsy8T91O7MRu NVHtrbzv8O9mmoLvtijcHSSE7i5Jr! ===== LICENSE END ==== |
三、入門引導
(1)簡單的輸出
1234567891011121314151617181920 | <?php /**
* Created by PhpStorm.
* User: LENOVO
* Date: 2014/9/28
* Time: 14:51
*/ // 輸出PHP詳細資訊 echo phpinfo();
//C: D:dizzyphp_ //phpinfo() //PHP Version => 5.6.1 // //System => Windows NT LENOVO-PC 6.1 build 7600 (Windows 7 Ultimate Edition) i586 //Build Date => Sep 24 2014 18:54:12 //Compiler => MSVC11 (Visual C++ 2012) //Architecture => x86 //Configure Command => cscript /nologo "--enable-snapshot-build" "--disable-isapi" "--enable-debug-pack" "--without-mssql" "--without-pdo-mssql" "--without-pi3web" "--with-pdo-oci=c:php-sdkoraclex86instantclient_12_1sdk,shared" "--with-oci8-12c=c:php-sdkoraclex86instantclient_12_1sdk,shared" "--enable-object-out-dir=../obj/" "--enable-com-dotnet=shared" "--with-mcrypt=static" "--without-analyzer" "--with-pgo" //Server API => Command Line Interface |
(2)簡單的表單處理
12345678910111213 | // 一個簡單的html表單 <form action= "" method= "post" >
<p>姓名: <input type= "text" name= "name" /></p>
<p>年齡: <input type= "text" name= "age" /></p>
<p><input type= "submit" /></p> </form>
// 接收表單資料, 使用超全域性變數 %_POST[ "name" ] %_POST[ "age" ] <?php echo htmlspecialchars( $_POST [ 'name' ]); ?> <?php echo (int) $_POST [ 'age' ]; ?> // 這便是最簡單的表單提交,及資料接收 |
四、基本語法
(1)PHP標記
123456 | <?php
echo "Hello World!" ;
// 當檔案為純PHP時,最好在末尾刪除PHP結束標記 //?> |
(2)從HTML中分離
1234567891011 | // 在一對開始和結束之外的內容,都會被PHP直譯器忽略。也就是html標籤和PHP程式碼混合的那種,跟jsp,asp一樣... <p>This is going to be ignored by PHP and displayed by the browser.</p> <?php echo 'While this is going to be parsed.' ; ?> <p>This will also be ignored by PHP and displayed by the browser.</p>
// 使用條件,高階分離 <?php if ( $expression == true): ?>
This will show if the expression is true. <?php else : ?>
Otherwise this will show. <?php endif ; ?> |
(3)指令分隔符,註釋
PHP需要在每個語句後面用分隔符結束指令。
註釋: // 或 /* ... */ 但是,*/ 會匹配最近的那個,切記!切記!
五、型別
PHP支援8種原始資料型別。
四種標量型別:boolean(布林型),integer(整型),float(浮點型,double),string(字串)
兩種複合型別:array(陣列),object(物件)
兩種特殊型別:resource(資源),NULL(無型別)
1234567891011121314151617181920 | <?php $a_bool = TRUE; // a boolean $a_str = "foo" ; // a string $a_str2 = 'foo' ; // a string $an_int = 12; // an integer
echo gettype ( $a_bool ); // prints out: boolean echo gettype ( $a_str ); // prints out: string
// If this is an integer, increment it by four if ( is_int ( $an_int )) {
$an_int += 4; }
// If $bool is a string, print it out // (does not print out anything) if ( is_string ( $a_bool )) {
echo "String: $a_bool" ; } ?> |
(1)Boolean 布林型別
可以為TRUE或FALSE,不區分大小寫。
一般非0,即為TRUE。
(2)Integer 整型
整型可以使用十進位制,十六進位制,八進位制或二進位制表示。八進位制前面必須加0(零),十六進位制加0x,二進位制加0b。
如果給定的一個數超出了interger的範圍,將會被解釋為float。同樣運算結果超出integer範圍,同樣如此。
php沒有整除運算子,1/2 將產生出 float 0.5。可以強制轉換為integer 或使用round() 更好的四捨五入。
1234567 | echo (int)2.9; // 輸出 2 echo round (2.555, 2) // 輸出 2.56 // 決不要將未知的分數強制轉換為 integer,這樣有時會導致不可預料的結果。 <?php echo (int) ( (0.1+0.7) * 10 ); // 顯示 7! ?> |
(3)Float 浮點型(double)
浮點型,也叫浮點數float,雙精度double, 實數real。
12345 | <?php $a = 1.234; $b = 1.2e3; $c = 7E-10; ?> |
(4)String 字元轉
一個字串string,就是由一系列的字元組成,其中每個字元等同於一個位元組。這就意味著php只能支援256個字符集,因此不支援Unicode。
string最大可以達到2GB。
123456789101112 | <?php $a = 123; echo '$a' ; // 輸出 $a echo "$a" ; // 輸出 123, 轉義字元 ''
$str = <<< 'EOD' Example of string spanning multiple lines using nowdoc syntax. EOD;
?> |
(5)Array 陣列
php中的陣列,實際上是一個有序序列。對映是把values關聯到keys的型別。
由於陣列元素的值也可以說是另外的陣列,樹形結構和多維陣列也是允許的`。
1234567891011121314151617 | <?php $array = array (
"foo" => "bar" ,
"bar" => "foo" , );
// 自PHP 5.4 起 $array = [
"foo" => "bar" ,
"bar" => "foo" , ] // key 可以是 integer 或 string 型別 // key 值為可選項, 如果未指定,則使用之前用過最大的integer鍵名加上1作為新鍵名 ?>
// 要修改某個值,通過其鍵名給該單元賦一個新值。 // 要刪除某個鍵值對,對其呼叫 unset() 函式。 |
使用 unset() 需要注意,此時陣列不會重建索引。需要重建索引,可以使用 array_values() 函式。
陣列計算總數: 使用 count() 函式
(6)Object 物件
123456789 | <?php class foo{
function do_foo(){
echo "Doing foo." ;
} } // 用 new 例項化一個類 $f = new foo; $f ->do_foo; |
(7)Resource 資源型別
資源 resource 是一種特殊的變數,儲存了到外部資源的一個引用。資源是通過專門的函式來建立和使用的。
(8)NULL
特殊的NULL 表示一個變數沒有值。NULL型別唯一可能的值就是NULL。
可被認定為NULL的變數:①被賦值為NULL ②尚未被賦值 ③被unset
(9)Callback 回撥型別
自PHP5.4 起,可以使用 callable 型別 指定回撥型別 callback。
六、變數
php中變數用一個美元符號 $ 後面跟變數名來表示的。區分大小寫。
變數預設總是傳值賦值。
12345678910 | <?php
$a = 1; // 值傳遞賦值 $b = $a // 引用賦值 $c = & $a
// global 關鍵字 global ; $GLOBALS |