简单的游戏来测试用户输入的秘密词是不是按预期工作。评估条件时没有任何东西返回到屏幕上。我很确定这是一个简单的问题,但这里的大多数问题/答案比我想要的要复杂得多。为什么我的秘密词功能不能正常工作
这就是我正在与之合作。要求用户输入正好9个字符的单词,并且必须包含@符号。所有的键盘字符都是现场直播。如果不符合要求,则向用户回传,如果符合要求则回应。
<?php
if (!isset($secret_word)) {
$secret_word = ''; }
/* prompt user to enter a secret word that contains 9 characters of which one must be @ sign and all keyboard characters are allowed. if the secret word isn't correct output what is wrong with the word. */
#get user input
$secret_word = filter_input(INPUT_POST, 'secret_word');
$wordTest = secretWord();
function secretWord() {
if (strlen($secret_word) < 9) {
echo "Secret word is too short!"; }
if (strlen($secret_word) > 9) {
echo "Secret word is too long!"; }
if (!preg_match("@", $secret_word)) {
echo "Secret word must contain @ sign"; }
if (strlen($secret_word) == 9 && preg_match("@", $secret_word)){
echo "$secret_word contains 9 characters and one sign.";}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="q4.css">
<title>Untitled Document</title>
</head>
<body>
<div class="header">
<header><h1>Secret Scroll Game</h1></header>
</div>
<div class="output">
<p><?php echo $wordTest(); ?></p>
</div>
<div class="link">
<a href="q4_index.html">Back To Homepage</a>
</div>
</body>
</html>
http://php.net/manual/en/function.error-reporting.php –
也许这是错误的,因为您正在测试html实体'&#64'而不是ASCII字符'@' –
' preg_match(“/ @ /”,$ secret_word)'。但是你可以使用'if(strpos($ secret_word,'@')!== false)''。 –