$_SESSION['familyName'] = $userData['familyName'];
$_SESSION['givenName'] = $userData['givenName'];
想要將$userData['familyName']和$userData['givenName']
一起存在Session['Name'],顯示出來的結果是familyName+givenName
有試著寫成$userData['familyName,givenName'];但沒有反應
想知道正確可行的寫法~謝謝
php 字串串接請使用 . (英文句點)
例如:
$_SESSION['name'] = $userData['familyName'] . $userData['givenName']
不過以上寫法有個問題,就是當 $userData['familyName'] 與 $userData['givenName'] 更新時,$_SESSION['name'] 的值不會更新。
如果希望值會同步更新,可以使用 function 來完成:
function getName()
{
return ($userData['familyName'] . $userData['givenName']);
}
雖然我不是寫php,也許你可以試試下面的寫法
Session['Name']=$userData['familyName'] + $userData['givenName'];
字串相接:
$_SESSION['name'] = $userData['familyName'] . $userData['givenName'];
以陣列存入:
$_SESSION['name'] = [$userData['familyName'], $userData['givenName']];
以陣列存入並保留key值:
$_SESSION['name']['familyName'] = $userData['familyName'];
$_SESSION['name']['givenName'] = $userData['givenName'];
變成MIXED DATA
$_SESSION['fullName'] =[$userData['familyName'], $userData['givenName']];
之後
echo $_SESSION['fullName'][0];
echo $_SESSION['fullName'][1];