My PHP practice code is as follows:
Near Download: http://files.cnblogs.com/great/MyPHPTest.rar
Hello. php
My first PHP program
Error:[$ Error_level] $ error_message
"; Echo" is about to stop the execution... "; die ();} set_error_handler (" customError ", E_USER_WARNING );/*? E_USER_ERROR-fatal user-generated run-time error. The error cannot be recovered. The script execution is interrupted .? E_USER_WARNING-run-time warning generated by non-fatal users. Script execution is not interrupted .? E_USER_NOTICE-default. User-generated run-time notification. A possible error is found in the script, or it may occur when the script runs normally. */$ TestNum = 0; if ($ testNum> 1) {trigger_error ("The parameter must be smaller than 1. ");} Echo"
"; Function checkNum ($ num) {if ($ num> 1) {throw new Exception (" value must be less than 1 ");} return true ;} try {checkNum (0); echo "the number parameter you passed in is correct! ";} Catch (Exception $ e) {echo" when the checkNum () method is called, the Exception is caught. Exception information :". $ e-> GETMessage ();} class MyCustomException extends Exception {public function errorMessage () {$ errorMsg = "error row number :". $ this-> getLine (). ", in ". $ this-> getFile (). ":". $ This-> getMessage ()."""; Return $ errorMsg;} echo"
"; Function checkPrice ($ tempPrice) {if ($ tempPrice> 100) {throw new MyCustomException (" The price cannot exceed 100 ");} return true ;} try {checkPrice (200); echo "the price parameter you passed in is correct! ";} Catch (MyCustomException $ e) {echo" when the checkPrice () method is called, the exception is caught. exception information: ". $ e-> errorMessage ();} echo"
"; Function CurrentPageException ($ ex) {echo" current page error! Information: ". $ ex-> getMessage ()." ";} set_exception_handler (" CurrentPageException "); // throw new Exception (" Exception thrown! "); Echo"
"; // Throw new MyCustomException (" thrown MyCustomException! "); // This is a comment/* to make PHP display errors, modify the configuration file php. in ini, change display_errors = Off to On */echo "Hello World, I am Jay Chou";?>
"; Print" length :". strlen ($ myWord ). "(note: Because the encoding method of this file is UTF8, in PHP, the length of a Chinese character is 3, and the length of English is 1. if the encoding method of this file is GB2312, so the length of a Chinese character is 2 bytes)
"; Echo $ myWord. "Long live China ". $ myWord; // echo ($ myWord); // print ($ myWord); echo "string \" Bruce. liu's best \ "Liu's position is :". strpos ("Bruce. liu is the best "," Liu ")."
"; If (4> 5) {echo" if execution
";} Elseif (4 = 5) {echo" elseif execution
";} Else {echo" else execution
";}$ 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 "name :". $ families ["kkk"] ["name"]. ", Gender :". $ families ["kkk"] ["sex"]; echo"
"; If ($ families [" nnn "] [" sex "]) {echo $ families [" nnn "] [" name "]." Gender is male! ";} Else {echo $ families [" nnn "] [" name "]." Gender: Female! ";} Echo"
The while loop, do while loop, and for loop in PHP are similar to those in other languages! The following describes the foreach loop:
"; Foreach ($ names as $ nameItem) {echo" name: ". $ nameItem ."
";} Echo"
"; Function Add ($ num1, $ num2) {return $ num1 + $ num2;} echo" 3.5 + 5 = ". add (3.5, 5/* if the second parameter is true, the result is 4, because the integer of true is 1 */); echo"
"; Echo date (" y/m/d "); echo"
";/* Syntax mktime (hour, minute, second, month, day, year, is_dst) */$ tomorrow = mktime (0, 0, date (" m "), date ("d") + 1, date ("Y"); echo "Tomorrow is ". date ("Y/m/d", $ tomorrow);?>
Read files
";}Fclose ($ file) ;}?>
";} Session_start ();/* name of the Cookie saved by Session: PHPSESSID */if (! Isset ($ _ SESSION ["currentUserName"]) {$ _ SESSION ["currentUserName"] = "zhang san";} else {echo $ _ SESSION ["currentUserName"]; // unset ($ _ SESSION ['currentusername']);/* unset () function is used to release the specified session variable * // session_destroy ();/* session_destroy () the function permanently ends session */}/* send mail syntax: 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. "; */?>
Function filtering
"; Echo" integer range verification: "; $ numRange = array (" options "=> array (" min_range "=> 0," max_range "=> 30 )); // Note: if (! Filter_var ($ num1, FILTER_VALIDATE_INT, $ numRange) {echo "the integer must be between 0 and 30! ";} Else {echo" the range of the integer you entered is valid! ";} Echo"
"; Function convertSpace ($ words) {return str_replace (" _ "," ", $ words );} $ string = "skin _ Special _ Yes _ one _ good _ people! "; Echo filter_var ($ string, FILTER_CALLBACK, array (" options "=>" convertSpace ");?>
DoSubmit. php
POST submission
The information you submitted is as follows:
Age:
GET submit
The information you submitted is as follows:
Name:
Age:
DoUpload. php
File Upload
The information you submitted is as follows:
0) {echo "upload error! Information: ". $ _ FILES [" yourPhotos "] [" error "]."
";} Else {/* File name: EditPlus V3.20.400 Chinese green edition. RAR Type: application/octet-stream size: g03.7607421875kb storage path: C: \ WINDOWS \ Temp \ php2B. tmp */$ currentFileName = $ _ FILES ["yourPhotos"] ["name"]; $ currentFileType = $ _ FILES ["yourPhotos"] ["type"]; $ currentFileSize = $ _ FILES ["yourPhotos"] ["size"]; $ currentFileTmp_Name = $ _ FILES ["yourPhotos"] ["tmp_name"]; echo "File name :". $ currentFileName."
"; Echo" type: ". $ currentFileType ."
& Quot; echo & quot; Size: & quot;. ($ currentFileSize/1024). & quot; KB
"; Echo" Temporary Storage path: ". $ currentFileTmp_Name ."
"; If (file_exists (" uploadedFiles/". $ currentFileName) {echo $ currentFileName." already exists! ";}Else {move_uploaded_file ($ currentFileTmp_Name," uploadedFiles/". $ currentFileName); echo" permanent storage path: "." uploadedFiles/". $ currentFileName ;}}?>
Thank you!