參數 | 描述 | |||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
name | 常量的名稱。這個名稱可以是保留字,但不建議這樣做。 | |||||||||||||||||||||||||||||||||||||||||||||
value | 常量的值。這個值必須是一個標量值(整型、浮點、字符串、布爾值或NULL)或數組。 | |||||||||||||||||||||||||||||||||||||||||||||
case_insensitive | 常量是否區分大小寫,默認是區分大小寫的。在PHP 7.3.0中,不推薦採用不區分大小寫的方式。"}}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"創建PHP腳本_constant.php_並定義名爲"},{"type":"codeinline","content":[{"type":"text","text":"CONSTANT"}]},{"type":"text","text":"的常量,如下所示。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"define(\"CONSTANT\", \"Hello PHP\");"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們還可以使用"},{"type":"codeinline","content":[{"type":"text","text":"const"}]},{"type":"text","text":"關鍵字來定義常量:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"const CONSTANT_2 = 'Hello php';\nconst Constant = 'HELLO PHP';\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"define()"}]},{"type":"text","text":"函數可以用來定義數組常量:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"define(\"Catalog\", ['Oracle Magazine','Java Magazine']);"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"數組常量的值可以使用數組元素訪問的方式進行輸出。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"echo Catalog[0]\necho Catalog[1]\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"_constant.php_文件如下所示:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"\"; \nconst CONSTANT_2 = 'Hello php';\necho CONSTANT_2.\" \";\nconst Constant = 'HELLO PHP';\necho Constant.\" \";\ndefine(\"Catalog\", ['Oracle Magazine','Java Magazine']);\necho Catalog[0].\" \";\necho Catalog[1]\n?>\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"運行腳本將會輸出腳本中定義的常量值。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"Hello PHP\nHello php\nHELLO PHP\nOracle Magazine\nJava Magazine\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"全局定義的常量,如"},{"type":"codeinline","content":[{"type":"text","text":"TRUE"}]},{"type":"text","text":"或"},{"type":"codeinline","content":[{"type":"text","text":"FALSE"}]},{"type":"text","text":",不能進行重定義。爲了闡述這一點,我們創建一個_const.php_腳本,該腳本定義了常量"},{"type":"codeinline","content":[{"type":"text","text":"TRUE"}]},{"type":"text","text":",並將它的值設置成了"},{"type":"codeinline","content":[{"type":"text","text":"20"}]},{"type":"text","text":":"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你運行該腳本的話,它將會輸出的值是"},{"type":"codeinline","content":[{"type":"text","text":"1"}]},{"type":"text","text":",這也是"},{"type":"codeinline","content":[{"type":"text","text":"TRUE"}]},{"type":"text","text":"全局定義的值。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"綁定對象作用域到閉包的新函數"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"閉包是用來表示匿名函數的類。PHP 7.0引入了新的"},{"type":"codeinline","content":[{"type":"text","text":"Closure::call()"}]},{"type":"text","text":"函數,能夠非常簡便地將對象作用域臨時綁定到一個閉包並調用它。爲了闡述這一點,創建腳本_closure.php_並複製如下清單所示的代碼:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"getMsg();};\necho $getMsg->call(new Hello);\n?>\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在上述腳本中,Hello是一個具有函數"},{"type":"codeinline","content":[{"type":"text","text":"getMsg()"}]},{"type":"text","text":"的類。"},{"type":"codeinline","content":[{"type":"text","text":"Closure::call()"}]},{"type":"text","text":"函數用來創建一個"},{"type":"codeinline","content":[{"type":"text","text":"Hello"}]},{"type":"text","text":"實例,並綁定它的作用域到一個調用"},{"type":"codeinline","content":[{"type":"text","text":"getMsg"}]},{"type":"text","text":"方法的閉包。運行該腳本將會輸出"},{"type":"codeinline","content":[{"type":"text","text":"Hello"}]},{"type":"text","text":"消息。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Expectation"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在討論_expectation_之前,我們先看一下傳統的斷言。傳統的"},{"type":"codeinline","content":[{"type":"text","text":"assert()"}]},{"type":"text","text":"方法定義如下所示。它會檢查如果代碼的預期值(稱爲_斷言_)是不是爲"},{"type":"codeinline","content":[{"type":"text","text":"FALSE"}]},{"type":"text","text":",如果是"},{"type":"codeinline","content":[{"type":"text","text":"FALSE"}]},{"type":"text","text":"的話,它會打印出描述消息,並且默認會中止程序。如果斷言不是"},{"type":"codeinline","content":[{"type":"text","text":"FALSE"}]},{"type":"text","text":"的話,"},{"type":"codeinline","content":[{"type":"text","text":"assert()"}]},{"type":"text","text":"沒有任何效果。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"bool assert ( mixed $assertion [, string $description ] )"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"按照設計,斷言用於開發和測試期的調試,而不是運行期的操作。"},{"type":"codeinline","content":[{"type":"text","text":"assert()"}]},{"type":"text","text":"的行爲可以通過"},{"type":"link","attrs":{"href":"https:\/\/www.php.net\/manual\/en\/function.assert-options.php","title":"","type":null},"content":[{"type":"text","text":"assert_options()"}]},{"type":"text","text":"或者"},{"type":"codeinline","content":[{"type":"text","text":".ini"}]},{"type":"text","text":"文件進行配置。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"傳統上,"},{"type":"codeinline","content":[{"type":"text","text":"assert()"}]},{"type":"text","text":"的第一個參數應該是一個要評估計算的字符串或者作爲斷言進行測試的boolean條件。如果所提供的是一個字符串的話,它會以PHP代碼的形式進行評估計算。如果所提供的是一個boolean條件的話,那麼在傳遞給"},{"type":"codeinline","content":[{"type":"text","text":"assert_options()"}]},{"type":"text","text":"定義的斷言回調函數(如果存在的話)之前,它將會被轉換成字符串。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"斷言在PHP 7中進行了徹底修改,現在被稱爲_expectation_,並且"},{"type":"codeinline","content":[{"type":"text","text":"assert()"}]},{"type":"text","text":"是PHP 7中的一個語言結構。作爲對"},{"type":"codeinline","content":[{"type":"text","text":"assert()"}]},{"type":"text","text":"的增強,expectations被添加了進來,並且具有如下的語法:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"bool assert ( mixed $assertion [, Throwable $exception ] )"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有了expectations之後,"},{"type":"codeinline","content":[{"type":"text","text":"assert()"}]},{"type":"text","text":"的第一個參數可能是會返回一個值的表達式,而不再是一個要評估執行的PHP代碼字符串或者要測試的boolean條件。表達式會被執行,得到的結果會被用來確定斷言是否成功。從PHP 7開始,使用字符串作爲第一個參數已經被廢棄掉了。"},{"type":"codeinline","content":[{"type":"text","text":"assert_options()"}]},{"type":"text","text":"依然能夠與expectations協同使用,但是並不推薦這麼做。相反,我們應該使用兩個新的php.ini配置指令,如表2所示。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"表2 Expectations的配置指令"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"embedcomp","attrs":{"type":"table","data":{"content":"
|
PHP 7入門:函數的增強
{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"本文要點"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"要在運行時創建命名的數組常量,我們可以使用新的"},{"type":"codeinline","content":[{"type":"text","text":"define()"}]},{"type":"text","text":"函數。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"要將對象作用域綁定到一個變量並調用它,我們可以使用新的"},{"type":"codeinline","content":[{"type":"text","text":"Closure::call()"}]},{"type":"text","text":"函數。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"要通過傳統的"},{"type":"codeinline","content":[{"type":"text","text":"assert()"}]},{"type":"text","text":"使用表達式和\/或自定義"},{"type":"codeinline","content":[{"type":"text","text":"AssertionError"}]},{"type":"text","text":",那麼我們可以使用expectation。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"PHP 7支持從Generator函數返回一個值。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"PHP 7支持將一個Generator函數委託給另一個Generator函數。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對於整數除法,使用名爲"},{"type":"codeinline","content":[{"type":"text","text":"intdiv()"}]},{"type":"text","text":"的新函數。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"要覆蓋"},{"type":"codeinline","content":[{"type":"text","text":"php.ini"}]},{"type":"text","text":"中的會話配置的設置,可以使用新的"},{"type":"codeinline","content":[{"type":"text","text":"session_start"}]},{"type":"text","text":"函數。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"要使用回調執行正則表達式搜索和替換,使用新的函數"},{"type":"codeinline","content":[{"type":"text","text":"preg_replace_callback_array()"}]},{"type":"text","text":"。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"PHP 7能夠生成加密安全的整數和字節。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"PHP 7.1支持將回調轉換爲閉包。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"箭頭(=>)函數爲匿名函數提供了簡潔的語法。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在"},{"type":"link","attrs":{"href":"https:\/\/www.infoq.com\/articles\/php7-new-type-features\/","title":"","type":null},"content":[{"type":"text","text":"PHP 7系列前面的文章中"}]},{"type":"text","text":",我們討論了PHP類型系統的新特性。在本文中,我們將會探討PHP 7在函數方面的改善。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"PHP支持多種類型的函數,包括用戶自定義函數、內部函數、變量函數以及匿名函數。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"定義數組常量的新函數"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"PHP 7.0添加了名爲"},{"type":"codeinline","content":[{"type":"text","text":"define()"}]},{"type":"text","text":"的新函數,用來在運行時定義命名的數組常量。define()的語法如下所示:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"bool define ( string $name , mixed $value [, bool $case_insensitive = FALSE ] )"}]},{"type":"text","text":""}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"表1討論了該函數的參數"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":""},{"type":"text","marks":[{"type":"strong"}],"text":"表1 define()函數的參數"},{"type":"text","text":""}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"embedcomp","attrs":{"type":"table","data":{"content":"
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.