一早上起来把50包开了,一张橙卡。。。就问还有谁。。。。。。。。。。。本命年啊,我去买红内裤还不行么。。。。
实时更新,老哥的号的30包什么都没有。。。。不过中午又开了5包,皇帝,好评啊!!!
五、代码重用与函数编写
include警告与require的错误;
大量的包含实现,可以改ini文件中的:auto_prepend_file和auto_append_file;
global关键字也是可以用在函数内的参数的;
参数的引用传递和return
namespace Bible\Basic\FunctionUse; include_once 'index.php';
$value=10;
increment1($value);
echo $value;echo "<br/>";
$value2=100;
$value2=increment2($value2);
echo $value2;echo "<br/>";
<?php
function increment1(&$value,$mount=1)
{
$value=$value+$mount;
} function increment2($value,$mount=1)
{
$value=$value+$mount;
return $value;
}
递归函数比循环慢且占用更多内存,虽然很多循环都可以用递归来代替;(在应用领域中基本不用它
六、面向对象
好玩的代码,学到些东西!
<?php
namespace Bible\Basic\ObjectPage; //Chapter6.P132 class Page
{
public $content;
public $title;
public $keyword;
public $button=array("Home"=>"http://115.159.201.78/wordpress/",
"Basic"=>"Section1.php"
); public function __set($name,$value)
{
$this->$name=$value;
} public function Display()
{
echo "<html>\n<head>\n";
$this->DisplayTitle();
$this->DisplayKeywords();
$this->DisplayStyles();
echo "</head>\n<body>\n";
$this->DisplayHeader();
$this->DisplayMenu($this->button);
echo $this->content;
$this->DisplayFooter();
echo "</body>\n</html>\n";
} private function DisplayTitle()
{
echo "<title>".$this->title."</title>";
} private function DisplayKeywords()
{
foreach ($this->keyword as $words){
echo "<meta name=\"keywords\" content=\"".$words."\"/>";
}
} private function DisplayStyles()
{
?>
<style>
h1 {
color:white; font-size:24pt; text-align:center;
font-family:arial,sans-serif
}
.menu {
color:white; font-size:12pt; text-align:center;
font-family:arial,sans-serif; font-weight:bold
}
td {
background:black
}
p {
color:black; font-size:12pt; text-align:justify;
font-family:arial,sans-serif
}
p.foot {
color:white; font-size:12pt; text-align:center;
font-family:arial,sans-serif; font-weight:bold
}
a:link,a:visited,a:active {
color:white
}
</style>
<?php
} private function DisplayHeader()
{
?>
<table width="100%" cellpadding="12"
cellspacing="0" border="0">
<tr bgcolor="black">
<td align="left"><img alt="img" src="logo.png"></td>
<td><h1>The OOP page</h1></td>
</tr>
</table>
<?php
} private function DisplayMenu($button)
{
echo "<table width=\"100%\" bgcolor=\"white\"
cellpadding=\"4\" cellsapcing=\"4\">\n ";
echo "<tr>\n";
$width=100/count($button);
while (list($name,$url)=each($button)) {
$this->DisplayButton($width,$name,$url,
!$this->IsURLCurrentPage($url));
}
echo "</tr>\n";
echo "</table>\n";
} private function IsURLCurrentPage($url)
{
if (strpos($_SERVER['PHP_SELF'], $url)==false){
return false;
} else{
return true;
}
} private function DisplayButton($width,$name,$url,$active=true)
{
if ($active){
echo "<td width=\"".$width."%\">
<a href=\"".$url."\" onclick=\"blank\">
<img src=\"w-logo-blue.png\" alt=\"".$name."\" border=\"0\" /></a>
<a href=\"".$url."\"><span class=\"menu\">".$name."</span></a>
</td>";
} else{
echo "<td width=\"".$width."%\">
<img src=\"w-logo-white.png\">
<span class=\"menu\">".$name."</span></td>";
}
} private function DisplayFooter()
{
?>
<table width="100%" bgcolor="black" cellpadding="12" border="0">
<tr>
<td>
<p class="foot">© Andy Liang.</p>
<p class="foot">Please visit my own site:<a href="http://115.159.201.78/wordpress/">Andy's Learning Diary</a></p>
</td>
</tr>
</table>
<?php
}
} $homepage=new Page();
$homepage->content="<p>"."I do not know what to write down, how about this?"."</p>";
$homepage->title="You have to try OO.";
$homepage->keyword=array("SAR","MRF");
$homepage->Display();
就是个默认网页的生成对象,还是有改进空间的。
然后,php写html,有点繁琐,但写完了一身爽。
<?php ?>标记的活用有点厉害啊,套路~
PS:上面代码中的链接不要乱点~会吓到你的。
当然,其实这种得到页面在应用中是不推荐的,只是试着练下手。
七、错误和异常处理
try{ throw new Exception() } catch{ }
PHP中,异常必须手动抛出;
try代码块和catch代码块是“绑定的”,每个try一定要有一个catch!
一个try可以有多个catch
<?php
//Session1.Chapter7.P146
namespace Bible\Basic\ExceptionTry; try {
throw new \ErrorException("A serious wrong has occured!", 14);
}
catch (\ErrorException $e){
echo "Exceotion".$e->getCode().":".$e->getMessage()."<br/>"
."File:".$e->getFile()."at Line:".$e->getLine()."<br/>";
echo $e;
}
应用中,希望可以自定义异常处理:继承已有的Exception类就好,需要注意的是,一般的getMessage等是final的,不能进行重载的,只有_tostring这一个方法可以重载;
class MyException extends \ErrorException
{
function _tostring()
{
return "Fatal error,sorry!";
}
} try {
throw new MyException("OH", 14);
}
catch (MyException $e){
echo $e->_tostring();
}
应用中,常常把异常处理用在最容易出错的I/O部分;格式一般都是
try{ if(!...) throw...} catch(){}
Session 2
(二) 使用MySQL
八、设计Web数据库