用php写的一个猜数字的程序

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

 <?php
$count=rand(0,100);
$number = (isset($_POST['number'])) ? $_POST['number']:$count; if(!isset($_POST['guess'])){
$message = "please enter a number";
}
elseif(!is_numeric($_POST['guess'])){
$message = "please make sure you entered a number";
}
elseif($_POST['guess'] > $number){
$message = "too big";
}
elseif($_POST['guess'] < $number){
$message = "too small";
}
else if($_POST['guess'] == $number){
header("Location:success.php");
}
else{
$message = "so confused";
}
?> <!doctype html>
<html>
<head>
<title>A number guess game</title>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h1><?php echo $number?></h1>
<h1><?php echo $message?></h1>
<form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p><label for="guess">Type your answer</label> </p>
<input type="text" id="guess" name="guess"/>
<input type="hidden" name="number" value = "<?php echo $number?>" >
<button type="submit" name="submit" value="submit">Submit</button>
</form>
</body>
</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的安装信息

上一篇:C#高级编程 (第六版) 学习 第五章:数组


下一篇:C#高级编程 (第六版) 学习 第三章:对象和类型