php - 如何打印错误而不显示html?

标签 php html error-handling

所以我有一个简单的index.php代码

<? php
    $number = $_GET['number']

    if (is_int($number) == FALSE) {
        // Don't show the below html, instead only show "ERROR: Expected a number"
    } else {
        $number = $number * 2 * 3 +1;
    }

    $number = $number - 10;
    // do some other php stuff here
?>

<html>
    <header>
        <title>Numbers are fun</title>
    </header>
    <u>Welcome to my cool website</u><br>
    <b>If you like numbers, you'll love this website!</b><br>
    <b>Your number is <?php echo $number ?></b>

    <footer>
        <br><br><b>This is the footer!!!</b>
    </footer>
</html>

我的问题是,我在php代码下面有html,因此,如果用户输入的字符串不是int,如何停止打印?我知道我可以在php中回显HTML,但是在代码中看起来很乱。

最佳答案

简单的解决方案:

    <? php
        $number = $_GET['number']

        if (is_int($value) == FALSE) {
            // Don't show the below html, instead only show "ERROR: Expected a number"
        } else {
    ?>

    <html>
    <header>
        <title>Numbers are fun</title>
    </header>
    <u>Welcome to my cool website</u><br>
    <b>If you like numbers, you'll love this website!</b><br>
    <b>Your number is <?php echo $number ?></b>

    <footer>
        <br><br><b>This is the footer!!!</b>
    </footer>
</html>
<?php } ?>

您也可以将PHP放在HTML内,以避免重复外部HTML结构。就像是:
<html>
<header>
    <title>Numbers are fun</title>
</header>
<u>Welcome to my cool website</u><br>
<b>If you like numbers, you'll love this website!</b><br>
<? php
    $number = $_GET['number']

    if (is_int($value) == FALSE) {
        // Don't show the below html, instead only show "ERROR: Expected a number"
        ?><b>An error occurred</b><?php 
    } else { ?>
        <b>Your number is <?php echo $number ?></b>
    <?php }
?>

<footer>
    <br><br><b>This is the footer!!!</b>
</footer>

关于php - 如何打印错误而不显示html?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53844191/

相关文章:

python - 跳过 for 循环中的错误行

html - 如何使用悬停叠加图像格式化文本

html - 在 bootstrap 的列上创建一个带有背景渐变的容器宽度

php - 浏览器关闭时自动注销

php - CodeIgniter 无法在本地主机上工作

javascript - 启动计时器后按钮不存在

mysql - 我如何通过使用 ORDER BY 和 LIMIT 的简单查询来解决这个问题?

android - 处理Android上的错误

php - 正则表达式:尝试使用无重音关键字修改重音句子

php - foreach 不返回单个值