user893970
user893970

Reputation: 899

Displaying $_SESSION['name'] and $_SESSION['surname']

I am just trying to display name and surname by session. But,they returned 0. I don't understand why it returned 0 even though both of them are string. There is nothing interesting with the codes;

<?php 

    echo   $_SESSION['name']+ ""+$_SESSION['surname'];

?>

Upvotes: 1

Views: 987

Answers (2)

Mohit Bumb
Mohit Bumb

Reputation: 2493

echo $_SESSION['name'] ." ". $_SESSION['surname'];

Output will be like Name Surname for example : Mohit Bumb.

Upvotes: 0

alex
alex

Reputation: 490617

The period (.) is the string concatenation operator in PHP.

By using the addition operator (+), you are coercing the operands to numbers, of which their value is 0. And obviously, 0 + 0 + 0 = 0.

What you want is...

echo $_SESSION['name'] . $_SESSION['surname'];

I removed the empty string in the middle because it won't do anything. Perhaps you meant a space?

Upvotes: 7

Related Questions