华企号 后端开发 php经典代码

php经典代码

经典循环例子

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

for的高级运用

/*
** 打印必要的说明文字
*/
print(“距离星期一还有几天?\n”);
print(”

    1. \n”);
    1.     for($currentDate = date(“U”);             //定义$currentDate时间格式
    1.         date(“l”, $currentDate) != “Monday”;     //判断是不是当前系统时间是Monday
    1.         $currentDate += (60 * 60 * 24))        //当前时间加上1天
    1.     {
    1.         /*
    1.         ** 打印时间名称
    1.         */
            print(”

  1. ” . date(“l”, $currentDate) . “\n”);
    }

    print(“

\n”);
?>

函数的简单调用:

<font size=5>
function printBold($inputText)            //定义function printBold()
{
print(“” . $inputText . ““);    ////打印$inputText
}
print(“这行没有加重!
\n”);            //直接打印字符串
printBold(“这行加重了!!!”);            //调用function printBold()函数
print(”
\n”);
print(“这行没有加重!
\n”);            //直接打印字符串
?>

</font size=5>

有返回值的函数

<font size=5>
function makeBold($inputText)        //定义function makeBold()函数
{
$boldedText = ““;
$boldedText .= $inputText;
$boldedText .= “
“;
return($boldedText);        //返回变量$boldedText
}
print(“这行没有加重!!!
\n”);    //直接打印字符串
print(makeBold(“这行被加重了!!!”) . ”
\n”);//调用function makeBold()函数
print(“这行没有加重!!!
\n”);    //直接打印字符串
?>

</font size=5>

有默认参数的函数

<font size=5>
function printColored($Text, $Color=”black”)        //定义function函数
{
print(“<font color=\”$color\”>$Text”);    //获取字符串的内容和颜色
}
printColored(“这是黑颜色的字!”);            //调用function函数
print(”

\n”);
printColored(“这是蓝颜色的字!”, “blue”);            //调用function函数
print(”
\n”);
?>

</font color=\”$color\”></font size=5>

用的规算法判断是否是整数

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”);
?>

初始化数组

<font size=5>
$monthName = array(1=>”January”, “February”, “March”,//初始化一个数组
“April”, “May”, “June”, “July”, “August”,
“September”, “October”, “November”, “December”);
print(” 英语的“5月”是 $monthName[5] 
\n”);//打印数组中的第6个元素
?>

</font size=5>

获取数组中的元素

$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]
?>

PHP4.0实现表格状打印

/*
** 数据表格化
*/

print(“<table bgcolor=’ffccoo’ border=\”1\”>\n”); // 表格开始

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

// do each column
for($Column=1; $Column <= 12; $Column ++)
{
print(“”);//开始列
print($Row * $Column);//表格元素乘积
print(“”);
}

print(“\n”); // 行结束

}

print(“\n”); // 表格结束

?>

</table bgcolor=’ffccoo’ border=\”1\”>

查看系统的一些变量

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(“<table border=\”1\”>\n”);
// 创建表格的头
print(“<font color=’red’>\n”);
print(“文件名\n”);
print(“文件的大小\n”);
print(“\n”);
$myDirectory = opendir(“.”);        // 建立操作目录的句柄
// 读出目录中的每一个子项
while($entryName = readdir($myDirectory))
{
print(“”);
print(“$entryName”);
print(“<td align=\”right\”>”);
print(filesize($entryName));
print(“”);
print(“\n”);
}
closedir($myDirectory);            // 关闭目录
print(“\n”);
?>

</td align=\”right\”></font color=’red’></table border=\”1\”>

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);                            //恢复默认超时设置
}
?>
<form enctype = “multipart form-data” name =” “SubmitForm” <BR”>ACTION = “default.php” METHOD = “POST”>
<input type = “hidden” name = “max_file_size” value =”1000000″>
<input type = “hidden” name = “uploadaction” value = “1”>
</input type = “hidden” name = “uploadaction” value = “1”></input type = “hidden” name = “max_file_size” value =”1000000″></form enctype = “multipart>

作者: 华企网通王鹏程序员

我是程序员王鹏,热爱互联网软件开发和设计,专注于大数据、数据分析、数据库、php、java、python、scala、k8s、docker等知识总结。 我的座右铭:"业精于勤荒于嬉,行成于思毁于随"
上一篇
下一篇

发表回复

联系我们

联系我们

028-84868647

在线咨询: QQ交谈

邮箱: tech@68v8.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部