这个

素数查找

1
2
3
4
5
6
7
8
9
10
11
12
13
// 素数:只能被1和自身整除
for($i=1;$i<100;$i++)
{
for($j=1;$j<$i;$j++)
{
if(($i%$j)==0)
{
$i++;
continue; //不是继续查找
}
}
echo $i."\n";
}

三元排大小

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
echo "<br>";
($a==$b&&$b==$c)?(
print "$a=$b=$c"
):(
($a==$b)?(
($a>$c)?(
print "$a=$b>$c"
):(
print "$c>$a=$b"
)
):(
($a==$c)?(
($a>$b)?(
print "$a=$c>$b"
):(
print "$b>$a=$c"
)
):(
($b==$c)?(
($a>$b)?(
print "$a>$b=$c"
):(
print "$b=$c>$a"
)
):(
($a>$b)? (($a>$c)? (($c>$b)? (print "$a>$c>$b"):(print "$a>$b>$c")):(print "$c>$a>$b")):(($b>$c)? (($a>$c)? (print "$b>$a>$c"):(print "$b>$c>$a")):(print "$c>$b>$a"))
)
)
)
);

五角星

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
for($i1=1;$i1<7;$i1++)             /*这是五角星的上面一个角*/
{
for($j1=1;$j1<19-$i1;$j1++) /*这是空格*/
printf(" ");
for($k1=1;$k1<=2*$i1-1;$k1++) /*这是“*”*/
printf("*");
printf("\n");
}
for($i2=1;$i2<5;$i2++) /*这是五角星的中间两个角*/
{
for($j2=1;$j2<3*$i2-3;$j2++)
printf(" ");
for($k2=1;$k2<=42-6*$i2;$k2++)
printf("*");
printf("\n");
}
for($i3=1;$i3<3;$i3++) //这是中间与下部相接的部分
{
for($j3=1;$j3<12-$i3;$j3++)
printf(" ");
for($k3=1;$k3<=12+2*$i3;$k3++)
printf("*");
printf("\n");
}
for($i4=1;$i4<5;$i4++) // 这是五角星的下面两个角
{
for($j4=1;$j4<10-$i4;$j4++)
printf(" ");
for($k4=1;$k4<=10-2*$i4;$k4++)
printf("*");
for($m4=1;$m4<6*$i4-3;$m4++)
printf(" ");
for($n4=1;$n4<=10-2*$i4;$n4++)
printf("*");
printf("\n");
}

for循环实现关联数组的遍历

1
2
3
4
5
6
7
$arr=['q'=>'12','w'=>'13','e'=>'14','r'=>"15"];
$num=count($arr);
for($i=0;$i<$num;$i++)
{
echo key($arr)."=>".current($arr)."\n";
next($arr);
}

通过文件读写对HTML表格的操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" >
<title>news</title>

</head>

<body background="img/2.jpg">

<table border="1" width="100%" cellspacing="10" cellpadding="10" align="center " bgcolor="#6495ed">
<tr>
<th id="pm">排名</th>
<th id="mz">名字</th>
<th id="jj" bgcolor="#a52a2a">简介</th>
<th id="ss">搜索指数</th>
</tr>
<?php
// 打开数据文件
$file=fopen("/Project/PHP/news/data/new.db","r");

if($file==null)
{
echo "文件打开失败";
die();
}
$str=fgets($file); // 去掉表格标题
$i=0;
while(!feof($file))
{
$str=fgets($file);
$data=explode('||',$str);// 获取字符串按 || 分割成数组
if($i<3)
{
// 前三行调用图片和简介

table_qiansan($data);
}
else{

table_fun($data);// 调用自定义函数
}
$i++;
}

fclose($file);
?>

</table>

</body>
</html>


<?php
// 三行前的信息读取
// 分别显示 id 图片 简介
function table_qiansan($data)
{
$data[2]=substr( $data[2],0,200);
echo <<<EOF
<tr>
<td>$data[0]</td>
<td><img src="img/1.jpg"></td>
<td colspan="2">$data[2]</td>
</tr>
EOF;
}

// 三行后的信息
function table_fun($data)
{
echo <<<EOF
<tr>
<td>$data[0]</td>
<td>$data[1]</td>
<td>$data[2]</td>
<td>$data[3]</td>
</tr>;
EOF;
}
?>

效果图