Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)aaa
published in(发表于) 2013/12/21 22:36:14
初学者学习PHP开发应该掌握的几段精华代码_php资料_编程技术

初学者学习PHP开发应该掌握的几段精华代码_php资料_编程技术-你的首页-uuhomepage.com
经典循环例子



经典循环例子


for($counter = 1; $counter <= 6; $counter++) //循环6次
{
print("counter is $counter
\n"); //打印6次
}
?>



for的高级运用


for的高级运用


/*
** 打印必要的说明文字
*/
print("距离星期一还有几天?\n");
print("
    \n");
    for($currentDate = date("U"); //定义$currentDate时间格式
    date("l", $currentDate) != "Monday"; //判断是不是当前系统时间是Monday
    $currentDate += (60 * 60 * 24)) //当前时间加上1天
    {
    /*
    ** 打印时间名称
    */
    print("
  1. " . date("l", $currentDate) . "\n");
    }

    print("
\n");
?>


函数的简单调用:



简单的函数



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("\n"); // 表格开始


for($Row=1; $Row <= 12; $Row ++)
{
print("\n"); // 开始行

// do each column
for($Column=1; $Column <= 12; $Column ++)
{
print("");
}

print("\n"); // 行结束

}

print("
");//开始列
print($Row * $Column);//表格元素乘积
print("
\n"); // 表格结束

?>


查看系统的一些变量



查看PHP的环境变量


print("你正在用文件的名字为: ");
print(__FILE__);
print("
\n");
print("
");
print("你的操作系统为: ");
print(PHP_OS);
print("
");
print("你的php的版本为: ");
print(PHP_VERSION)
?>


打开本地或者远程文件



打开本地或者远程文件


print("

通过http协议打开文件

\n");
// 通过 http 协议打开文件
if(!($myFile = fopen("d:web/web/php/test/data.txt", "r")))
{
print("文件不能打开");
exit;
}
while(!feof($myFile)) //循环
{
// 按行读取文件中的内容
$myLine = fgetss($myFile, 255);
print("$myLine
\n");
}
// 关闭文件的句柄
fclose($myFile);
?>

打开文件的几种方式比较


读取文件内容


// 打开文件同时打印文件的每一个字符
if($myFile = fopen("data.txt", "r"))
{
while(!feof($myFile))
{
$myCharacter = fgetc($myFile);
print($myCharacter);
}
fclose($myFile);
}
?>
");?>
// 打开文件同时打印文件的每一行
if($myFile = fopen("data.txt", "r"))
{
while(!feof($myFile))
{
$myLine = fgets($myFile, 255);
print($myLine);
}
fclose($myFile);
}
?>
");?>
/* 打开文件同时打印文件的每一行,
同时去掉取回字符串中的 HTML 语言
*/
if($myFile = fopen("data.txt", "r"))
{
while(!feof($myFile))
{
$myLine = fgetss($myFile, 255);
print($myLine);
}
fclose($myFile);
}
?>

访问文件常见属性



访问文件常见属性




print("文件的所有者(UID 值):");
print(fileowner("data.txt")."
");
print("文件的大小:");
print(filesize("data.txt")."
");
print("文件的类型:");
print(filetype("data.txt")."
");
?>

调用文本文件内容


调用文本文件内容



// 打开文件同时,打印每一行
$myFile = file( "data.txt");
for($index = 0; $index < count($myFile); $index++)
{
print($myFile[$index]."
");
}
?>


创建目录函数


创建目录函数


if(mkdir("myDir1", 0777)) //创建目录的函数
{
print("目录创建成功"); //目录建立成功
}
else
{
print("目录建立失败!"); //目录建立失败
}
?>

浏览目录


浏览目录


// 使用表格浏览目录的结构
print("\n");
// 创建表格的头
print("\n");
print("
\n");
print("
\n");
print("
\n");
$myDirectory = opendir("."); // 建立操作目录的句柄
// 读出目录中的每一个子项
while($entryName = readdir($myDirectory))
{
print("");
print("");
print("");
print("\n");
}
closedir($myDirectory); // 关闭目录
print("
文件名文件的大小
$entryName");
print(filesize($entryName));
print("
\n");
?>

PHP相关信息


PHP相关信息


phpinfo();
?>

常用的数值判断函数


常用的数值判断函数


//判断数组
$colors = array("red", "blue", "green");
if(is_array($colors))
{
print("colors is an array"."
");
}
//双精度数判断
$Temperature = 15.23;
if(is_double($Temperature))
{
print("Temperature is a double"."
");
}
//整数判断
$PageCount = 2234;
if(is_integer($PageCount))
{
print("$PageCount is an integer"."
");
}
//对象判断
class widget
{
var $name;
var $length;
}
$thing = new widget;
if(is_object($thing))
{
print("thing is an object"."
");
}
//字符判断
$Greeting = "Hello";
if(is_string($Greeting))
{
print("Greeting is a string"."
");
}
?>

文件上传界面


文件上传界面


if($UploadAction){
$UploadAction=0;
$TimeLimit=60;
/*设置超时限制时间默认时间为 30s,设置为0时为不限时 */
set_time_limit($TimeLimit);
If(($Upfile != "none")&&
($Upfile != ""))
{
$Filepath="d:\web\web\php\test"; //上载文件存放路径
$FileName=$Filepath.$Upfile_name;
if($Upfile_size <1024) //上载文件大小
{$FileSize = (string)$Upfile_size . "字节";}
elseif($Upfile_size <(1024 * 1024))
{
$FileSize = number_format((double)($Upfile_size / 1024), 1) . " KB";
}
else
{
$FileSize = number_format((double)($Upfile_size/(1024*1024)),1)."MB";
}
if(!file_exists($FileName))
{
if(copy($Upfile,$FileName))
{unlink($Upfile);
echo "
\n";
echo "文件 $Upfile_name 已上载成功!";
echo "
\n";
echo "文件位置:$FileName";
echo "
\n";
echo "文件大小:$FileSize";
echo "
\n";
}
else
{echo "文件 $Upfile_name上载失败!"; }
}
else
{echo "文件 $Upfile_name已经存在!"; }
}
else
{echo "你没有选择任何文件上载!"; }
set_time_limit(30); //恢复默认超时设置
}
?>
ACTION = "default.php" METHOD = "POST">












If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759