Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6802077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:10:44+00:00 2026-05-26T19:10:44+00:00

I know that in php I can put a variable name inside a quoted

  • 0

I know that in php I can put a variable name inside a quoted string when I use echo, but I apparently can’t do this with a session variable. Can anyone explain why?

Here is the code, with the “offending” php commented out:

<?php
session_start();
$test = 100;
$_SESSION['test'] = 200;
?>
<html>
  <head>
    <title>Test</title>
  </head>
  <body>
  <p><?php echo($test."<br />");?></p>
  <p><?php echo("$test"."<br />");?></p>
  <p><?php echo($_SESSION['test']."<br />");?></p>
  <p><?php //echo("$_SESSION['test']"."<br />");?></p>
  </body>
</html>

And the output looks like this:

100

100

200

But if I uncomment the offending code line:

  <p><?php echo("$_SESSION['test']"."<br />");?></p>

I get no output and the following error:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in - on line 14

So I can go on my merry way knowing how to do it correctly (just keep the session variable outside of the double quotes), but I would really like to understand why this doesn’t work for session variables.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:10:45+00:00Added an answer on May 26, 2026 at 7:10 pm

    Inside a double-quoted string you must enclose a complex variable (array or object property) in {}:

    <p><?php echo("{$_SESSION['test']}"."<br />");?></p>
    

    This isn’t an issue with $_SESSION specifically, but any array accessed by quoted keys. Note, that you can include a numerically indexed array value with wrapping in {}, as in "echo $array[2] is two";

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that this is a simple question for PHP guys but I don't
I know you can get a $_SERVER['HTTP_X_REQUESTED_WITH'] php variable if jquery has loaded. That's
I would like to know how to create a php function that can be
I know that PHP doesn't yet have native Enumerations. But I have become accustomed
I know that it's not possible to style any given PHP but I do
I use the PHP language for file upload and want to know that the
I know you can assign a function's return value to a variable and use
I realize that you can't 100% know that this will work in all browsers.
I know that PHP is compiled to byte code before it is run on
I know that it does in PHP, and I'm pretty sure it does in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.