Basit bir kelime için kullanıcı girdisini sınamak için basit bir oyun beklendiği gibi çalışmıyor. Koşullar değerlendirildiğinde hiçbir şey ekrana geri gönderilmez. Eminim bu basit bir problemdir, ancak buradaki soruların/cevapların çoğu aradığımdan daha karmaşıktır.Neden gizli kelime işlevim düzgün çalışmıyor
Çalıştığım şey budur. Kullanıcıdan tam olarak 9 karakterden oluşan bir kelime girmesini isteyin ve @ sembolünü içermelidir. Tüm klavye karakterleri de canlı. Gerekirse, gereksinimin karşılanmadığı veya başarı sağlanamadığı takdirde kullanıcıya eko yapın.
<?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 –
Belki de yanlıştır, çünkü ASCII karakterinin yerine @ html varlığını sınamaktasınız '' '' –
' preg_match ("/ @ /", $ secret_word). Ama eğer '' (strpos ($ secret_word, '@')! == false '' komutunu kullanabilirsiniz. –