我的PHP練習

來源:互聯網
上載者:User
代碼如下:

附近下載: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;}}?>

謝謝瀏覽!

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.