在某个地方显示php变量的值

在某个地方显示php变量的值,php,Php,我编写了一段代码来显示HTML段落标记中某个特定位置的返回变量值,而不使用AJAX 我写了下面的代码,但它不适合我 <?php $Success = ""; $Failed = ""; ?> <br><br> <center> <h3>Already a user? Log in:</h3> <p style="color:green;"><?php echo $Success; ?&g

我编写了一段代码来显示HTML段落标记中某个特定位置的返回变量值,而不使用AJAX

我写了下面的代码,但它不适合我

<?php
$Success = "";
$Failed = "";
?>

<br><br>

<center>
    <h3>Already a user? Log in:</h3>
    <p style="color:green;"><?php echo $Success; ?></p>
    <p style="color:red;"><?php echo $Failed; ?></p>
    <form action="" method="POST">
        <div>Username:</div> <input type="text" name="username">
        <div>Password:</div> <input type="password" name="password">
        <br><br>
        <input type="submit" value="Log In" name="login">
    </form>
</center>

<?php
if(isset($_POST['login']))
{
    if( !empty($_POST['username']) && !empty($_POST['password']) )
    {
        $Success = "Thanks.";
    }
    else
    {
        $Failed = "You must supply a username and password.";
    }
}
?>

Php脚本逐行编译,所以在使用这些脚本之前必须分配变量。您可以使用以下选项:

<?php
$Success = "";
$Failed = "";

if(isset($_POST['login']))
{
    if( !empty($_POST['username']) && !empty($_POST['password']) )
    {
        $Success = "Thanks.";
    }
    else
    {
        $Failed = "You must supply a username and password.";
    }
}
?>

<br><br>

<center>
    <h3>Already a user? Log in:</h3>
    <p style="color:green;"><?php echo $Success; ?></p>
    <p style="color:red;"><?php echo $Failed; ?></p>
    <form action="" method="POST">
        <div>Username:</div> <input type="text" name="username">
        <div>Password:</div> <input type="password" name="password">
        <br><br>
        <input type="submit" value="Log In" name="login">
    </form>
</center>

请考虑以下几点:兄弟,我知道通过这样做,它会起作用。但我不想在页面的顶部写php代码,这是不可能的!为什么不想呢?在页面顶部编写PHP代码不是一种好的编程技术。谁告诉你们的?没人:假设:P