PHP语法误解


PHP syntax misinterpretation

好吧,我目前不知道写下面代码的最佳方式是什么。我很困惑如何逃离一个echo以包含更多的php,然后进行相同的echo。我有点理解你需要使用double和single"answers"。我已经包含了一个在查看源代码以及下面的代码时显示在chrome中的屏幕截图。

我设法在另一个页面上实现了这一点,但使用相同的方法似乎在这个页面上不起作用。

<?php echo "You are logged in as " . $_SESSION['user_name'] . "." ?>  

任何关于我哪里出错的指针或方向都将不胜感激,即使只是示例格式。

我的图像显示输出内容-https://i.stack.imgur.com/aKm1D.png

<?php session_start(); ?>
<?php if(isset($_SESSION['user_name']) && !empty($_SESSION['user_name'])){
echo "<!doctype html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<title>test</title>
<link rel='stylesheet' type='text/css' href='style.css'>
</head>
<body>
<div>
'.include 'inc/sidebar.php'; .'
<div id='content-wrapper'>
<div class='page-content'>
'.include 'inc/headerbar.php';.'
'.include 'inc/settings.php';.'
</div><!-- End Page Content -->
</div><!-- End Content Wrapper -->
</div><!-- End Page Wrapper -->
</body>
</html>";
}
else{
echo "You aren't logged in.";
} ?>

试试这样的东西:

<?php session_start(); ?>
<!doctype html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<title>test</title>
<link rel='stylesheet' type='text/css' href='style.css'>
</head>
<body>
<?php if(isset($_SESSION['user_name']) && !empty($_SESSION['user_name'])): ?>
    <div>
        <?php include 'inc/sidebar.php'; ?>
        <div id='content-wrapper'>
            <div class='page-content'>
                <?php include 'inc/headerbar.php'; ?>
                <?php include 'inc/settings.php'; ?>
            </div><!-- End Page Content -->
        </div><!-- End Content Wrapper -->
    </div><!-- End Page Wrapper -->
<?php else: ?>
    You aren't logged in.
<?php endif; ?>
</body>
</html>

这种代码让我想起了我们担心Y2K漏洞的日子。。。

PHP本身实际上就是一个模板引擎。这就是为什么<?php ... ?>标记集首先存在的原因。对于大量的静态HTML,您所需要做的就是退出PHP模式:

<?php session_start(); ?>
<?php if(isset($_SESSION['user_name']) && !empty($_SESSION['user_name'])){ ?>
<!doctype html>
<html lang='en'>
<head>
<meta charset='UTF-8'>