php 回显整个 html 而不使用 readfile

php echo entire html without using readfile

如标题,是否可以回显整个html和css? 我正在使用 if's,每次匹配都需要生成不同的页面。

此外,在 html 代码中的 html 代码中,我需要回显包含在 div 中并设置样式的变量。

有什么想法可以实现吗?

我收到以下错误 语法错误,意外 T_STRING,预计

谢谢

    echo '<!DOCTYPE html><html lang="mk">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

            <style type="text/css">
            @import url(http://fonts.googleapis.com/css?family=Roboto&subset=cyrillic);
            @import url(http://fonts.googleapis.com/css?family=Open+Sans&subset=cyrillic);
            @import url(http://fonts.googleapis.com/css?family=Fjalla+One);

            *:focus { outline: none; }
            ::-webkit-scrollbar { width: 0; }
            .animate { -webkit-transition: all 0.2s ease-in-out; -moz-transition: all 0.2s ease-in-out; -o-transition: all 0.2s ease-in-out; transition: all 0.2s ease-in-out; }

            body { min-width: 300px; width: auto; max-width: 1100px; height: auto; margin: 0 auto; padding: 0; background-color: white; }

            div#container {
                position: relative; float: left; clear: none;
                width: 100%; height: auto; margin: -3px 0 30px 0; padding: 0 0 50px 0;
                background-image: url(guide.jpg); background-position: center bottom; background-repeat: no-repeat; background-size: cover;
                background-color: pink;
                -webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px;
            }

            html body div#container form {
                position: relative; float: left; clear: none;
                width: 100%; height: auto; margin: 40px 0 0 0; padding: 0;
            }

            html body div#container form div#email-container {
                position: relative; float: left; clear: none; display: block;
                width: 100%; height: auto; margin: 0 auto; padding: 0; overflow: hidden;                
                text-align: center;
            }
            html body div#container form div#email-container div#email-input {
                position: relative; float: none; clear: none; display: inline-block;

                width: auto; height: auto; margin: 0 auto; padding: 25px 40px;
                font-family: 'Fjalla One', sans-serif; font-weight: 400; font-size: 40px; letter-spacing: normal;

                -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px;
                background-color: rgba(26, 60, 88, 0.9); color: #ffffff;
                cursor: default;
            }


            label.styled {
                position: relative; float: left; clear: none; display: block;
                width: auto; height: auto; margin: 0; padding: 10px 20px; overflow: hidden;

                font-family: 'Roboto', sans-serif; font-weight: 400; font-size: 15px; letter-spacing: normal;

                -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px;
                background-color: #ffffff; color: rgba(128, 130, 132, 1); box-shadow: inset 0 0 0 2px #b6b6b8; 
                cursor: pointer;
            }

            label.styled:before {
                position: absolute; float: none; clear: both; top: 0; left: 0; display: block;
                width: 40px; height: 40px; margin: 0 0 0 10px; padding: 0; 

                content: '✔';
                font-size: 60px;

                opacity: 0;
            }

            input.styled {
                display: none;
            }

input.styled:checked ~ label.styled {
  padding-left: 50px;
  opacity: 1;

  background-color: rgba(128, 130, 132, 0.9); color: #ffffff; box-shadow: inset 0 0 0 2px rgba(128, 130, 132, 0.9);;
}
input.styled:checked ~ label.styled:before {
  top: -10px; left: -2px;
  opacity: 1; color: rgba(255, 255, 255, 0.4);
}


        </style>
</head>
<body>

    <div id="container" class="animate">


    <form method="post" action="$action" class="animate">

        <input type="hidden" name="mode_login">
        <input type="hidden" name="redirect" value="$redirect">
        <input type="hidden" name="accept_terms" value="yes">

            <div id="email-container">
            <div id="email-input">' . $_POST['fes-email'] . '</div>
            </div>

    </div>


    <input type="checkbox" class="styled animate" id="checkbox-tnc-accept">
    <label class="styled animate" for="checkbox-tnc-accept">Ги прифаќам Условите и правилата <br> за користење на овој сервис</label>

    <button type="submit" value="Enter">Продолжи</button>

    </form>




</body>
</html>';
echo '...assuming the previous part of the entire code are also present...
<form>    
<div id="email-input">' . $_POST['fes-email']; . '</div>
</form>
...';

请注意,在 $_POST 之前,我删除了回显;如果您已经在回显,则没有理由开始回显。当你 post ' 。 $_POST['fes-email'] 。 ' 你只是允许一个变量;它一直是 运行 回声,所以所有新的回声都会混淆它。如果您必须以这种方式拥有它,那么最好按照

的方式拥有一些东西
  • htdocs(文件夹)
  • index.php(文件)
  • 包括(文件夹)
  • includes/YourForm.php 然后在 YourForm.php 中添加你想要的 HTML 然后在 index.php 中添加

    require_once("includes/YourForm.php");

虽然我不同意你现在的做法,但我 posted 应该行得通。 不确定为什么 require 没有格式化为代码。抱歉。

我建议你不要用 php 打印 html 东西,关闭 php 标签并写正常的 html,所以这里我去掉了大回声声明并写正常 html。这使代码更干净,您可以更好地了解什么是 html 以及 PHP.

在哪里

所以你的 php 代码:

<div id="email-input">' . $_POST['fes-email'] . '</div>

如果你在 echo 语句中没有它,就使用这个:

<div id="email-input"><?= $_POST['fes-email'] ?></div>

这与:

