gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 08:10:25 25 4
gpt4 key购买 nike

所以我有一个简单的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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com