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