php,sessions,arrays - 如何将字符串分配给会话数组的末尾

时间:2011-05-31 10:14:05

标签: php arrays session

我有这个小代码,为什么它不起作用以及如何正确使用它?

$temp = $_SESSION['contactPersonInterest'][$i];
$temp += ',Medlemskort';
//$_SESSION['contactPersonInterest'][$i] = $temp;

我正在用

进行测试
?><script>alert('<?php echo  $_SESSION['contactPersonInterest'][$i] ?>'+'----------'+'<?php echo $temp ?>');</script> <?php

我得到的是:

blbla,blll----------0

怎么了?

谢谢

4 个答案:

答案 0 :(得分:2)

String concatenation is done with . in PHP。尝试:

$temp .= ',Medlemskort';

否则您执行添加,如果两个字符串都不以数字开头,则它们将转换为00 + 0 = 0:)

查看Type Juggling

答案 1 :(得分:1)

那是因为+ =是添加整数而不是字符串的运算符。你想连接字符串(这是“。”)。此外,无需创建临时变量,仅覆盖现有变量。这应该有效:

$_SESSION['contactPersonInterest'][$i] .= ',Medlemskort';

答案 2 :(得分:0)

您错误地通过+为变量分配了更多内容。您应该使用.代替。

$temp .= ',Medlemskort';

答案 3 :(得分:0)

如果您希望$ i具有临时值,则不需要+ =:

$temp = ""; // good habit to initialize before usage
$temp = $_SESSION['contactPersonInterest'][$i];
$temp = ',Medlemskort';
$_SESSION['contactPersonInterest'][$i] = $temp;
// or even save a $temp
 $_SESSION['contactPersonInterest'][$i] = ',Medlemskort';

希望这是有道理的,祝你好运