<?php
$num_to_guess=42;if(!isset($_POST["guess"])){
$message="Welcome";
}else if($message = $_POST["guess"] > $num_to_guess){
$message=$_POST["guess"]."大了";
}else if($message = $_POST["guess"] < $num_to_guess){
$message=$_POST["guess"]."小了";
}else{
$message="正确";
}
?><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
<h1><?php echo $message; ?></h1>
<form  method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
  <label>猜数字:
  <input name="guess" type="text" />
  </label>
  <p>
    <label>
    <input type="submit" value="提交" />
    </label>
  </p>
</form></body>
</html>
1、$_SERVER是什么,$_SERVER["PHP_SELF"]是什么意思
2、这个猜数字游戏填了数字都能正确显示大或小或正确,但不填数字就总是转到显示“小了”,请问哪里错了

解决方案 »

  1.   

    $_SERVER is an array containing information such as headers, paths, and script locations.$_SERVER['PHP_SELF']:
    The filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/test.php/foo.bar would be /test.php/foo.bar.因为你填数字的时候$_POST["guess"]还是存在的只不是空值,所以你的第一个判断应该改为:
    if($_POST["guess"] == ""){
        $message="Welcome";
    }
      

  2.   

    1、$_SERVER是什么,$_SERVER["PHP_SELF"]是什么意思
    $_SERVER是服务器环境变量数组。$_SERVER["PHP_SELF"]当前程序的url地址(相对网站根)2、这个猜数字游戏填了数字都能正确显示大或小或正确,但不填数字就总是转到显示“小了”,请问哪里错了
    没有错!不填数字就判为0
      

  3.   

    可以用 var_dump 打印变量
      

  4.   

    <?php
    $num_to_guess=42;
    $num_tries=(!$_POST["num_trumies"]=="")?$num_tries+1:1;//记录猜的次数if($_POST["guess"]==""){
    $message="Welcome";
    }else if($message = $_POST["guess"] > $num_to_guess){
    $message=$_POST["guess"]."大了";
    }else if($message = $_POST["guess"] < $num_to_guess){
    $message=$_POST["guess"]."小了";
    }else{
    $message="正确";
    }
    $guess=$_POST["guess"];
    ?>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    </head><body>
    <?php echo $num_tries; ?>//显示猜的次数
    <h1><?php echo $message; ?></h1>
    <form  method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
      guess:
      <input name="guess" type="text" value="<?php echo $guess;?>" />
     
      <input type="hidden" name="num_tries" value="<?php echo $num_tries; ?>"/>增加的隐藏字段
      <p>
        
        <input type="submit" value="提交" />
        
      </p>
    </form></body>
    </html>还是猜数字,增加了一个隐藏字段来保存状态,记录猜的次数。
    问题是,次数总是1,不能增加