经典循环例子
for($counter = 1; $counter <= 6; $counter++) //循环6次
{
print("counter is $counter
\n"); //打印6次
}
?>
for的高级运用
/*
** 打印必要的说明文字
*/
print("距离星期一还有几天?\n");
print("
print("
函数的简单调用:
function printBold($inputText) //定义function printBold()
{
print("" . $inputText . ""); ////打印$inputText
}
print("这行没有加重!
\n"); //直接打印字符串
printBold("这行加重了!!!"); //调用function printBold()函数
print("
\n");
print("这行没有加重!
\n"); //直接打印字符串
?>
有返回值的函数
function makeBold($inputText) //定义function makeBold()函数
{
$boldedText = "";
$boldedText .= $inputText;
$boldedText .= "";
return($boldedText); //返回变量$boldedText
}
print("这行没有加重!!!
\n"); //直接打印字符串
print(makeBold("这行被加重了!!!") . "
\n");//调用function makeBold()函数
print("这行没有加重!!!
\n"); //直接打印字符串
?>
有默认参数的函数
function printColored($Text, $Color="black") //定义function函数
{
print("$Text"); //获取字符串的内容和颜色
}
printColored("这是黑颜色的字!"); //调用function函数
print("
\n");
printColored("这是蓝颜色的字!", "blue"); //调用function函数
print("
\n");
?>
用的规算法判断是否是整数
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("0是整数吗?" .
checkInteger(0) . "
\n");
print("7是整数吗? " .
checkInteger(7) . "
\n");
print("3.5呢?" . checkInteger(3.5) . "
\n");
print("那么-5呢?" . checkInteger(-5) . "
\n");
print("还有-9.2?" . checkInteger(-9.2) . "
\n");
?>
初始化数组
$mOnthName= array(1=>"January", "February", "March",//初始化一个数组
"April", "May", "June", "July", "August",
"September", "October", "November", "December");
print(" 英语的“5月”是 $monthName[5] 。
\n");//打印数组中的第6个元素
?>
获取数组中的元素
$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 5 is " . $monthName[5]. "
\n");
print("Month Aug is " . $monthName["Aug"] . "
\n");
print("Month June is " . $monthName["June"] . "
\n");
?>
创建一个多维数组
$Cities = array( //二维数组array()
"华北地区"=>array(
"北京市",
"天津市",
"石家庄"
),
"西北地区"=>array(
"西安",
"拉萨"
)
);
print("华北地区: ".$Cities["华北地区"][0]); //打印$Cities["华北地区"][0]
?>
PHP 4.0实现表格状打印
/*
** 数据表格化
*/
print("
");//开始列 print($Row * $Column);//表格元素乘积 print(" | ");
?>
查看系统的一些变量
print("你正在用文件的名字为: ");
print(__FILE__);
print("
\n");
print("
打开本地或者远程文件
print("
// 打开文件同时打印文件的每一个字符
if($myFile = fopen("data.txt", "r"))
{
while(!feof($myFile))
{
$myCharacter = fgetc($myFile);
print($myCharacter);
}
fclose($myFile);
}
?>
print("
print("文件的所有者(UID 值):");
print(fileowner("data.txt")."
");
print("文件的大小:");
print(filesize("data.txt")."
");
print("文件的类型:");
print(filetype("data.txt")."
");
?>
调用文本文件内容
文件名 | \n");文件的大小 | \n");
---|---|
$entryName | ");"); print(filesize($entryName)); print(" | ");