zoukankan      html  css  js  c++  java
  • 用php写的一个猜数字的程序

    写一个php的猜数字的小程序,提高自己对php的熟悉程度。

     1 <?php
     2 $count=rand(0,100);
     3 $number = (isset($_POST['number'])) ? $_POST['number']:$count;
     4 
     5 if(!isset($_POST['guess'])){
     6  $message =  "please enter a number";
     7 }
     8 elseif(!is_numeric($_POST['guess'])){
     9  $message = "please make sure you entered a number";
    10 }
    11 elseif($_POST['guess'] > $number){
    12  $message = "too big";
    13 }
    14 elseif($_POST['guess'] < $number){
    15  $message = "too small";
    16 }
    17 else if($_POST['guess'] == $number){
    18  header("Location:success.php");
    19 }
    20 else{
    21  $message = "so confused";
    22 }
    23 ?>
    24 
    25 <!doctype html>
    26 <html>
    27 <head>
    28  <title>A number guess game</title>
    29  <meta charset="UTF-8">
    30  <title>Document</title>
    31 </head>
    32 <body>
    33 <h1><?php echo $number?></h1>
    34 <h1><?php echo $message?></h1>
    35 <form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    36  <p><label for="guess">Type your answer</label> </p>
    37  <input type="text" id="guess" name="guess"/>
    38  <input type="hidden" name="number" value = "<?php echo $number?>" >
    39  <button type="submit" name="submit" value="submit">Submit</button>
    40 </form>
    41 </body>
    42 </html>

    总结:

    1.第2行代码:$number = (isset($_POST['number'])) ? $_POST['number']:$count; 这样写可以保证每次刷新页面时答案不会变化;

    2.第18行代码:header("Location:success.php");转去success.php页面;

    3.第35行代码:  action = "<?php echo $_SERVER['PHP_SELF']; ?>";传回本页面;

    4.第38行代码:<input type="hidden" name="number" value = "<?php echo $number?>" > 不在页面显示而传值;

    5.(不在本项目中):<?php phpinfo(); ?>输出PHP的安装信息

  • 相关阅读:
    GeoServer 发布矢量切片服务
    GeoServer 服务 端口修改
    Python 启动 FastAPI 报错 Error: [WinError 10013] 以一种访问权限不允许的方式做了一个访问套接字的尝试
    conda和pip的常用语句
    ArcGIS Pro 图层数据批量执行多部件转单部件
    ArcGIS Pro 中文(简体)语言包-指定路径为空
    2020 Ateneo de Manila University DISCS PrO HS Division
    N皇后问题-汇编解法
    HDU
    HDU
  • 原文地址:https://www.cnblogs.com/godlei/p/6501583.html
Copyright © 2011-2022 走看看