I am trying to make a game of hangman in php, where there a person is trying to guess the secret word. I am having trouble with it, I have the first part of the code done and will upload it as soon as I get back to my home computer. Im trying to get it so it loops the word and returns the right characters and records the characters I used.. I would appreciate any help.. Thanks

    try put the words in an array and count them then do a rand(1, $ammountOfWords); and then you can get the words but it would be better to be in javascript.

      bradmasterx wrote:

      but it would be better to be in javascript.

      But then I can view the page source and view the secret word in plain text.

      edit

      I would keep the secret word server side and use str_replace on the letters that have not been guessed, turning them into asterisks or something.

        I am doing an Array with a loop, to loop it through every letter and displaying the one that is right, the only problem is I am not quite sure how to do it, like i said i am a very novice coder who needs help. To top it off my dreamweaver is ****ed up so now I have to do it in a lab. Help would greatly relieve some stress.. Thanks guys

          Guys, this is really imporant.. I really need some help I would greatly appricate it....

            Hello,
            Just creates this on the fly and i am sure that this can be made a lot better...

            <?php 
            session_start();
            if($_GET['reset'] == 1){
            unset($_SESSION['input']);
            }
            ?>
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>Hang Man</title>
            </head>
            
            <body>
            <form action="hang.php" method="post">
              Letter:
              <input name="letter" type="text" size="1" maxlength="1"/>
            </form>
            
            <?php 
            $word = "h e l l o w o r l d";
            
            if (!isset( $_SESSION['input'] ) ) { 
            	$_SESSION['input'] = array();
            	}
            
            $_SESSION['input'][] = $_POST['letter'];
            
            $word = explode(" ",$word);
            
            foreach($word as $x){
            
            if(in_array($x, $_SESSION['input'])){
            echo $x;
            }else{
            echo " _ ";
            }
            }
            ?>
            <p><a href="?reset=1">Reset</a></p>
            </body>
            </html>
            

            The only thing i could not do is split a word into an array, i had to enter the spaces manually. Any help would be handy 🙂

              benracer wrote:

              The only thing i could not do is split a word into an array

              A string is just an array of characters.

              You can access each letter by it's subscript.

              i.e.

              $word = 'hello world';
              echo $word[0]; // prints h

              edit

              <?php
                $word = 'Hello World!';
                echo $word[0];
              
                echo '<br /><br />';
              
                $word_array = array();
              
                for($i = 0; $i < strlen($word); $i++)
                  $word_array[] = $word[$i];
              
                print_r($word_array);
              
              ?>
                Write a Reply...