簡體   English   中英

PHP模具功能

[英]PHP die function

我如何使用php die功能僅停止加載某些div? 我了解它可能與die完全不一樣,但是我如何才能停止僅加載特定的div? 因此,我想學習的“ col”和“ col last”不是要加載的,而是要加載div id欄。 另外,如何使它在if語句中的H2標記不適用於else語句中的表?

<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>

你不能 die()向PHP指示停止進一步處理。

但是,這將起作用:

<?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 ?>

然后在其他地方

<?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 ?>

else部分中,只需使用echo()顯示想要顯示的任何文本,然后exit 您不需要die()

使用if塊:

<?php if($stuff): ?>
    <div></div>
<?php endif; ?>

“死”是一個句號; 有多種方法可以攔截它,但沒有便捷的方法。 在稍微不同的情況下,“返回”可能會執行您需要的操作(因為它僅退出當前函數或文件)。

“所以我不希望學習“ col”和“ col last”不加載”。 用鼠標突出顯示這些行。 點擊刪除鍵:)

如果您不希望它們顯示,請使用if else語句。 它將首先查看if部分,如果為true,則執行代碼,如果為false,將移至else語句並嘗試。

請不要死! 不要放棄希望-總是有其他選擇。

嘗試從條件語句中輸出標記。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM