PHP_Bibel阅读笔记(二)--脸黑的一天(?一年)
一早上起来把50包开了,一张橙卡。。。就问还有谁。。。。。。。。。。。本命年啊,我去买红内裤还不行么。。。。
实时更新,老哥的号的30包什么都没有。。。。不过中午又开了5包,皇帝,好评啊!!!
五、代码重用与函数编写
include警告与require的错误;
大量的包含实现,可以改ini文件中的:auto_prepend_file和auto_append_file;
global关键字也是可以用在函数内的参数的;
参数的引用传递和return
1 namespace Bible\Basic\FunctionUse;2 3 include_once 'index.php';4 $value=10;5 increment1($value);6 echo $value;echo "<br/>";7 $value2=100;8 $value2=increment2($value2);9 echo $value2;echo "<br/>";
1 <?php 2 function increment1(&$value,$mount=1) 3 { 4 $value=$value+$mount; 5 } 6 7 function increment2($value,$mount=1) 8 { 9 $value=$value+$mount;10 return $value;11 }
递归函数比循环慢且占用更多内存,虽然很多循环都可以用递归来代替;(在应用领域中基本不用它
六、面向对象
好玩的代码,学到些东西!
1 <?php 2 namespace Bible\Basic\ObjectPage; 3 4 //Chapter6.P132 5 6 class Page 7 { 8 public $content; 9 public $title; 10 public $keyword; 11 public $button=array("Home"=>"http://115.159.201.78/wordpress/", 12 "Basic"=>"Section1.php" 13 ); 14 15 public function __set($name,$value) 16 { 17 $this->$name=$value; 18 } 19 20 public function Display() 21 { 22 echo "<html>\n<head>\n"; 23 $this->DisplayTitle(); 24 $this->DisplayKeywords(); 25 $this->DisplayStyles(); 26 echo "</head>\n<body>\n"; 27 $this->DisplayHeader(); 28 $this->DisplayMenu($this->button); 29 echo $this->content; 30 $this->DisplayFooter(); 31 echo "</body>\n</html>\n"; 32 } 33 34 private function DisplayTitle() 35 { 36 echo "<title>".$this->title."</title>"; 37 } 38 39 private function DisplayKeywords() 40 { 41 foreach ($this->keyword as $words){ 42 echo "<meta name=\"keywords\" content=\"".$words."\"/>"; 43 } 44 } 45 46 private function DisplayStyles() 47 { 48 ?> 49 <style> 50 h1 { 51 color:white; font-size:24pt; text-align:center; 52 font-family:arial,sans-serif 53 } 54 .menu { 55 color:white; font-size:12pt; text-align:center; 56 font-family:arial,sans-serif; font-weight:bold 57 } 58 td { 59 background:black 60 } 61 p { 62 color:black; font-size:12pt; text-align:justify; 63 font-family:arial,sans-serif 64 } 65 p.foot { 66 color:white; font-size:12pt; text-align:center; 67 font-family:arial,sans-serif; font-weight:bold 68 } 69 a:link,a:visited,a:active { 70 color:white 71 } 72 </style> 73 <?php 74 } 75 76 private function DisplayHeader() 77 { 78 ?> 79 <table width="100%" cellpadding="12" 80 cellspacing="0" border="0"> 81 <tr bgcolor="black"> 82 <td align="left"><img src="/static/imghw/default1.png" data-src="logo.png" class="lazy" alt="img"></td> 83 <td><h1 id="The-OOP-page">The OOP page</h1></td> 84 </tr> 85 </table> 86 <?php 87 } 88 89 private function DisplayMenu($button) 90 { 91 echo "<table width=\"100%\" bgcolor=\"white\" 92 cellpadding=\"4\" cellsapcing=\"4\">\n "; 93 echo "<tr>\n"; 94 $width=100/count($button); 95 while (list($name,$url)=each($button)) { 96 $this->DisplayButton($width,$name,$url, 97 !$this->IsURLCurrentPage($url)); 98 } 99 echo "</tr>\n";100 echo "</table>\n";101 }102 103 private function IsURLCurrentPage($url)104 {105 if (strpos($_SERVER['PHP_SELF'], $url)==false){106 return false;107 } else{108 return true;109 }110 }111 112 private function DisplayButton($width,$name,$url,$active=true)113 {114 if ($active){115 echo "<td width=\"".$width."%\">116 <a href=\"".$url."\" onclick=\"blank\">117 <img src=\"w-logo-blue.png\" alt=\"".$name."\" border=\"0\" /></a>118 <a href=\"".$url."\"><span class=\"menu\">".$name."</span></a>119 </td>";120 } else{121 echo "<td width=\"".$width."%\">122 <img src=\"w-logo-white.png\" alt="PHP_Bibel阅读笔记(二)--脸黑的一天(?一年)" >123 <span class=\"menu\">".$name."</span></td>"; 124 } 125 }126 127 private function DisplayFooter()128 {129 ?>130 <table width="100%" bgcolor="black" cellpadding="12" border="0">131 <tr>132 <td>133 <p class="foot">© Andy Liang.</p>134 <p class="foot">Please visit my own site:<a href="http://115.159.201.78/wordpress/">Andy's Learning Diary</a></p>135 </td>136 </tr>137 </table>138 <?php 139 }140 }141 142 $homepage=new Page();143 $homepage->content="<p>"."I do not know what to write down, how about this?"."</p>";144 $homepage->title="You have to try OO.";145 $homepage->keyword=array("SAR","MRF");146 $homepage->Display();
就是个默认网页的生成对象,还是有改进空间的。
然后,php写html,有点繁琐,但写完了一身爽。
标记的活用有点厉害啊,套路~
PS:上面代码中的链接不要乱点~会吓到你的。
当然,其实这种得到页面在应用中是不推荐的,只是试着练下手。
七、错误和异常处理
try{ throw new Exception() } catch{ }
PHP中,异常必须手动抛出;
try代码块和catch代码块是“绑定的”,每个try一定要有一个catch!
一个try可以有多个catch
1 <?php 2 //Session1.Chapter7.P146 3 namespace Bible\Basic\ExceptionTry; 4 5 try { 6 throw new \ErrorException("A serious wrong has occured!", 14); 7 } 8 catch (\ErrorException $e){ 9 echo "Exceotion".$e->getCode().":".$e->getMessage()."<br/>"10 ."File:".$e->getFile()."at Line:".$e->getLine()."<br/>";11 echo $e; 12 }
应用中,希望可以自定义异常处理:继承已有的Exception类就好,需要注意的是,一般的getMessage等是final的,不能进行重载的,只有_tostring这一个方法可以重载;
1 class MyException extends \ErrorException 2 { 3 function _tostring() 4 { 5 return "Fatal error,sorry!"; 6 } 7 } 8 9 try {10 throw new MyException("OH", 14);11 }12 catch (MyException $e){13 echo $e->_tostring();14 }
应用中,常常把异常处理用在最容易出错的I/O部分;格式一般都是
try{ if(!...) throw...} catch(){}
Session 2
(二) 使用MySQL
八、设计Web数据库

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.
