使用... 運算子定義變長參數函數(PHP 5 >= 5.6.0, PHP 7)
現在可以不依賴func_get_args(), 使用.. . 運算子來實現變長參數函數。
<?php function f($req, $opt = null, ...$params) { // $params 是一个包含了剩余参数的数组 printf('$req: %d; $opt: %d; number of params: %d'."\n", $req, $opt, count($params)); } f(1); f(1, 2); f(1, 2, 3); f(1, 2, 3, 4); f(1, 2, 3, 4, 5); ?>
以上例程會輸出:
$req: 1; $opt: 0; number of params: 0
$req: 1; $opt: 2; number of params : 0
$req: 1; $opt: 2; number of params: 1
$req: 1; $opt: 2; number of params: 2
$req: 1; $opt: 2 ; number of params: 3
使用... 運算子進行參數展開(PHP 5 >= 5.6.0, PHP 7)
在呼叫函數的時候,使用... 運算符, 將陣列和可遍歷物件展開為函數參數。 在其他程式語言,例如 Ruby中,這被稱為連接運算符,。
<?php function add($a, $b, $c) { return $a + $b + $c; } $operators = [2, 3]; echo add(1, ...$operators); ?>
以上例程會輸出:
6
#use function 以及use const (PHP 5 >= 5.6.0, PHP 7)
use 運算子被進行了擴展以支援在類別中導入外部的函數和常數。 對應的結構為 use function 和 use const。
<?php namespace Name\Space { const FOO = 42; function f() { echo FUNCTION."\n"; } } namespace { use const Name\Space\FOO; use function Name\Space\f; echo FOO."\n"; f(); } ?>
以上程式會輸出:
42
Name\Space\f
debugInfo() (PHP 5 > = 5.6.0, PHP 7)
加入debugInfo(), 當使用var_dump() 輸出物件的時候, 可以用來控制要輸出的屬性和值。
<?php class C { private $prop; public function construct($val) { $this->prop = $val; } public function debugInfo() { return [ 'propSquared' => $this->prop ** 2, ]; } } var_dump(new C(42)); ?>
以上例程會輸出:
object(C)#1 (1) { ["propSquared"]=> int(1764) }
標量類型宣告(PHP 7)
標量型別宣告有兩種模式:強制(預設) 和嚴格模式。 現在可以使用下列型別參數(無論是強制模式或嚴格模式): 字串(string), 整數 (int), 浮點數 (float), 以及布林值 (bool)。它們擴充了PHP5中引入的其他類型:類別名,接口,數組和 回呼類型。
<?php // Coercive mode function sumOfInts(int ...$ints) { return array_sum($ints); } var_dump(sumOfInts(2, '3', 4.1));
以上例程會輸出:
int(9)
要使用嚴格模式,一個 declare 宣告指令必須放在檔案的頂端。這意味著嚴格聲明標量是基於文件可配的。 這個指令不僅影響參數的型別聲明,也影響函數的回傳值聲明(參見回傳值型別宣告, 內建的PHP函數以及擴充中載入的PHP函數)
傳回值類型宣告(PHP 7)
PHP 7 增加了對傳回類型宣告的支援。 類似於參數類型聲明,傳回類型聲明指明了函數傳回值的類型。可用的類型與參數聲明中可用的類型相同。
<?php function arraysSum(array ...$arrays): array { return array_map(function(array $array): int { return array_sum($array); }, $arrays); } print_r(arraysSum([1,2,3], [4,5,6], [7,8,9]));
以上程式會輸出:
Array
(
[0] => 6
[1] => 15
[2] => 24
)
null合併運算子(PHP 7)
由於日常使用中存在大量同時使用三元表達式和isset()的情況, 我們加入了null合併運算子(??) 這個語法糖。如果變數存在且值不為NULL, 它就會傳回自身的值,否則傳回它的第二個運算元。
<?php // Fetches the value of $_GET['user'] and returns 'nobody' // if it does not exist. $username = $_GET['user'] ?? 'nobody'; // This is equivalent to: $username = isset($_GET['user']) ? $_GET['user'] : 'nobody'; // Coalesces can be chained: this will return the first // defined value out of $_GET['user'], $_POST['user'], and // 'nobody'. $username = $_GET['user'] ?? $_POST['user'] ?? 'nobody'; ?>
太空船運算子(組合比較子)(PHP 7)
太空船運算子用於比較兩個表達式。當$a小於、等於或大於$b時它分別回傳-1、0或1。 比較的原則是沿用 PHP 的常規比較規則進行的。
<?php // Integers echo 1 <=> 1; // 0 echo 1 <=> 2; // -1 echo 2 <=> 1; // 1 // Floats echo 1.5 <=> 1.5; // 0 echo 1.5 <=> 2.5; // -1 echo 2.5 <=> 1.5; // 1 // Strings echo "a" <=> "a"; // 0 echo "a" <=> "b"; // -1 echo "b" <=> "a"; // 1 ?>
透過 define() 定義常數數組 (PHP 7)
Array 類型的常數現在可以透過 define() 來定義。在 PHP5.6 中僅能透過 const 定義。
<?php define('ANIMALS', [ 'dog', 'cat', 'bird' ]); echo ANIMALS[1]; // outputs "cat" ?>
以上是php7函數,聲明,傳回值等新特性介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

tostartaphpsession,usesesses_start()attheScript'Sbeginning.1)placeitbeforeanyOutputtosetThesessionCookie.2)useSessionsforuserDatalikeloginstatusorshoppingcarts.3)regenerateSessiveIdStopreventFentfixationAttacks.s.4)考慮使用AttActAcks.s.s.4)

會話再生是指在用戶進行敏感操作時生成新會話ID並使舊ID失效,以防會話固定攻擊。實現步驟包括:1.檢測敏感操作,2.生成新會話ID,3.銷毀舊會話ID,4.更新用戶端會話信息。

PHP会话对应用性能有显著影响。优化方法包括:1.使用数据库存储会话数据,提升响应速度;2.减少会话数据使用,只存储必要信息;3.采用非阻塞会话处理器,提高并发能力;4.调整会话过期时间,平衡用户体验和服务器负担;5.使用持久会话,减少数据读写次数。

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

phpIdentifiesauser'ssessionSessionSessionCookiesAndSessionId.1)whiwsession_start()被稱為,phpgeneratesainiquesesesessionIdStoredInacookInAcookInAcienamedInAcienamedphpsessIdontheuser'sbrowser'sbrowser.2)thisIdallowSphptpptpptpptpptpptpptpptoretoreteretrieetrieetrieetrieetrieetrieetreetrieetrieetrieetrieetremthafromtheserver。

PHP會話的安全可以通過以下措施實現:1.使用session_regenerate_id()在用戶登錄或重要操作時重新生成會話ID。 2.通過HTTPS協議加密傳輸會話ID。 3.使用session_save_path()指定安全目錄存儲會話數據,並正確設置權限。

phpsessionFilesArestoredIntheDirectorySpecifiedBysession.save_path,通常是/tmponunix-likesystemsorc:\ windows \ windows \ temponwindows.tocustomizethis:tocustomizEthis:1)useession_save_save_save_path_path()


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

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

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

Dreamweaver CS6
視覺化網頁開發工具

Dreamweaver Mac版
視覺化網頁開發工具

SublimeText3 Linux新版
SublimeText3 Linux最新版