php练习一

1、直角星星矩阵

 1 <!DOCTYPE html>
 2 <html>
 3     <head>        
 4         <title>test</title>
 5     </head>
 6     <body>
 7     <?php        
 8         $n=10;
 9         for($i=0;$i<$n;$i++){        
10             for($j=0;$j<=$i;$j++){
11                 echo "*";
12             }
13             echo "<br>";            
14         }    
15     ?>
16     </body>
17 </html>

2、等腰星星矩阵

 1 <!DOCTYPE html>
 2 <html>
 3     <head>        
 4         <title>test</title>
 5     </head>
 6     <body>
 7     <?php        
 8         $n=5;
 9         for($i=1;$i<=$n;$i++){        
10             for($j=1;$j<=$n-$i;$j++){
11                 echo '&nbsp;';
12             }
13             for($k=1;$k<=$i*2-1;$k++){
14                 echo "*";
15             }
16             echo "<br>";            
17         }    
18     ?>
19     </body>
20 </html>

 3、空心金字塔

 1 <!DOCTYPE html>
 2 <html>
 3     <head>        
 4         <title>test</title>
 5     </head>
 6     <body>
 7     <?php        
 8         $n=5;
 9         for($i=1;$i<=$n;$i++){        
10             for($j=1;$j<=$n-$i;$j++){
11                 echo '&nbsp;';
12             }
13             for($k=1;$k<=$i*2-1;$k++){
14                 if($i==1||$i==$n){
15                     echo "*";
16                 }else{
17                     if($k==1||$k==$i*2-1){
18                         echo "*";
19                     }else{
20                         echo '&nbsp;';
21                     }
22                 }                
23             }
24             echo "<br>";            
25         }    
26     ?>
27     </body>
28 </html>

 4、棱形星型

 1 <!DOCTYPE html>
 2 <html>
 3     <head>        
 4         <title>test</title>
 5     </head>
 6     <body>
 7     <?php        
 8         $n=5;
 9         for($i=1;$i<=$n;$i++){        
10             for($j=1;$j<=$n-$i;$j++){
11                 echo '&nbsp;';
12             }
13             for($k=1;$k<=$i*2-1;$k++){
14                 if($i==1){
15                     echo "*";
16                 }else{
17                     if($k==1||$k==$i*2-1){
18                         echo "*";
19                     }else{
20                         echo '&nbsp;';
21                     }
22                 }                
23             }
24             echo "<br>";            
25         }    
26         for($i=$n;$i>=1;$i--){        
27             for($j=$n-$i;$j>=1;$j--){
28                 echo '&nbsp;';
29             }
30             for($k=$i*2-1;$k>=1;$k--){
31                 if($i==1){
32                     echo "*";
33                 }else{
34                     if($k==1||$k==$i*2-1){
35                         echo "*";
36                     }else{
37                         echo '&nbsp;';
38                     }
39                 }                
40             }
41             echo "<br>";            
42         }    
43     ?>
44     </body>
45 </html>

5、九九乘法表

 1 <!DOCTYPE html>
 2 <html>
 3     <head>        
 4         <title>test</title>
 5     </head>
 6     <body>
 7     <h1>九九乘法表</h1>
 8     <?php
 9         $n=9;
10         for($i=1;$i<=$n;$i++){
11             for($j=1;$j<=$i;$j++){
12                 $res=$i*$j;
13                 echo "$i*$j=$res &nbsp;&nbsp;";
14             }
15             echo "<br>";
16         }
17     ?>
18     </body>
19 </html>

 6、PHP计算器

index.html:

 1 <!DOCTYPE html>
 2 <html>
 3     <head>    
 4         <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
 5         <title>PHP计算器</title>
 6     </head>
 7     <body>
 8     <h1>PHP计算器</h1>
 9     <form action="res.php" method="post">
10         <table width="300px">
11             <tr>
12                 <td>num1</td>
13                 <td><input name="num1" type="text"></td>
14             </tr>
15             <tr>
16                 <td>num2</td>
17                 <td><input name="num2" type="text"></td>
18             </tr>
19             <tr>
20                 <td>选择运算符</td>
21                 <td>
22                     <select name="oper">
23                         <option>--------运算符--------</option>
24                         <option value="+">+</option>
25                         <option value="-">-</option>
26                         <option value="*">*</option>
27                         <option value="/">/</option>
28                     </select>
29                 </td>
30             </tr>
31             <tr>                
32                 <td colspan="2"><input value="计算结果" type="submit"></td>
33             </tr>
34         </table>
35     </form>
36     
37     </body>
38 </html>

 1 //res.php
  <?php 2 //接收用户从index.php(对应静态页面)提交的数据 3 //1、接收num1 4 //$_REQUEST,该方法可以接受用户的post或get请求数据 5 $num1=$_REQUEST["num1"];//num1对应的name 6 //2、接收num2 7 $num2=$_REQUEST["num2"];//num2对应的name 8 //3、接收oper 9 $oper=$_REQUEST["oper"];//oper对应的name 10 $res=0; 11 switch($oper){ 12 case "+": 13 $res=$num1+$num2; 14 break; 15 case "-": 16 $res=$num1-$num2; 17 break; 18 case "*": 19 $res=$num1*$num2; 20 break; 21 case "/": 22 $res=$num1/$num2; 23 break; 24 default: 25 echo "运算符号有误"; 26 } 27 echo "运算结果是:".$res; 28 29 ?> 30 <br> 31 <a href="index.php">返回计算器页面</a>

 7、简单的php表单验证实例(W3C)

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
 5     <title>提交表单</title>
 6     <style>
 7     .error{color:red;}
 8     </style>
 9 </head>
10 <body>
11 <?php
12     $name=$email=$gender=$comment=$website="";
13     $nameErr=$emailErr=$genderErr=$websiteErr="";
14     if($_SERVER["REQUEST_METHOD"]=="POST"){
15         if(empty($_POST["name"])){
16             $nameErr=" 姓名是必填字段";
17         }else{
18             $name=test_input($_POST["name"]);
19             //preg_match() 函数检索字符串的模式,如果模式存在则返回 true,否则返回 false。
20             if(!preg_match("/^[a-zA-z]*/",$name)){
21                 $nameErr="只允许字母和空格";
22             }
23         }
24         if(empty($_POST["e-mail"])){
25             $emailErr="邮箱是必填字段";
26         }else{
27             $email=test_input($_POST["e-mail"]);
28             if(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)){
29                 $emailErr="无效的Email格式";
30             }
31         }
32         if(empty($_POST["gender"])){
33             $genderErr="性别是必填字段";
34         }else{
35             $gender=test_input($_POST["gender"]);
36         }
37         if(empty($_POST["comment"])){
38             $comment="";
39         }else{
40             $comment=test_input($_POST["comment"]);
41         }
42         if(empty($_POST["website"])){
43             $websiteErr="网址是必填字段";
44         }else{
45             $website=test_input($_POST["website"]);
46             if(!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%
47                =~_|]/i",$website)){
48                 $websiteErr="无效的 URL 格式";
49             }
50         }            
51     }
52     function test_input($data){
53         $data=trim($data);//去除不必要的字符(多余的空格、制表符、换行)
54         $data=stripslashes($data);//删除用户输入数据中的反斜杠(\)
55         $data=htmlspecialchars($data);
56         return $data;
57     }
58     
59 ?>
60 <h1>PHP表单验证实例</h1>
61 <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
62     姓名:<input name="name" type="text" value="<?php echo $name;?>"><span class="error">* <?php echo $nameErr;?></span>
63     <br>
64     邮箱:<input name="e-mail" type="text" value="<?php echo $email;?>"><span class="error">* <?php echo $emailErr;?></span>
65     <br>
66     网址:<input name="website" type="text" value="<?php echo $website;?>"><span class="error" >* <?php echo $websiteErr;?></span>
67     <br>
68     评论:<textarea name="comment" rows="5" cols="40" ><?php echo $comment;?></textarea><br>
69     性别:<input type="radio" name="gender" <?php if(isset($gender)&& $gender="男") echo "checked";?> value="男">男
70     <input type="radio" name="gender" <?php if(isset($gender)&& $gender="女") echo "checked";?> value="女">女
71     <span class="error">* <?php echo $genderErr;?></span>
72     <br>
73     <input type="submit" name="submit" value="提交">
74 </form>
75 
76 </body>
77 </html>

 

posted @ 2015-04-29 13:20  U_can  阅读(242)  评论(0编辑  收藏  举报