代码如下 | 复制代码 |
<html> <head> <title>第一个php教程程序</title> </head> <body> <?php echo "hello, 这是第一个php程序"; ?> |
</body>
</html>
实例二 html代码讲解范例
代码如下 | 复制代码 |
<!doctype html public "-//w3c//dtd html 4.0 transitional//en"> <html> <head> <title> html中嵌入php的例子</title> </head> <body> <p>我最喜欢的颜色: <?php echo "红色"; ?> </p> </body> </html> |
源代码主要用函数实现,包括计数,字符串连接,函数返 回结果等通过函数来实现的例子
函数
<?php教程
function mycount($invalue1,$invalue2)
{
$addvalue = $invalue1+$invalue2;
return $addvalue; //返回计算结果
}
$count = mycount(59,100);
echo $count; //输出159
?>
函数调用
<?php
function str_unite (&$string)
{
$string .= '也喜欢蓝色.';
}
$str = '喜欢红色,';
str_unite ($str);
echo $str; // 输出结果: '喜欢红色,也喜欢蓝色.'
?>
函数返回值
<?php
function mycolor ($incolor = "蓝色")
{
return "我喜欢的颜色: $incolor.n";
}
echo mycolor();
echo mycolor("粉色");
?>
全局变量
<?php
$a = 1;
$b = 2;
function sum()
{
global $a, $b;
$b = $a + $b;
}
sum();
echo $b;
?>
一款简单数字计算
<?php
function square ($num)
{
return $num * $num;
}
echo square (4); // 输出'16'.
?>
这款函数比较实用于数据采集,一般采集数据时会要指定一个区域的内容,这个代码就可以实例;
*/
代码如下 | 复制代码 |
$str="<!-- fdaf-- -->"; function strip_comments($data) { while ($the_rest) { $result .= substr($the_rest, 0, $start); $end = strpos($the_rest, "-->", $start); $the_rest = substr($the_rest, $end+3); return $result; |
post表单传递参数
代码如下 | 复制代码 |
<html> <head> </head> <body> <h3>search</h3> <form action="c.php" method="post"> depart name:<input type="text" size=25 name="depart" value=""><br><br> <input type="submit" name="submit" value="search"> </form> </body> </html> |
c.php页面
代码如下 | 复制代码 |
<?php $depart=$_post["depart"]; $q = "select * from info where depart='$depart'"; ?> |
实例二get 方法传递参数
代码如下 | 复制代码 |
<a href=www.111cn.net/a.php?value=www.111cn.net>传get参数传</a> |
a.php页面代码
代码如下 | 复制代码 |
echo $_get['value']; |
代码如下 | 复制代码 |
*/ if(preg_match("/image|^(jpg|gif|png|bmp)/", $type)) { return $size.$attachicons[$typeid]; |