Итак, у меня есть простой 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>
У меня вопрос, у меня есть HTML ниже php-кода, так как я могу остановить его от печати, если введенная пользователем строка не является int? Я знаю, что могу повторить HTML в php, но это выглядит грязно в коде.
Простое решение:
<? 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
$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>
Мы можем использовать HTML-код внутри PHP-кода эхо заявление
Чтобы не запутаться в коде, вы можете использовать этот онлайн-инструмент для преобразования 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
?>