一早上起来把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 alt="img" src="logo.png"></td> 83 <td><h1>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数据库