简体   繁体   中英

How can you put html inside a php variable?

None of the other methods work so could you please help? Here is my code:

<?php
include("db.php");
If(isset($_SESSION['name']))
{
$main = "<h2>hey</h2>";
} 
else
{
$main = "<h2>welcome</h2>"
}
?>


<html>
<body>
<?php echo $main;?>
</body>
</html>

//db.php consists of

<?php
mysql_connect("localhost", "root", "");
mysql_select_db("notes");
echo mysql_error();
session_start();
?>

Whenever I run the code it always shows: welcome. No matter if I'm logged in or logged out of my website.

Your html is looks good but It seems that you did not set session_start() at the top of your php file or you did not set session variable.

try like this:

<?php
session_start();
if(isset($_SESSION['name']))
{
$main = "<h2>hey</h2>";
} 
else
{
$main = "<h2>welcome</h2>"
}
?>


<html>
<body>
<?php echo $main;?>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM