为什么变量不会连接?

时间:2016-09-22 22:04:53

标签: php

在页面上重新加载或提交它应该将猜测的单词添加到$猜测并添加一个管道。每次重新加载它都应该不添加替换。我将它存储在一个隐藏的元素中以保持页面加载,但它似乎没有工作。我做错了什么?

<html>
<body>
    <p style='text-align:center;'>
    <?php 
        $wordChoices = array("grape","apple","orange","banana","plum","grapefruit");//Course provided aray/code
        $randChoice;
        $tempVar;
        if(isset($_POST['gu']))
            $guesses=$_POST['gu'];
        else
            $guesses="";
        echo "|||||||||" . $guesses . "||||||||";
        if(empty($_POST['va']))
        {
            $randChoice=rand(0,5);
        }
        else
        {
            $randChoice=$_POST['va'];       
        }   

        //$randChoice = rand(0,5);//NEEDS FIX
        for($i=0;$i<=5;$i++)//output of array and green text
        {
            if($i==$randChoice)
                echo "<span style='color: green; text-align:center;'>$wordChoices[$i]</span>";//prints green word
            else
                echo "<span style='text-align:center;'>$wordChoices[$i]</span>";//print word
            if($i<5)//kills extrta "|"
                echo " | ";//print break between words

        }
    ?>
    </p>
    <h1 style='text-align:center;'>Word guess</h1>
    <div style='text-align:center'>
        <a href='lab1.php'>Refresh This Page</a>
    </div>
    <h3 style='text-align:center;'>Guess the word I'm thinking</h3>
    <form action="<?php $_SERVER['PHP_SELF'];?>" method='POST' style='text-align:center;'>
        <input type='text' name='tb1' placeholder=<?php echo $wordChoices[$randChoice];?>>
        <input type='submit' name='butt'>
        <input type='hidden' name='va' id='va' value="<?php echo $randChoice;?>">
        <input type='hidden' name='gu' id='gu' value="<?php echo $guesses;?>">
    </form>
    <?php 
        if($_POST)
        {
            if(!empty($_POST['tb1']))
            {
                $guesses += $_POST['tb1'] . "|";
            }
            if(isset($_POST['tb1']) && $_POST['tb1'] == $wordChoices[$randChoice])//checks to see if correct
                echo "<p style='text-align:center; color:red;'>You guessed " . $wordChoices[$randChoice]. " and that's CORRECT!!! (3)</p>";//prints out correct
            else if(!isset($_POST['tb1'])||$_POST['tb1']=="")//checks if nothing entered
                echo "<p style='text-align:center; color:red;'>Come on, enter something (2)</p>";//prints out message to enter text
            else if(isset($_POST['tb1']) && isValid($_POST['tb1']) && strtoupper($_POST['tb1'])!=strtoupper($wordChoices[$randChoice]))//checks for valid guess but incorrect term
                echo "<p style='text-align:center; color:red;'>Sorry " . $_POST['tb1'] . " is wrong. Try again (4)</p>";//prints out valid incorrect guess
            else if(isset($_POST['tb1']) && !isValid($_POST['tb1']))//checks if guess is valid
                echo "<p style='text-align:center; color:red;'>Hey, that's not even a valid guess. Try again (5).</p>";//prints out invalid
            echo "<ul>".$guesses."</ul>";
        }
        else 
            echo "<p style='text-align:center; color:red;'>It's time to play the guessing game! (1)</p>";

        function isValid($textResp)//checks is guessed word is a valid word
        {
            print($textResp);
            $wordChoices = array("grape","apple","orange","banana","plum","grapefruit");
            if($textResp==$wordChoices[0])
                return true;
            else if($textResp==$wordChoices[1])
                return true;
            else if($textResp==$wordChoices[2])
                return true;                    
            else if($textResp==$wordChoices[3])                 
                return true;
            else if($textResp==$wordChoices[4])
                return true;
            else if($textResp==$wordChoices[5])
                return true;
            else
                return false;

        }
    ?>
</body>

2 个答案:

答案 0 :(得分:2)

attr()Java或类似内容不同,拥有C# + -的运营商仅适用于号码。字符串具有用于字符串连接的*运算符。如果在字符串上使用. PHP将键入一个字符串到一个数字。底线使用+.

答案 1 :(得分:2)

您使用的是错误的赋值运算符; .=用于数学,$guesses += $_POST['tb1'] . "|"; 用于字符串连接。

演示:

  1. docs
  2. https://eval.in/647334
  3. 所以:

    $guesses .= $_POST['tb1'] . "|";
    

    应该是:

    <img src="my-image.jpg" class="img-responsive" />
    

    对于所有赋值运算符的细分,请参阅https://eval.in/647333上的第一个用户贡献。