How to use single quotes in single quotes?
Can someone explain how to make this code work?
echo '<div id="panel1-under">Welcome <?php echo "$_SESSION['username']"; ?></div>'; I tried to remove single quotes ( echo '<div id="panel1-under">Welcome <?php echo "$_SESSION[username]"; ?></div>'; ) but it does not work.
echo "<div id=\"panel1-under\">Welcome ".$_SESSION['username']."</div>"; or
echo '<div id="panel1-under">Welcome '.$_SESSION['username'].'</div>'; Short description:
- You do not need to re-open the tags inside the String echo ("...")
- What I did here is pass the "Greetings" string, combined with $ _SESSION ['username'] and "" (which the statement 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
Additional Information: PHP.net - echo
You need to combine strings and variables.
echo '<div id="panel1-under">Welcome ' . $_SESSION['username'] . '</div>'; Variable expansion does not work inside single quotes. You can do it:
echo "<div id=\"panel1-under\">Welcome {$_SESSION['username']}</div>"; or
echo '<div id="panel1-under">Welcome ' . $_SESSION['username'] . '</div>'; In single quotes, variable names are not parsed, as in double quotes. If you want to use single quotes here, you will 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 header is that to use a literal single quote inside a single quote string, you avoid the single quote using a backslash: echo 'Here is a single-quote: \''; Generally speaking, to use a single quote inside a string using a single quote as a delimiter, just avoid a single quote inside a string
echo 'That\ all, folks'; It is not clear what the purpose of your code is.
echo '<div id="panel1-under">Welcome <?php echo "$_SESSION['username']"; ?></div>'; Since you are already using PHP code, <?php echo not required. If you are trying to display the contents of a session variable, you can use
echo '<div id="panel1-under">Welcome ' . $_SESSION['username'] . '</div>'; Use the following code
$data = $session['user']; echo "a big string and $data thats simple"