当前位置:Gxlcms > PHP教程 > 我的PHP练习

我的PHP练习

时间:2021-07-01 10:21:17 帮助过:3人阅读

代码如下:

附近下载:http://files.cnblogs.com/great/MyPHPTest.rar

Hello.php

我的第一个PHP程序发生错误: [$error_level] $error_message
"; 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 "
";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 = "“错误行号:".$this->getLine().",在".$this->getFile().":".$this->getMessage().""; return $errorMsg; }}echo "
";function checkPrice($tempPrice){ if($tempPrice > 100) { throw new MyCustomException("价格不能大于100"); } return true;}try{ checkPrice(200); echo "您传入的价格参数正确!";}catch(MyCustomException $e){ echo "调用checkPrice()方法时,异常被捕获,异常信息:".$e->errorMessage();}echo "
";function CurrentPageException($ex){ echo "当前页发生错误!信息:".$ex->getMessage()."";}set_exception_handler("CurrentPageException");//throw new Exception("抛出Exception异常!"); echo "
";//throw new MyCustomException("抛出MyCustomException异常!");//This is a comment/*要让PHP显示错误,修改配置文件 php.ini 中,把display_errors = Off改为 On*/echo "Hello World,我是周杰伦";?>
"; print "长度为:".strlen($myWord)."(提示:由于本文件的编码方式是UTF8,在PHP中,一个中文的长度为3,英文的长度为1,如果本文件的编码方式是GB2312,那么一个汉字的长度为2字节)
";echo $myWord."中国万岁".$myWord;//echo($myWord);//print($myWord);echo "字符串\"布鲁斯Bruce.刘Liu最棒\"中Liu的位置为:".strpos("布鲁斯Bruce.刘Liu最棒","Liu")."
";if(4>5){ echo "if执行
";}elseif(4==5){ echo "elseif执行
";}else{ echo "else执行
";}$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 "
";$names = array("aaa","bbb","ccc");echo $names[1];echo "
";$ages = array("aaa"=>18,"bbb"=>19,"ccc"=>28);echo $ages["ccc"];echo "
";$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 "
";if($families["nnn"]["sex"]){ echo $families["nnn"]["name"]."的性别为男性!";}else{ echo $families["nnn"]["name"]."的性别为女性!";}echo "
PHP中的while循环、do while循环、for 循环与其他语言都类似!下面重点介绍 foreach 循环:
";foreach($names as $nameItem){ echo "姓名:".$nameItem."
";}echo "
";function Add($num1,$num2){ return $num1 + $num2;}echo "3.5+5=".Add(3.5,5 /* 如果第二个参数为 true,则
结果为4,因为 true 的整数值为1 */);echo "
";echo date("y/m/d");echo "
";/*语法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);?>

读取文件

"; } fclose($file);}?>";}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.";*/?>

函数过滤

"; 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 "
"; function convertSpace($words) { return str_replace("_", " ", $words); } $string = "皮_特_是_一_个_好_人!"; echo filter_var($string, FILTER_CALLBACK, array("options"=>"convertSpace")); ?>

doSubmit.php

			

POST提交

您提交过来的信息如下

姓名:
年龄:

GET提交

您提交过来的信息如下

姓名:
年龄:

doUpload.php

			

文件上传

您提交过来的信息如下

0) { echo "上传出错!信息:".$_FILES["yourPhotos"]["error"]."
"; } 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."
"; echo "类型:".$currentFileType."
"; echo "大小:".($currentFileSize/1024)."KB
"; echo "临时存储路径:".$currentFileTmp_Name."
"; if (file_exists("uploadedFiles/" . $currentFileName)) { echo $currentFileName . " 已经存在!"; } else { move_uploaded_file($currentFileTmp_Name,"uploadedFiles/" . $currentFileName); echo "永久存储路径:" . "uploadedFiles/" . $currentFileName; } } ?>

谢谢浏览!

人气教程排行