首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP die函数

PHP die函数
EN

Stack Overflow用户
提问于 2012-05-09 06:24:56
回答 5查看 1.3K关注 0票数 1

我怎么能使用php die函数来仅停止加载某些div呢?我知道它可能不会和die完全一样,但是我怎么能只停止加载特定的div呢?所以我不想让learn "col“和"col”加载,而是让div id条加载。另外,如何使if语句中的H2标记不应用于else语句中的表?

代码语言:javascript
复制
<html>
<body>



<div id="header">
<div id="site">
    <h1><a href="#">site</a></h1>
    </div>

   <div id="menuholder">
    <ul id="menu">
        <li><a href="index.html">Home</a></li>
        <li class="active"><a href="about.php">about application</a></li>
        <li><a href="#">register</a></li>
        <li><a href="#">contact</a></li>
    </ul>
    </div>


</div>

<div id="teaser">
    <div class="wrap">
        <div id="image"></div>
        <div class="box">

<?php
session_start ();
if (isset($_SESSION['username']))
echo "<h2>Welcome, ".$_SESSION['username']."!</h2>";
else
die("You must be logged in to view this page. 
Register to gain access to learn more about the application.    
<form action='login.php' method='POST'>
<table>
<tr><td>Username: </td><td><input type='text' name='username'/></td><td>Password: </td> <td>              <input type='password' name='password'/></td> <td><input type='submit'  name='submit' value='Login'/></td></tr>
</table>
</form>
");

?>


            <p>Information about the application would go here</p>

        </div>

    </div>
</div>

<div id="bar">
    <div class="wrap">

    </div>
</div>



<div class="wrap">
    <div class="col">
        <h3>Learn <span class="red">More</span></h3>
        <p>More info </p>
    </div>
    <div class="col">
        <h3>User <span class="red">Statistics</span></h3>
        <p>More info</p>
    </div>
    <div class="col last">
        <h3>About <span class="red">Phapsy</span></h3>
        <p>More info</p>

    </div>
</div>

<div id="footer">
    <p class="right">Contact: </p>
</div>
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2012-05-09 06:28:01

你不能。die()指示PHP停止进一步的处理。

然而,这是可行的:

代码语言:javascript
复制
<?php
session_start ();
if (isset($_SESSION['username'])) {
    echo "<h2>Welcome, ".$_SESSION['username']."!</h2>";
    $auth = true;
} else {
    $auth = false; // Show the following HTML
?>
You must be logged in to view this page. 
Register to gain access to learn more about the application.    
<form action='login.php' method='POST'>
<table>
<tr><td>Username: </td><td><input type='text' name='username'/></td><td>Password: </td> <td>              <input type='password' name='password'/></td> <td><input type='submit'  name='submit' value='Login'/></td></tr>
</table>
</form>
<?php } // End ?>

然后在别处..。

代码语言:javascript
复制
<?php if ( $auth == true ) { // Display this HTML only if authenticated ?>
    <div class="col last">
        <h3>About <span class="red">Phapsy</span></h3>
        <p>More info</p>

    </div>
<?php } // End ?>
票数 4
EN

Stack Overflow用户

发布于 2012-05-09 06:27:59

else部件中,只需使用echo()exit显示您想要显示的任何文本。你不需要die()

票数 1
EN

Stack Overflow用户

发布于 2012-05-09 06:29:19

使用if块:

代码语言:javascript
复制
<?php if($stuff): ?>
    <div></div>
<?php endif; ?>

"die“是一个句号;有办法拦截它,但不是方便的方法。在略有不同的情况下,"return“可能会执行所需的操作(因为它只退出当前函数或文件)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10507437

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档