代码如下:
附近下载:https://files.cnblogs.com/great/MyPHPTest.rar
Hello.php
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>我的第一个PHP程序</title> <style type="text/css"> div { border:solid 1px red; margin: 10px 0px 10px 0px; padding: 5px; } div.innerDiv { border-color:green; } </style> </head> <body> <?php /* php 变量名区分大小写,而 类名、方法名不区分大小写 */ function customError($error_level,$error_message /* 可选参数如下:error_file, error_line, error_context */) { echo "<b>发生错误:</b> [$error_level] $error_message<br />"; echo "即将停止脚步执行..."; die(); } set_error_handler("customError" , E_USER_WARNING); /* •E_USER_ERROR - 致命的用户生成的 run-time 错误。错误无法恢复。脚本执行被中断。 •E_USER_WARNING - 非致命的用户生成的 run-time 警告。脚本执行不被中断。 •E_USER_NOTICE - 默认。用户生成的 run-time 通知。脚本发现了可能的错误,也有可能在脚本运行正常时发生。 */ $testNum=0; if ($testNum>1) { trigger_error("参数必须小于1。"); } echo "<br/>"; function checkNum($num) { if($num > 1) { throw new Exception("值必须小于1"); } return true; } try { checkNum(0); echo "您传入的数字参数正确!"; } catch(Exception $e) { echo "调用checkNum()方法时,异常被捕获,异常信息:".$e->GETMessage(); } class MyCustomException extends Exception { public function errorMessage() { $errorMsg = "<span style=\"color:red;\">“错误行号:".$this->getLine().",在".$this->getFile().":<b>".$this->getMessage()."</b>”</span>"; return $errorMsg; } } echo "<br/>"; function checkPrice($tempPrice) { if($tempPrice > 100) { throw new MyCustomException("价格不能大于100"); } return true; } try { checkPrice(200); echo "您传入的价格参数正确!"; } catch(MyCustomException $e) { echo "调用checkPrice()方法时,异常被捕获,异常信息:".$e->errorMessage(); } echo "<br/>"; function CurrentPageException($ex) { echo "<span style=\"color:red;background-color:gray;\">当前页发生错误!信息:".$ex->getMessage()."</span>"; } set_exception_handler("CurrentPageException"); //throw new Exception("抛出Exception异常!"); echo "<br/>"; //throw new MyCustomException("抛出MyCustomException异常!"); //This is a comment /* 要让PHP显示错误,修改配置文件 php.ini 中,把 display_errors = Off 改为 On */ echo "Hello World,我是周杰伦"; ?> <br/> <div> <?php $myWord = "哈哈,世界和平!<br/>"; print "长度为:".strlen($myWord)."(提示:由于本文件的编码方式是UTF8,在PHP中,一个中文的长度为3,英文的长度为1,如果本文件的编码方式是GB2312,那么一个汉字的长度为2字节)<br/>"; echo $myWord."中国万岁".$myWord; //echo($myWord); //print($myWord); echo "字符串\"布鲁斯Bruce.刘Liu最棒\"中Liu的位置为:".strpos("布鲁斯Bruce.刘Liu最棒","Liu")."<br/>"; if(4>5) { echo "if执行<br/>"; } elseif(4==5) { echo "elseif执行<br/>"; } else { echo "else执行<br/>"; } $x="hello"; switch ($x) { case "bruce": echo "Number 1"; break; case "Hello": echo "Number 2"; break; case "hello": echo "Number 3"; break; default: echo "No number between 1 and 3"; } echo "<br/>"; $names = array("aaa","bbb","ccc"); echo $names[1]; echo "<br/>"; $ages = array("aaa"=>18,"bbb"=>19,"ccc"=>28); echo $ages["ccc"]; echo "<br/>"; $families = array( "mmm"=>array("name"=>"bruce","age"=>18,"sex"=>true), "nnn"=>array("name"=>"kim","age"=>19,"sex"=>false), "kkk"=>array("name"=>"yage","age"=>20,"sex"=>true) ); echo "姓名:".$families["kkk"]["name"].",性别:".$families["kkk"]["sex"]; echo "<br/>"; if($families["nnn"]["sex"]) { echo $families["nnn"]["name"]."的性别为男性!"; } else { echo $families["nnn"]["name"]."的性别为女性!"; } echo "<br/>PHP中的while循环、do while循环、for 循环与其他语言都类似!下面重点介绍 foreach 循环:<br/>"; foreach($names as $nameItem) { echo "姓名:".$nameItem."<br/>"; } echo "<br/>"; function Add($num1,$num2) { return $num1 + $num2; } echo "3.5+5=".Add(3.5,5 /* 如果第二个参数为 true,则结果为4,因为 true 的整数值为1 */); echo "<br/>"; echo date("y/m/d"); echo "<br/>"; /* 语法 mktime(hour,minute,second,month,day,year,is_dst) */ $tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y")); echo "Tomorrow is ".date("Y/m/d", $tomorrow); ?> </div> <?php /* include() 方法与 require() 的异同 同:服务器端引用 异:include() 函数会生成一个警告(但是脚本会继续执行),而 require() 函数会生成一个致命错误(fatal error)(在错误发生后脚本会停止执行)。 */ require("doSubmit.php"); ?> <div> <h2>读取文件</h2> <?php if(!file_exists("welcome.txt")) { die("welcome.txt没有找到!"); /* 不往下执行服务器脚本 */ } else { $file = fopen("welcome.txt", "r") or exit("不能打开文件!"); //Output a line of the file until the end is reached while(!feof($file)) { echo fgets($file). "<br />"; } fclose($file); } ?> </div> <?php require("doUpload.php"); ?> <div> <?php /* 语法:setcookie(name, value, expire, path, domain); */ if(!isset($_COOKIE["userInfo"])) { setcookie("userInfo", "Alex Porter", time()+3600 /* 一个小孩后过期 */); } else { echo "欢迎您:".$_COOKIE["userInfo"]."!<br/>"; } session_start(); /* Session 保存的Cookie名称为:PHPSESSID */ if(!isset($_SESSION["currentUserName"])) { $_SESSION["currentUserName"] = "张三"; } else { echo $_SESSION["currentUserName"]; //unset($_SESSION['currentUserName']); /* unset() 函数用于释放指定的 session 变量 */ //session_destroy(); /* session_destroy() 函数彻底终结 session */ } /* 发送邮件 语法:mail(to,subject,message,headers,parameters) $to = "testzhangsan@163.com"; $subject = "Test mail"; $message = "Hello! This is a simple email message."; $from = "testzhangsan@gmail.com"; $headers = "From: $from"; mail($to,$subject,$message,$headers); echo "Mail Sent."; */ ?> </div> <div> <div class="innerDiv"> <h2>函数过滤</h2> </div> <div> <?php $num1 = 15; echo "类型验证:"; if(!filter_var($num1,FILTER_VALIDATE_INT)) { //FILTER_VALIDATE_INT 区分大小写 echo "必须是整数类型!"; } else { echo "您输入的整数类型合法!"; } echo "<br/>"; echo "整数范围验证:"; $numRange = array( "options"=>array( "min_range"=>0, "max_range"=>30 ) ); //注意:上面的 options、min_range、max_range 不能写错 if(!filter_var($num1,FILTER_VALIDATE_INT, $numRange)) { echo "整数必须在0和30之间!"; } else { echo "您输入的整数的范围合法!"; } echo "<br/>"; function convertSpace($words) { return str_replace("_", " ", $words); } $string = "皮_特_是_一_个_好_人!"; echo filter_var($string, FILTER_CALLBACK, array("options"=>"convertSpace")); ?> </div> </div> </body> </html>
doSubmit.php
<div> <div class="innerDiv"> <h2>POST提交</h2> </div> <div class="innerDiv"> <form action="hello.php" method="post"> 姓名:<input type="text" name="name" /><br/> 年龄:<input type="text" name="age" /><br/> <input type="submit" value="提交" /> </form> </div> <div class="innerDiv"> <h3>您提交过来的信息如下</h3> 姓名:<?php echo isset($_POST["name"]) ? $_POST["name"] : ""; ?><br/> 年龄:<?php echo isset($_POST["age"]) ? $_POST["age"] : ""; ?><br/> </div> </div> <div> <div class="innerDiv"> <h2>GET提交</h2> </div> <div class="innerDiv"> <form action="hello.php" method="get"> 姓名:<input type="text" name="name" /><br/> 年龄:<input type="text" name="age" /><br/> <input type="submit" value="提交" /> </form> </div> <div class="innerDiv"> <h3>您提交过来的信息如下</h3> <?php /* PHP 的 $_REQUEST 变量包含了 $_GET, $_POST 以及 $_COOKIE 的内容。 */ ?> 姓名:<?php echo isset($_GET["name"]) ? $_GET["name"] : ""; /* 也可以用 $_REQUEST["name"] 获取 */ ?><br/> 年龄:<?php echo isset($_GET["age"]) ? $_GET["age"] : ""; ?><br/> </div> </div>
doUpload.php
<div> <div class="innerDiv"> <h2>文件上传</h2> </div> <div class="innerDiv"> <form action="Hello.php" method="post" enctype="multipart/form-data"> <label for="yourImages">您的照片:</label> <input type="file" name="yourPhotos" id="yourImages" /><br /> <input type="submit" name="submit" value="上传" /> </form> </div> <div class="innerDiv"> <h3>您提交过来的信息如下</h3> <?php if($_FILES["yourPhotos"]["error"] > 0) { echo "上传出错!信息:".$_FILES["yourPhotos"]["error"]."<br/>"; } else { /* 文件名:EditPlus V3.20.400 中文绿色版.RAR 类型:application/octet-stream 大小:1093.7607421875KB 存储路径:C:\WINDOWS\Temp\php2B.tmp */ $currentFileName = $_FILES["yourPhotos"]["name"]; $currentFileType = $_FILES["yourPhotos"]["type"]; $currentFileSize = $_FILES["yourPhotos"]["size"]; $currentFileTmp_Name = $_FILES["yourPhotos"]["tmp_name"]; echo "文件名:".$currentFileName."<br/>"; echo "类型:".$currentFileType."<br/>"; echo "大小:".($currentFileSize/1024)."KB<br/>"; echo "临时存储路径:".$currentFileTmp_Name."<br/>"; if (file_exists("uploadedFiles/" . $currentFileName)) { echo $currentFileName . " 已经存在!"; } else { move_uploaded_file($currentFileTmp_Name,"uploadedFiles/" . $currentFileName); echo "永久存储路径:" . "uploadedFiles/" . $currentFileName; } } ?> </div> </div>
谢谢浏览!