如何打印错误而不显示HTML?

时间:2018-12-19 03:30:42

标签: 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,但是在代码中看起来很乱。

3 个答案:

答案 0 :(得分:2)

简单解决方案:

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

答案 1 :(得分:0)

您可以尝试以下方法:

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

    if (is_int($number) == FALSE) {
        exit('Expecting the entered number to be an integer');
    } 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>

答案 2 :(得分:0)

我们可以通过 echo 语句

在php代码中使用html代码

为了不使代码混乱,您可以使用此在线工具将HTML代码转换为PHP代码

https://codebeautify.org/html-to-php-converter

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

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

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

$number = $number - 10;
?>

另一种方法是

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

if (is_int($number) == FALSE) {
    print_r("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 
    $number = $number * 2 * 3 +1;
}

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