开发者

php, sessions, arrays - how to assign strign to the end of the session array

开发者 https://www.devze.com 2023-03-09 17:12 出处:网络
I have this small code , why it does not work and how to make it correctly ? $temp = $_SESSION[\'contactPersonInterest\'][$i];

I have this small code , why it does not work and how to make it correctly ?

$temp = $_SESSION['contactPersonInterest'][$i];
$temp += ',Medlemskort';
//$_SESSION['contactPersonIntere开发者_Python百科st'][$i] = $temp;

I am testing it with

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

And what i get is :

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

Whats wrong ?

Thank you


String concatenation is done with . in PHP. Try:

$temp .= ',Medlemskort';

Otherwise you perform addition, and if both strings don't start with numbers, they will be converted to 0 and 0 + 0 = 0 :)

Have a look at Type Juggling.


That's because += is an operator for adding integers, not strings. You want to concatenate strings (which is "."). Also, there is no need to create a temporary variable, only to overwrite the existing one. This should work:

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


You wrongly assign more things to the variable via +. You should use . instead.

$temp .= ',Medlemskort';


If you want $i to have temp's value, no need for the +=:

$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';

Hope this makes sense, good-luck

0

精彩评论

暂无评论...
验证码 换一张
取 消