0

Simple game to test user input for a secret word is not working as expected. Nothing is returned to the screen when conditions are evaluated. I'm pretty sure it is simple problem but most of the questions/answers here are more complicated than I believe looking for.

This is what i'm working with. Ask user to enter word of exactly 9 characters and it must include the @ symbol. All keyboard characters are live too. Echo to user if the requirement isn't met or success if it does.

<?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("&#64", $secret_word)) {
    echo "Secret word must contain &#64 sign"; }

if (strlen($secret_word) == 9 && preg_match("&#64", $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>
Martin
  • 22,212
  • 11
  • 70
  • 132
allendks45
  • 339
  • 5
  • 18

1 Answers1

1

Jeez, the more I look the more I see wrong with the question code here.... please read to the bottom of this answer...

echo $wordTest(); is asking PHP to return the result of a function, but the function has not been defined, you may instead be intending to echo $wordTest the variable which does not have brackets appended to it.

So

Change

echo $wordTest();

To

echo $wordTest;

P.S:

You would have discovered this yourself very easily if you had enabled PHP error reporting on your script, as implied by Fred-ii. Research Error reporting on StackOverflow.


P.P.S:

Your function is a mess, your function should return a value, not print direct to the screen. So you need to replace all occurances of the echo text with returning a variable which is the defined text, so:

function secretWord() {
if (strlen($secret_word) < 9) {
    echo "Secret word is too short!"; }

should become:

function secretWord() {
if (strlen($secret_word) < 9) {
    $var = "Secret word is too short!"; 
 }
 ...
 //etc. etc. do this for each text if statement... then:
return $var; //give the text value back to were the 
//function was called from.
} //this closes the function. 

This means that when you have:

$wordTest = secretWord();

That $wordTest will equal the value returned by the secretWord() function. Without return the value of $wordTest will always be NULL.


More information:

The scope of $secret_word [meaning where this value is defined, or not] is not inside the function that you declare, so you need to give this value to the function to get the correct response.

So: declare your function and place the variable in the brackets:

 function secretWord($givenWord) {

And then inside the function refactor all variables called $secret_word to $givenWord. Then outside of the function, you need to pass the secret word to your function by reference, so :

$wordTest = secretWord($secret_word);

or

echo secret_word($secret_word);

Alternatively because you're not actually doing anything else with the $wordTestvariable you don't really need it so in yout HTML you can just do:

<div class="output">
<p><?php  echo secretWord(); ?></p>

Which will echo out whatever the function returns.


Also $_POST inputs are going to not be HTML (special)characters, they will be characters in the character-set the form was submitted in, so your current search pattern is looking for a string of characters that would never appear as a substitute for another character.


You will need to apply each of the changes suggested here to all entities in the code that the changes apply to, I'm not rewriting your whole code for you.

Please read about PHP Variable Scopes and PHP user-defined functions.

Community
  • 1
  • 1
Martin
  • 22,212
  • 11
  • 70
  • 132
  • Appreciate the guidance and input. Been learning this for 2 weeks now. Much to learn and study. a bit different than js. Still have some tweaking to do for this program but thanks again! – allendks45 Mar 27 '16 at 21:33
  • @allendks45 it's all in the syntaxes, I have similar issues of syntax construction when I put together javascript. Good luck with it :-) – Martin Mar 27 '16 at 22:03
  • @allendks45 a couple of final notes: look up using PHP multibyte string functions (`mb_`) and also try and get into understanding (ie google!) PHP Object Orientated approach, it will make things like your `function` declaration much more future-proof. :-) – Martin Mar 27 '16 at 22:05