几乎相同
<div id="email-input"><?php echo $_POST['fes-email']; ?></div>

对于有用的错误消息,您可以使用以下行打开错误报告:

<?php
    ini_set("display_errors", 1);
    error_reporting(E_ALL);
?>

您可以将它们放在 php 文件的顶部,并确保它们仅在暂存时打开!

整个代码应如下所示:

<!DOCTYPE html>
<html lang="mk">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

            <style type="text/css">

                @import url(http://fonts.googleapis.com/css?family=Roboto&subset=cyrillic);
                @import url(http://fonts.googleapis.com/css?family=Open+Sans&subset=cyrillic);
                @import url(http://fonts.googleapis.com/css?family=Fjalla+One);

                *:focus { outline: none; }
                ::-webkit-scrollbar { width: 0; }
                .animate { -webkit-transition: all 0.2s ease-in-out; -moz-transition: all 0.2s ease-in-out; -o-transition: all 0.2s ease-in-out; transition: all 0.2s ease-in-out; }

                body { min-width: 300px; width: auto; max-width: 1100px; height: auto; margin: 0 auto; padding: 0; background-color: white; }

                div#container {
                    position: relative; float: left; clear: none;
                    width: 100%; height: auto; margin: -3px 0 30px 0; padding: 0 0 50px 0;
                    background-image: url(guide.jpg); background-position: center bottom; background-repeat: no-repeat; background-size: cover;
                    background-color: pink;
                    -webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px;
                }

                html body div#container form {
                    position: relative; float: left; clear: none;
                    width: 100%; height: auto; margin: 40px 0 0 0; padding: 0;
                }

                html body div#container form div#email-container {
                    position: relative; float: left; clear: none; display: block;
                    width: 100%; height: auto; margin: 0 auto; padding: 0; overflow: hidden;                
                    text-align: center;
                }
                html body div#container form div#email-container div#email-input {
                    position: relative; float: none; clear: none; display: inline-block;

                    width: auto; height: auto; margin: 0 auto; padding: 25px 40px;
                    font-family: 'Fjalla One', sans-serif; font-weight: 400; font-size: 40px; letter-spacing: normal;

                    -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px;
                    background-color: rgba(26, 60, 88, 0.9); color: #ffffff;
                    cursor: default;
                }


                label.styled {
                    position: relative; float: left; clear: none; display: block;
                    width: auto; height: auto; margin: 0; padding: 10px 20px; overflow: hidden;

                    font-family: 'Roboto', sans-serif; font-weight: 400; font-size: 15px; letter-spacing: normal;

                    -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px;
                    background-color: #ffffff; color: rgba(128, 130, 132, 1); box-shadow: inset 0 0 0 2px #b6b6b8; 
                    cursor: pointer;
                }

                label.styled:before {
                    position: absolute; float: none; clear: both; top: 0; left: 0; display: block;
                    width: 40px; height: 40px; margin: 0 0 0 10px; padding: 0; 

                    content: '✔';
                    font-size: 60px;

                    opacity: 0;
                }

                input.styled {
                    display: none;
                }

                input.styled:checked ~ label.styled {
                  padding-left: 50px;
                  opacity: 1;

                  background-color: rgba(128, 130, 132, 0.9); color: #ffffff; box-shadow: inset 0 0 0 2px rgba(128, 130, 132, 0.9);;
                }
                input.styled:checked ~ label.styled:before {
                  top: -10px; left: -2px;
                  opacity: 1; color: rgba(255, 255, 255, 0.4);
                }


        </style>

    </head>

    <body>

        <div id="container" class="animate">

        <form method="post" action="$action" class="animate">

            <input type="hidden" name="mode_login">
            <input type="hidden" name="redirect" value="$redirect">
            <input type="hidden" name="accept_terms" value="yes">

            <div id="email-container">
                <div id="email-input"><?= $_POST['fes-email'] ?></div>
            </div>

        </div>


            <input type="checkbox" class="styled animate" id="checkbox-tnc-accept">
            <label class="styled animate" for="checkbox-tnc-accept">Ги прифаќам Условите и правилата <br> за користење на овој сервис</label>

            <button type="submit" value="Enter">Продолжи</button>

    </form>




    </body>
</html>

编辑:

如果你在 if 语句中有这个 echo 语句,你仍然不需要那样打印它:D

举个例子(伪代码):

<?php

    if(condition) {
        echo "<div>HTML STUFF</div>";  //Don't do it that way
    } else {
?>

<div>Put your HTML stuff here</div>

<?php
    }
?>

此外,如果您必须连接东西:

                   //v Your variable here
  "String is here" . $myVariable . " string goes further"
//^String start  ^ ^Concatenate  ^ ^                    ^String ends
              // |Break the string |

有关串联的更多信息,请参阅手册:http://php.net/manual/en/language.operators.string.php

我还建议您不要编写 CSS 内联或样式标签。您可以使用 *.css 编写自己的 CSS 文件,然后将它们包含在 html head 标签中:

<link rel="stylesheet" type="text/css" href="yourCssFile.css">

为什么要在 PHP 文件中回显 HTML。你可以做的是,你可以在你的条件中包含 HTML 个文件。示例:-

if(condition 1) 
{
   include "one.php";
}
else if(condition 2)
{
  include "two.php";
}

在你的 one.php 和 two.php 中你有你的 HTML ,在你的情况下你的所有 HTML 将转到这些文件之一。在该文件中,您可以回显您的变量

<div id="email-input"><?php echo $_POST['fes-email']; ?></div>