12

Can anyone explain how to make this code work?

echo '<div id="panel1-under">Welcome <?php echo "$_SESSION['username']"; ?></div>';

I've tried removing the single quotes (echo '<div id="panel1-under">Welcome <?php echo "$_SESSION[username]"; ?></div>';), but it doesn't work.

apaderno
  • 28,547
  • 16
  • 75
  • 90
Joey Morani
  • 25,431
  • 32
  • 84
  • 131

7 Answers7

15
echo "<div id=\"panel1-under\">Welcome ".$_SESSION['username']."</div>";

or

echo '<div id="panel1-under">Welcome '.$_SESSION['username'].'</div>';

Quick Explain :

  • You don't have to reopen the tags inside a echo String (" ... ")
  • What I have done here is to pass the string "Welcome " concatenated to $_SESSION['username'] and "" (what the . operator does)
  • PHP is even smart enough to detect variables inside a PHP string and evaluate them :

    $variablename = "Andrew";

    echo "Hello $variablename, welcome ";

=> Hello Andrew, welcome

More infos : PHP.net - echo

Kami
  • 5,959
  • 8
  • 38
  • 51
  • 1
    PHP will replace variables inside a PHP string IF the string is in double quotes or a Heredoc. This is an important distinction. – JAL Aug 01 '10 at 21:49
  • +1 It's so in the example but I haven't mentionned it explicitly ! thx – Kami Aug 01 '10 at 23:12
  • 1
    `echo "
    Welcome {$_SESSION['username']}
    ";` Should also work?
    – Svish Aug 01 '10 at 23:32
9

Inside single quotes, variable names aren't parsed like they are inside double-quotes. If you want to use single-quoted strings here, you'll need to use the string concatenation operator, .:

echo '<div id="panel1-under">Welcome <?php echo "'.$_SESSION['username'].'"; ?></div>';

By the way: the answer to the question in the title is that in order to use a literal single-quote inside a single-quoted string, you escape the single-quote using a backslash:

echo 'Here is a single-quote: \'';
Hammerite
  • 21,755
  • 6
  • 70
  • 91
6

You need to concatenate your strings & variables.

echo '<div id="panel1-under">Welcome ' . $_SESSION['username'] . '</div>';
Ben Rowe
  • 28,406
  • 6
  • 55
  • 75
5

Variable expansion doesn't work inside of single quotes. You can do either:

echo "<div id=\"panel1-under\">Welcome {$_SESSION['username']}</div>";

or

echo '<div id="panel1-under">Welcome ' . $_SESSION['username'] . '</div>';
jasonbar
  • 13,333
  • 4
  • 38
  • 46
  • Cool! +1. I didn't know you can use assocative arrays like in the first example, I was suspicious if it'd work and I tested it myself and it actually works, although I'll probably never use it that way... – zz1433 Aug 01 '10 at 21:45
  • 1
    The first example will bring up a warning like "undefined constant 'username', assuming 'username'". It's best to use the bracket syntax:`echo "
    Welcome {$_SESSION['username']}
    ";
    – JAL Aug 01 '10 at 21:47
  • The {} method is also the only way to interpolate sub dimensions of a multimdimensional array inside a double-quoted string. echo "$a[b][c]" will ouput the equivalent of `$a[b] . "[c]"` (something like 'Array[c]'), as PHP's parser isn't greedy for dimensional references. – Marc B Aug 03 '10 at 04:53
3

Generally speaking, to use the single quote inside a string that is using the single quote as delimiter, you just escape the single quote inside the string.

echo 'That\'s all, folks';

It's not clear what the purpose of your code is, though.

echo '<div id="panel1-under">Welcome <?php echo "$_SESSION['username']"; ?></div>';

As you are already using PHP code, <?php echo is not necessary. If you are trying to output the content of a session variable, you can use the following code.

echo '<div id="panel1-under">Welcome ' . $_SESSION['username'] . '</div>';
apaderno
  • 28,547
  • 16
  • 75
  • 90
0

Use the following code

$data = $session['user'];
echo "a big string and $data   thats simple"
kba
  • 19,333
  • 5
  • 62
  • 89
0

You can use double or single quotes inside double quotes.

#Result: Hello "World" 's
$a = '"World"';   
echo "Hello $a 's";

#Result: Hello "World" 's    
echo "Hello \"World\" 's";

You can also use double or single quotes inside single quotes. (Not recommended when building SQL queries)

#Result: Hello World's "s
echo 'Hello World\'s "s';
csandreas1
  • 2,026
  • 1
  • 26
  • 48