Skip to content
PHP Logo

Why does using $_SESSION['test'] in a string not work as expected?

When using $_SESSION['test'] in a string, it does not work as expected because PHP does not automatically parse variables inside single quotes. To solve this issue, you can concatenate the variable with the string using the dot (.) operator or use double quotes instead of single quotes to allow variable interpolation.

php
// Using concatenation
echo 'Session test value: ' . $_SESSION['test'];

// Using double quotes
echo "Session test value: {$_SESSION['test']}";