php实验代码(php实验总结报告)
华为云服务器特价优惠火热进行中! 2核2G2兆仅需 38 元;4核4G3兆仅需 79 元。购买时间越长越优惠!更多配置及优惠价格请咨询客服。
合作流程: |
今天给各位分享php实验代码的知识,其中也会对php实验总结报告进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
微信号:cloud7591如需了解更多,欢迎添加客服微信咨询。
复制微信号
本文目录一览:
- 1、PHP代码编写
- 2、简单PHP代码
- 3、php目录操作实例代码
- 4、求php的实例代码
- 5、php最经典,最基础的代码,适合入门的
- 6、求个简单的php代码
PHP代码编写
这里我自己创建了一个数组,你的数组可以是从数据库里读出来的~~记住要取关联数组啊 如果不取关联数组,就只能自己改echo 语句啦~~
$points = array(
'php' = 90,
'java'= 80,
'div' = 51
);
$str="";
$totalPoints=0;
foreach ($points as $key=$val){
if ($val=90){
$str.="您的".$key."成绩是".$val.",优秀。";
}elseif ($val90 $val=80){
$str.="您的".$key."成绩是".$val.",良好。";
}elseif ($val80 $val=70){
$str.="您的".$key."成绩是".$val.",及格。";
}else{
$str.="您的".$key."成绩是".$val.",不及格。";
}
$totalPoints+=$val;
}
$average = $totalPoints/count($points);
if(intval(substr($average,strpos(".",$average),1))=5){
$average = ceil($average);
}else{
$average = floor($average);
}
echo $str."您的总分是:".$totalPoints.",您的平均分是:".$average;

