学php必收藏的几个经典代码第1/2页
程序员文章站
2022-05-31 16:50:09
经典循环例子 经典循环例子
经典循环例子
<html>
<head>
<title>经典循环例子</title>
</head>
<body>
<?
for($counter = 1; $counter <= 6; $counter++) //循环6次
{
print("<b>counter is $counter</b><br>\n"); //打印6次
}
?>
</body>
</html>
for的高级运用
<html>
<head>
<title>for的高级运用</title>
</head>
<body>
<?
/*
** 打印必要的说明文字
*/
print("<b>距离星期一还有几天?</b>\n");
print("<ol>\n");
for($currentdate = date("u"); //定义$currentdate时间格式
date("l", $currentdate) != "monday"; //判断是不是当前系统时间是monday
$currentdate += (60 * 60 * 24)) //当前时间加上1天
{
/*
** 打印时间名称
*/
print("<li>" . date("l", $currentdate) . "\n");
}
print("</ol>\n");
?>
</body>
</html>
函数的简单调用:
<html>
<head>
<title>简单的函数</title>
</head>
<body>
<font size=5>
<?
function printbold($inputtext) //定义function printbold()
{
print("<b>" . $inputtext . "</b>"); ////打印$inputtext
}
print("这行没有加重!<br>\n"); //直接打印字符串
printbold("这行加重了!!!"); //调用function printbold()函数
print("<br>\n");
print("这行没有加重!<br>\n"); //直接打印字符串
?>
</font>
</body>
</html>
有返回值的函数
<html>
<head>
<title>有返回值的函数</title>
</head>
<body>
<font size=5>
<?
function makebold($inputtext) //定义function makebold()函数
{
$boldedtext = "<b>";
$boldedtext .= $inputtext;
$boldedtext .= "</b>";
return($boldedtext); //返回变量$boldedtext
}
print("这行没有加重!!!<br>\n"); //直接打印字符串
print(makebold("这行被加重了!!!") . "<br>\n");//调用function makebold()函数
print("这行没有加重!!!<br>\n"); //直接打印字符串
?>
</size>
</body>
</html>
有默认参数的函数
<html>
<head>
<title>有默认参数的函数</title>
</head>
<body>
<font size=5>
<?
function printcolored($text, $color="black") //定义function函数
{
print("<font color=\"$color\">$text</font>"); //获取字符串的内容和颜色
}
printcolored("这是黑颜色的字!"); //调用function函数
print("<br><br>\n");
printcolored("这是蓝颜色的字!", "blue"); //调用function函数
print("<br>\n");
?>
</size>
</body>
</html>
用的规算法判断是否是整数
<html>
<head>
<title>判断整数</title>
</head>
<body>
<?
function checkinteger($number)
{
if($number > 1)
{
/* 整数减1仍然是整数 */
return(checkinteger($number-1));
}
elseif($number < 0)
{
/* 对于一个负数,*/
/* 可以分析它的绝对值*/
return(checkinteger((-1)*$number-1));//取绝对值,把负数按整数分析
}
else
{
if(($number > 0) and ($number < 1))
{
return("当然不是");
}
else
{
/* 0 和 1 是整数 */
/* 根据相关数学定义 */
return("是的");
}
}
}
print("<b>0是整数吗?</b>" .
checkinteger(0) . "<br>\n");
print("<b>7是整数吗?</b> " .
checkinteger(7) . "<br>\n");
print("<b>3.5呢?</b>" . checkinteger(3.5) . "<br>\n");
print("<b>那么-5呢?</b>" . checkinteger(-5) . "<br>\n");
print("<b>还有-9.2?</b>" . checkinteger(-9.2) . "<br>\n");
?>
</body>
</html>
初始化数组
<html>
<head>
<title>初始化数组</title>
</head>
<font size=5>
<?
$monthname = array(1=>"january", "february", "march",//初始化一个数组
"april", "may", "june", "july", "august",
"september", "october", "november", "december");
print(" 英语的“5月”是<b> $monthname[5] </b>。<br>\n");//打印数组中的第6个元素
?>
</font>
</body>
</html>
获取数组中的元素
<html>
<head>
<title>获取数组中的元素</title>
</head>
<?
$monthname = array(
/*定义$monthname[1]到$monthname[12]*/
1=>"january", "february", "march",
"april", "may", "june",
"july", "august", "september",
"october", "november", "december",
/*定义$monthname["jan"]到$monthname["dec"]*/
"jan"=>"january", "feb"=>"february",
"mar"=>"march", "apr"=>"april",
"may"=>"may", "jun"=>"june",
"jul"=>"july", "aug"=>"august",
"sep"=>"september", "oct"=>"october",
"nov"=>"november", "dec"=>"december",
/*定义$monthname["jan"]到$monthname["dec"]*/
"january"=>"january", "february"=>"february",
"march"=>"march", "april"=>"april",
"may"=>"may", "june"=>"june",
"july"=>"july", "august"=>"august",
"september"=>"september", "october"=>"october",
"november"=>"november", "december"=>"december"
);
/*打印相关的元素*/
print("month <b>5</b> is <b>" . $monthname[5]. "</b><br>\n");
print("month <b>aug</b> is <b>" . $monthname["aug"] . "</b><br>\n");
print("month <b>june</b> is <b>" . $monthname["june"] . "</b><br>\n");
?>
</body>
</html>
创建一个多维数组
<html>
<head>
<title>创建一个多维数组</title>
</head>
<?
$cities = array( //二维数组array()
"华北地区"=>array(
"北京市",
"天津市",
"石家庄"
),
"西北地区"=>array(
"西安",
"拉萨"
)
);
print("华北地区: ".$cities["华北地区"][0]); //打印$cities["华北地区"][0]
?>
</body>
</html>
<html>
<head>
<title>经典循环例子</title>
</head>
<body>
<?
for($counter = 1; $counter <= 6; $counter++) //循环6次
{
print("<b>counter is $counter</b><br>\n"); //打印6次
}
?>
</body>
</html>
for的高级运用
<html>
<head>
<title>for的高级运用</title>
</head>
<body>
<?
/*
** 打印必要的说明文字
*/
print("<b>距离星期一还有几天?</b>\n");
print("<ol>\n");
for($currentdate = date("u"); //定义$currentdate时间格式
date("l", $currentdate) != "monday"; //判断是不是当前系统时间是monday
$currentdate += (60 * 60 * 24)) //当前时间加上1天
{
/*
** 打印时间名称
*/
print("<li>" . date("l", $currentdate) . "\n");
}
print("</ol>\n");
?>
</body>
</html>
函数的简单调用:
<html>
<head>
<title>简单的函数</title>
</head>
<body>
<font size=5>
<?
function printbold($inputtext) //定义function printbold()
{
print("<b>" . $inputtext . "</b>"); ////打印$inputtext
}
print("这行没有加重!<br>\n"); //直接打印字符串
printbold("这行加重了!!!"); //调用function printbold()函数
print("<br>\n");
print("这行没有加重!<br>\n"); //直接打印字符串
?>
</font>
</body>
</html>
有返回值的函数
<html>
<head>
<title>有返回值的函数</title>
</head>
<body>
<font size=5>
<?
function makebold($inputtext) //定义function makebold()函数
{
$boldedtext = "<b>";
$boldedtext .= $inputtext;
$boldedtext .= "</b>";
return($boldedtext); //返回变量$boldedtext
}
print("这行没有加重!!!<br>\n"); //直接打印字符串
print(makebold("这行被加重了!!!") . "<br>\n");//调用function makebold()函数
print("这行没有加重!!!<br>\n"); //直接打印字符串
?>
</size>
</body>
</html>
有默认参数的函数
<html>
<head>
<title>有默认参数的函数</title>
</head>
<body>
<font size=5>
<?
function printcolored($text, $color="black") //定义function函数
{
print("<font color=\"$color\">$text</font>"); //获取字符串的内容和颜色
}
printcolored("这是黑颜色的字!"); //调用function函数
print("<br><br>\n");
printcolored("这是蓝颜色的字!", "blue"); //调用function函数
print("<br>\n");
?>
</size>
</body>
</html>
用的规算法判断是否是整数
<html>
<head>
<title>判断整数</title>
</head>
<body>
<?
function checkinteger($number)
{
if($number > 1)
{
/* 整数减1仍然是整数 */
return(checkinteger($number-1));
}
elseif($number < 0)
{
/* 对于一个负数,*/
/* 可以分析它的绝对值*/
return(checkinteger((-1)*$number-1));//取绝对值,把负数按整数分析
}
else
{
if(($number > 0) and ($number < 1))
{
return("当然不是");
}
else
{
/* 0 和 1 是整数 */
/* 根据相关数学定义 */
return("是的");
}
}
}
print("<b>0是整数吗?</b>" .
checkinteger(0) . "<br>\n");
print("<b>7是整数吗?</b> " .
checkinteger(7) . "<br>\n");
print("<b>3.5呢?</b>" . checkinteger(3.5) . "<br>\n");
print("<b>那么-5呢?</b>" . checkinteger(-5) . "<br>\n");
print("<b>还有-9.2?</b>" . checkinteger(-9.2) . "<br>\n");
?>
</body>
</html>
初始化数组
<html>
<head>
<title>初始化数组</title>
</head>
<font size=5>
<?
$monthname = array(1=>"january", "february", "march",//初始化一个数组
"april", "may", "june", "july", "august",
"september", "october", "november", "december");
print(" 英语的“5月”是<b> $monthname[5] </b>。<br>\n");//打印数组中的第6个元素
?>
</font>
</body>
</html>
获取数组中的元素
<html>
<head>
<title>获取数组中的元素</title>
</head>
<?
$monthname = array(
/*定义$monthname[1]到$monthname[12]*/
1=>"january", "february", "march",
"april", "may", "june",
"july", "august", "september",
"october", "november", "december",
/*定义$monthname["jan"]到$monthname["dec"]*/
"jan"=>"january", "feb"=>"february",
"mar"=>"march", "apr"=>"april",
"may"=>"may", "jun"=>"june",
"jul"=>"july", "aug"=>"august",
"sep"=>"september", "oct"=>"october",
"nov"=>"november", "dec"=>"december",
/*定义$monthname["jan"]到$monthname["dec"]*/
"january"=>"january", "february"=>"february",
"march"=>"march", "april"=>"april",
"may"=>"may", "june"=>"june",
"july"=>"july", "august"=>"august",
"september"=>"september", "october"=>"october",
"november"=>"november", "december"=>"december"
);
/*打印相关的元素*/
print("month <b>5</b> is <b>" . $monthname[5]. "</b><br>\n");
print("month <b>aug</b> is <b>" . $monthname["aug"] . "</b><br>\n");
print("month <b>june</b> is <b>" . $monthname["june"] . "</b><br>\n");
?>
</body>
</html>
创建一个多维数组
<html>
<head>
<title>创建一个多维数组</title>
</head>
<?
$cities = array( //二维数组array()
"华北地区"=>array(
"北京市",
"天津市",
"石家庄"
),
"西北地区"=>array(
"西安",
"拉萨"
)
);
print("华北地区: ".$cities["华北地区"][0]); //打印$cities["华北地区"][0]
?>
</body>
</html>
1
上一篇: Ubuntu 优化、美化(主题、终端)
下一篇: PHP程序员编程注意事项