时间:2021-07-01 10:21:17 帮助过:9人阅读
常见问题参考资料连接如下:
(1)http://www.2cto.com/os/201312/264364.html
(2)http://jingyan.baidu.com/article/ceb9fb10d909c48cac2ba06c.html
php教程简要入门版可参见W3C(供入门菜鸟使用):
下面给出几个PHP学习粗浅的例子,仅供娱乐:
例子1:表单提交处理页面
formSubmit.php文件:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <title>php basis-1</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="author" content="Wayne Ng" /> <meta name="description" content="basis-1" /> <meta name="revised" content="Wayne Ng,2016/2/16" /> <style> .errColor{color: red;} </style> </head> <body> <?php function process_input($data){ $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } $name = $email = $website = $comment = $gender = ""; $nameErr = $emailErr = $genderErr = $websiteErr = ""; if($_SERVER["REQUEST_METHOD"] == "POST"){ //提取name信息 if(empty($_POST["name"])){ $nameErr = "名字是必须的"; } else{ $name = process_input($_POST["name"]); //检查名字是否仅包含字母和空格 if(!preg_match("/^[a-zA-Z ]*$/", $name)){ $nameErr = "名字只能包含英文字母和空格"; } } //提取email信息 if(empty($_POST["email"])){ $emailErr = "邮箱是必须的"; } else{ $email = process_input($_POST["email"]); //检查邮箱名字是否合法 if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) { $emailErr = "无效的 email 格式!"; } } //提取website信息 if(empty($_POST["website"])){ $website = ""; } else{ $website = process_input($_POST["website"]); if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/% =~_|]/i",$website)) { $websiteErr = "无效的 URL"; } } //提取comment信息 if(empty($_POST["comment"])){ $comment = ""; } else{ $comment = process_input($_POST["comment"]); } //提取gender信息 if(empty($_POST["gender"])){ $genderErr = "性别是必须的"; } else{ $gender = process_input($_POST["gender"]); } } ?> <h2>PHP表单实例</h2> <p><span class="errColor">* 必填项目</span></p> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 姓名:    <input type="text" name="name" value="<?php echo $name;?>"/> <span class="errColor">*<?php echo $nameErr;?></span><br /> 邮箱:    <input type="text" name="email" value="<?php echo $email;?>"/> <span class="errColor">*<?php echo $emailErr;?></span><br /> 网址:    <input type="text" name="website" value="<?php echo $website;?>"/> <span class="errColor"><?php echo $websiteErr;?></span><br /> 评论:    <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea><br /> 性别:    <input type="radio" name="gender" <?php if(isset($gender) && $gender=="male") echo "checked";?> value="male">男 <input type="radio" name="gender" <?php if(isset($gender) && $gender=="female") echo "checked";?> value="female">女 <span class="errColor">*<?php echo $genderErr;?></span><br /><br /> <input type="submit" name="submit" value="提交表单"> </form> <?php echo "<h2>您提交的表单数据如下所示:</h2>"; echo "名字:  ".$name."<br />"; echo "邮箱:  ".$email."<br />"; echo "网址:  ".$website."<br />"; echo "评论:  ".$comment."<br />"; echo "性别:  ".$gender."<br />"; ?> </body> </html>
显示效果:
例子2:文件提交
FileSubmit.php文件:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <title>php basis-3</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="author" content="Wayne Ng" /> <meta name="description" content="basis-3" /> <meta name="revised" content="Wayne Ng,2016/2/17" /> <style> .warning{color: red;} </style> </head> <body> <h2>文件上传</h2> <span class="warning">*文件大小必须小于100kb</span> <form method="post" action="/uploadFile.php" enctype="multipart/form-data"> <label for="file">文件名:</label> <input type="file" name="file" id="file" /><br /> <input type="submit" name="submit" value="提交" /> </form> </body> </html>
uploadFile文件:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <?php //限制上传文件大小(必须小于100kb) if($_FILES["file"]["size"] < 100 * 1024){ if($_FILES["file"]["error"] > 0){ echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else{ echo "上传文件:".$_FILES["file"]["name"] . "<br />"; echo "文件类型:".$_FILES["file"]["type"] . "<br />"; echo "文件大小:".round($_FILES["file"]["size"]/1024, 1) . "kb<br />"; echo "临时文件:".$_FILES["file"]["tmp_name"] . "<br />"; //检验同名文件是都已经存在 if(file_exists("upload/" . $_FILES["file"]["name"])){ echo $_FILES["file"]["name"] . "已经存在!"; } else{ move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "保存于:" . "upload/" . $_FILES["file"]["name"]; } } } else{ echo "文件大小必须小于100kb"; } ?>
效果显示:
例子3:文件读写操作
fileOperation.php文件:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <title>php basis-2</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="author" content="Wayne Ng" /> <meta name="description" content="basis-2" /> <meta name="revised" content="Wayne Ng,2016/2/17" /> </head> <body> <?php function printFile($file){ while(!feof($file)){ echo fgets($file)."<br />"; } } $fileName = "spam.txt"; //查看原始文件内容 echo "文件原始内容:<br />"; $fileContent = fopen($fileName, ‘r‘) or die("Unable to open file!"); printFile($fileContent); fclose($fileContent); //文件末尾追加内容 $fileContent = fopen($fileName, ‘a+‘) or die("Unable to open file!"); fwrite($fileContent, "Maybe carrot doesn‘t tastes terrible.\n"); fclose($fileContent); //查看修改后文件内容 echo "文件修改后的内容:<br />"; $fileContent = fopen($fileName, ‘r‘) or die("Unable to open file!"); printFile($fileContent); fclose($fileContent); ?> </body> </html>
显示效果:
例子4:XML文件解析
game.xml文件:
<?xml version = "1.0" encoding = "UTF-8"?> <!DOCTYPE game SYSTEM "game.dtd"> <!-- Date:2016/1/26 Writer:Wayne Ng Theme:MapleStory hero briefInstruction --> <game> <warrior> <hero> <name>英雄</name> <weapons>剑、斧、钝器</weapons> <skill>终极打击</skill> </hero> <hero> <name>圣骑士</name> <weapons>剑、钝器</weapons> <skill>神圣冲击</skill> </hero> <hero> <name>黑骑士</name> <weapons>长枪、矛</weapons> <skill>黑暗穿刺</skill> </hero> </warrior> <magician> <hero> <name>主教</name> <weapons>长杖、短仗</weapons> <skill>圣光普照</skill> </hero> <hero> <name>火毒法师</name> <weapons>长杖、短仗</weapons> <skill>末日火焰</skill> </hero> <hero> <name>冰雷法师</name> <weapons>长杖、短仗</weapons> <skill>冰咆哮</skill> </hero> </magician> <archer> <hero> <name>神射手</name> <weapons>弓</weapons> <skill>暴风箭雨</skill> </hero> <hero> <name>箭神</name> <weapons>弩</weapons> <skill>终极扫射</skill> </hero> </archer> <ranger> <hero> <name>侠盗</name> <weapons>短剑、短刀</weapons> <skill>暗杀</skill> </hero> <hero> <name>隐士</name> <weapons>拳套</weapons> <skill>四连镖</skill> </hero> <hero> <name>暗影双刀</name> <weapons>短剑、短刀</weapons> <skill>终极斩</skill> </hero> </ranger> </game>
expatParser.php文件:
<?php header("Content-type: text/html; charset=utf-8"); //初始化XML解析器 $parser = xml_parser_create(); //当遇到元素头部(如<body>)时回调函数 function start($parser, $element_name, $element_attrs){ switch($element_name){ case "WARRIOR": {echo "<h2>warrior</h2>";break;} case "MAGICIAN": {echo "<h2>magician</h2>";break;} case "ARCHER": {echo "<h2>archer</h2>";break;} case "RANGER": {echo "<h2>ranger</h2>";break;} case "NAME": {echo "名称: ";break;} case "WEAPONS": {echo "武器: ";break;} case "SKILL": {echo "技能: ";break;} } } //当遇到元素尾部(如</body>)时回调函数 function stop($parser, $element_name){ switch($element_name){ case"NAME": {echo "<br />";break;} case"WEAPONS": {echo<