简单PHP代码
$_env 是环境变量,通过环境方式传递给当前脚本的变量的数组。
$_ENV['defaultapp'] = array('portal.php' = 'portal', 'forum.php' = 'forum', 'group.php' = 'group', 'home.php' = 'home');
是赋值 , 你可以用 var_dump($_env['defaultapp']) 看赋值结果。
--------------------------------------------------------------------
$_ENV['hostarr'] = explode('.', $_SERVER['HTTP_HOST']);
环境变量 用.分隔 主域名(你可以 echo $_server['HTTP_HOST']里面有什么)
----------------------------------------------------------------
$url = $domainroot.'forum.php?mod=groupfid='.$domain['id'].'page=1';
构造一个URL 直白点 结果就是: ;fid=1page=1
----------------------------------------
$url = empty($_ENV['domain']['app']['default']) ? (!empty($_ENV['domain']['defaultindex']) ? $_ENV['domain']['defaultindex'] : 'forum.php') : 'http://'.$_ENV['domain']['app']['default'];
结构简化 $url = $a ? (!$b? $c : $d) :$e; 2个3元运算嵌套, 至于看起来复杂的变量都是多维数组的值
php目录操作实例代码
这篇文章主要介绍了php目录操作实例代码,需要的朋友可以参考下
代码如下:
?php
/**
*
listdir
*/
header("content-type:text/html;charset=utf-8");
$dirname
=
"./final/factapplication";
function
listdir($dirname)
{
$ds
=
opendir($dirname);
while
(false
!==
($file
=
readdir($ds)))
{
$path
=
$dirname.'/'.$file;
if
($file
!=
'.'
$file
!=
'..')
{
if
(is_dir($path))
{
listdir($path);
}
else
{
echo
$file."br";
}
}
}
closedir($ds);
}
listdir($dirname);
核心:递归的经典应用,以及文件和目录的基本操作。
代码如下:
?php
/**
*
copydir
*/
$srcdir
=
"../fileupload";
$dstdir
=
"b";
function
copydir($srcdir,
$dstdir)
{
mkdir($dstdir);
$ds
=
opendir($srcdir);
while
(false
!==
($file
=
readdir($ds)))
{
$path
=
$srcdir."/".$file;
$dstpath
=
$dstdir."/".$file;
if
($file
!=
"."
$file
!=
"..")
{
if
(is_dir($path))
{
copydir($path,
$dstpath);
}
else
{
copy($path,
$dstpath);
}
}
}
closedir($ds);
}
copydir($srcdir,
$dstdir);
核心:copy函数。
代码如下:
?php
/**
*
deldir
*/
$dirname
=
'a';
function
deldir($dirname)
{
$ds
=
opendir($dirname);
while
(false
!==
($file
=
readdir($ds)))
{
$path
=
$dirname.'/'.$file;
if($file
!=
'.'
$file
!=
'..')
{
if
(is_dir($path))
{
deldir($path);
}
else
{
unlink($path);
}
}
}
closedir($ds);
return
rmdir($dirname);
}
deldir($dirname);
核心:注意unlink删除的是带path的file。
代码如下:
?php
/**
*
dirsize
*/
$dirname
=
"a";
function
dirsize($dirname)
{
static
$tot;
$ds
=
opendir($dirname);
while
(false
!==
($file
=
readdir($ds)))
{
$path
=
$dirname.'/'.$file;
if
($file
!=
'.'
$file
!=
'..')
{
if(is_dir($path))
{
dirsize($path);
}
else
{
$tot
=
$tot
+
filesize($path);
}
}
}
return
$tot;
closedir($ds);
}
echo
dirsize($dirname);
核心:通过判断$tot在哪里返回,理解递归函数。
求php的实例代码
一般用户登是不用cookie的而是用session
以下是我的一人登程序
表单:
FORM name=form1 action="?=$PHP_SELF?" method=post
TR height=5
TD width=5/TD
TD width=56/TD
TD/TD
/TR
TR height=36
TD/TD
TD用户名/TD
TDINPUT name=userName id="userName"
style="BORDER-RIGHT: #000000 1px solid; BORDER-TOP: #000000 1px solid; BORDER-LEFT: #000000 1px solid; BORDER-BOTTOM: #000000 1px solid" size=24
maxLength=30/TD
/TR
TR height=36
TD /TD
TD口 令/TD
TDINPUT
name=password
type=password id="password"
style="BORDER-RIGHT: #000000 1px solid; BORDER-TOP: #000000 1px solid; BORDER-LEFT: #000000 1px solid; BORDER-BOTTOM: #000000 1px solid" size=24 maxLength=30/TD
/TR
TR height=5
TD colSpan=3/TD
/TR
TR
TD /TD
TD /TD
TDINPUT type=image height=18 width=70
src="images/bt_login.gif"/TD
/TR
input type="hidden" value="action" name="action" /
/FORM
处理表单程序
?
header("Content-Type: text/html;charset=utf-8");
session_start();
require "../common/dbconn-utf-8.php";
$Conn=@DBConn();
?
?
$iTABLE = "userTab";
?
?
function getIP(){global $ip;if (getenv("HTTP_CLIENT_IP"))$ip = getenv("HTTP_CLIENT_IP");else if(getenv("HTTP_X_FORWARDED_FOR"))$ip = getenv("HTTP_X_FORWARDED_FOR");else if(getenv("REMOTE_ADDR"))$ip = getenv("REMOTE_ADDR");else $ip = "Unknow";return $ip;}
if($_POST["action"]=="action"){
$userName = $_POST['userName'];
$password = $_POST['password'];
if($userName $password)
{
$rs = mysql_query("select id,title from `".$iTABLE."` where title = '$userName' and password = '$password'");
$num = mysql_num_rows($rs);
if($num == 1){
$AdminID = mysql_result($rs,0,"id");
$AdminTitle = mysql_result($rs,0,"title");
$sqlUpdate = "update userTab set LoginTimes = LoginTimes + 1,ip='".getIP()."' where id=".$AdminID;
//echo $sqlUpdate;
mysql_query($sqlUpdate);
// $update_sql = "Update ajax_book Set relcontent = '$relcontent',reltime = '$reltime' Where id = '$id'";
$_SESSION["ADMINID"] = $AdminID;
$_SESSION["ADMINTITLE"] = $AdminTitle;
//echo $_SESSION["ADMINTITLE"];
@mysql_close($Conn);
$newWin = "./"; //跳转至登陆
header("Location:".$newWin);
exit;
}
}
$Msg = "scriptalert('用户名或密码错误!');location.href='login.php'/script";
echo $Msg;
@mysql_close($Conn);
}
?
dbconn-utf-8.php文件
?
/*
* 数据库连接池
* copyright 2004/01/03
* write by jatic
* filename dbconn.php
*/
function DBConn()
{
//$dbServer="localhost";
//$dbUser="root";
//$dbPasswd="lwf";
//$dbName="database_zhongchang";
$dbServer="111.111.111.111";
$dbUser="222";
$dbPasswd="222";
$dbName="222";
$Conn=@mysql_connect($dbServer,$dbUser,$dbPasswd) or die(mysql_error());
if(!$Conn){
echo "无法与数据库建立连接!";
exit;
} else {
if(@mysql_select_db($dbName)) {mysql_query("SET NAMES 'utf8'");return $Conn;}
else {
echo "无法选择数据库!";
exit;
}
}
}
?
php最经典,最基础的代码,适合入门的
PHP是一种可以嵌入到HTML中的运行在服务器端的脚本语言,所以为了体现PHP的特性我们可以分两种模式来实现PHP代码
1、 PHP嵌入到HTML中,例如index.php
html
head/head
body
!--因为PHP嵌入到HTML中,所以需要完全区分PHP代码和HTML代码--
?php
//输出hello world
echo 'hello world;
?
/body
/html
2、 PHP独立文件,只有PHP代码,例如index.php
?php
//输出
echo 'hello world';
//不需要闭合标签
求个简单的php代码
_tags($string, $replace_with_space = true)
{
if ($replace_with_space) {
return preg_replace('![^]*?!', ' ', $string);
} else {
return strip_tags($string);
}
}
截取字符函数(匹配各种编码)
function truncate($string, $length = 80, $etc = '...', $break_words = false, $middle = false){
if ($length == 0)
return '';
if (is_callable('mb_strlen')) {
if (mb_detect_encoding($string, 'UTF-8, ISO-8859-1') === 'UTF-8') {
// $string has utf-8 encoding
if (mb_strlen($string) $length) {
$length -= min($length, mb_strlen($etc));
if (!$break_words !$middle) {
$string = preg_replace('/\s+?(\S+)?$/u', '', mb_substr($string, 0, $length + 1));
}
if (!$middle) {
return mb_substr($string, 0, $length) . $etc;
} else {
return mb_substr($string, 0, $length / 2) . $etc . mb_substr($string, - $length / 2);
}
} else {
return $string;
}
}
}
// $string has no utf-8 encoding
if (strlen($string) $length) {
$length -= min($length, strlen($etc));
if (!$break_words !$middle) {
$string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length + 1));
}
if (!$middle) {
return substr($string, 0, $length) . $etc;
} else {
return substr($string, 0, $length / 2) . $etc . substr($string, - $length / 2);
}
} else {
return $string;
}
}
综合就是
$arc=strip_tags($arc);
$arc=truncate($arc,200)
关于php实验代码和php实验总结报告的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
