经常会看到:javascript
1
|
require_once
(
'../../lib/some_class.php'
);
|
该方法有不少缺点:php
它首先查找指定的php包含路径, 而后查找当前目录.css
所以会检查过多路径.html
若是该脚本被另外一目录的脚本包含, 它的基本目录变成了另外一脚本所在的目录.前端
另外一问题, 当定时任务运行该脚本, 它的上级目录可能就不是工做目录了.java
所以最佳选择是使用绝对路径:mysql
1
2
3
4
|
define(
'ROOT'
,
'/var/www/project/'
);
require_once
(ROOT .
'../../lib/some_class.php'
);
//rest of the code
|
咱们定义了一个绝对路径, 值被写死了. 咱们还能够改进它. 路径 /var/www/project 也可能会改变, 那么咱们每次都要改变它吗? 不是的, 咱们可使用__FILE__常量, 如:linux
1
2
3
4
5
6
7
|
//suppose your script is /var/www/project/index.php
//Then __FILE__ will always have that full path.
define(
'ROOT'
,
pathinfo
(
__FILE__
, PATHINFO_DIRNAME));
require_once
(ROOT .
'../../lib/some_class.php'
);
//rest of the code
|
如今, 不管你移到哪一个目录, 如移到一个外网的服务器上, 代码无须更改即可正确运行.程序员
能够在脚本头部引入多个文件, 像类库, 工具文件和助手函数等, 如:web
1
2
3
4
|
require_once
(
'lib/Database.php'
);
require_once
(
'lib/Mail.php'
);
require_once
(
'helpers/utitlity_functions.php'
);
|
这种用法至关原始. 应该更灵活点. 应编写个助手函数包含文件. 例如:
1
2
3
4
5
6
7
8
9
|
function
load_class(
$class_name
)
{
//path to the class file
$path
= ROOT .
'/lib/'
.
$class_name
.
'.php'
);
require_once
(
$path
);
}
load_class(
'Database'
);
load_class(
'Mail'
);
|
有什么不同吗? 该代码更具可读性.
將来你能够按需扩展该函数, 如:
1
2
3
4
5
6
7
8
9
10
|
function
load_class(
$class_name
)
{
//path to the class file
$path
= ROOT .
'/lib/'
.
$class_name
.
'.php'
);
if
(
file_exists
(
$path
))
{
require_once
(
$path
);
}
}
|
还可作得更多:
为一样文件查找多个目录
能很容易的改变放置类文件的目录, 无须在代码各处一一修改
可以使用相似的函数加载文件, 如html内容.
在开发环境中, 咱们打印数据库查询语句, 转存有问题的变量值, 而一旦问题解决, 咱们注释或删除它们. 然而更好的作法是保留调试代码.
在开发环境中, 你能够:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
define(
'ENVIRONMENT'
,
'development'
);
if
(!
$db
->query(
$query
)
{
if
(ENVIRONMENT ==
'development'
)
{
echo
"$query failed"
;
}
else
{
echo
"Database error. Please contact administrator"
;
}
}
|
在服务器中, 你能够:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
define(
'ENVIRONMENT'
,
'production'
);
if
(!
$db
->query(
$query
)
{
if
(ENVIRONMENT ==
'development'
)
{
echo
"$query failed"
;
}
else
{
echo
"Database error. Please contact administrator"
;
}
}
|
system, exec, passthru, shell_exec 这4个函数可用于执行系统命令. 每一个的行为都有细微差异. 问题在于, 当在共享主机中, 某些函数可能被选择性的禁用. 大多数新手趋于每次首先检查哪一个函数可用, 然而再使用它.
更好的方案是封成函数一个可跨平台的函数.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
/**
Method to execute a command in the terminal
Uses :
1.
system
2. passthru
3.
exec
4. shell_exec
*/
function terminal(
$command
)
{
//
system
if
(function_exists(
'system'
))
{
ob_start();
system
(
$command
,
$return_var
);
$output
= ob_get_contents();
ob_end_clean();
}
//passthru
else
if
(function_exists(
'passthru'
))
{
ob_start();
passthru(
$command
,
$return_var
);
$output
= ob_get_contents();
ob_end_clean();
}
//
exec
else
if
(function_exists(
'exec'
))
{
exec
(
$command
,
$output
,
$return_var
);
$output
= implode(
"\n"
,
$output
);
}
//shell_exec
else
if
(function_exists(
'shell_exec'
))
{
$output
= shell_exec(
$command
) ;
}
else
{
$output
=
'Command execution not possible on this system'
;
$return_var
= 1;
}
return
array(
'output'
=>
$output
,
'status'
=>
$return_var
);
}
terminal(
'ls'
);
|
上面的函数將运行shell命令, 只要有一个系统函数可用, 这保持了代码的一致性.
1
2
3
4
5
6
|
function
add_to_cart(
$item_id
,
$qty
)
{
$_SESSION
[
'cart'
][
'item_id'
] =
$qty
;
}
add_to_cart(
'IPHONE3'
, 2 );
|
使用上面的函数添加单个项目. 而当添加项列表的时候,你要建立另外一个函数吗? 不用, 只要稍加留意不一样类型的参数, 就会更灵活. 如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
function
add_to_cart(
$item_id
,
$qty
)
{
if
(!
is_array
(
$item_id
))
{
$_SESSION
[
'cart'
][
'item_id'
] =
$qty
;
}
else
{
foreach
(
$item_id
as
$i_id
=>
$qty
)
{
$_SESSION
[
'cart'
][
'i_id'
] =
$qty
;
}
}
}
add_to_cart(
'IPHONE3'
, 2 );
add_to_cart(
array
(
'IPHONE3'
=> 2 ,
'IPAD'
=> 5) );
|
如今, 同个函数能够处理不一样类型的输入参数了. 能够参照上面的例子重构你的多处代码, 使其更智能.
6. 有意忽略php关闭标签
我很想知道为何这么多关于php建议的博客文章都没提到这点.
1
2
3
4
5
|
<?php
echo
"Hello"
;
//Now dont close this tag
|
这將节约你不少时间. 咱们举个例子:
一个 super_class.php 文件
1
2
3
4
5
6
7
8
9
10
|
<?php
class
super_class
{
function
super_function()
{
//super code
}
}
?>
//super extra character after the closing tag
|
index.php
1
2
3
|
require_once
(
'super_class.php'
);
//echo an image or pdf , or set the cookies or session data
|
这样, 你將会获得一个 Headers already send error. 为何? 由于 “super extra character” 已经被输出了. 如今你得开始调试啦. 这会花费大量时间寻找 super extra 的位置.
所以, 养成省略关闭符的习惯:
1
2
3
4
5
6
7
8
9
10
|
<?php
class
super_class
{
function
super_function()
{
//super code
}
}
//No closing tag
|
这会更好.
这称为输出缓冲, 假如说你已在不一样的函数输出内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
function
print_header()
{
echo
"<div id='header'>Site Log and Login links</div>"
;
}
function
print_footer()
{
echo
"<div id='footer'>Site was made by me</div>"
;
}
print_header();
for
(
$i
= 0 ;
$i
< 100;
$i
++)
{
echo
"I is :
$i
<br />';
}
print_footer();
|
替代方案, 在某地方集中收集输出. 你能够存储在函数的局部变量中, 也可使用ob_start和ob_end_clean. 以下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
function
print_header()
{
$o
=
"<div id='header'>Site Log and Login links</div>"
;
return
$o
;
}
function
print_footer()
{
$o
=
"<div id='footer'>Site was made by me</div>"
;
return
$o
;
}
echo
print_header();
for
(
$i
= 0 ;
$i
< 100;
$i
++)
{
echo
"I is :
$i
<br />';
}
echo
print_footer();
|
为何须要输出缓冲:
>>能够在发送给浏览器前更改输出. 如 str_replaces 函数或多是 preg_replaces 或添加些监控/调试的html内容.
>>输出给浏览器的同时又作php的处理很糟糕. 你应该看到过有些站点的侧边栏或中间出现错误信息. 知道为何会发生吗? 由于处理和输出混合了.
输出一些xml.
1
2
3
4
5
6
7
|
$xml
=
'<?xml version="1.0" encoding="utf-8" standalone="yes"?>'
;
$xml
= "<response>
<code>0</code>
</response>";
//Send xml data
echo
$xml
;
|
工做得不错. 但须要一些改进.
1
2
3
4
5
6
7
8
|
$xml
=
'<?xml version="1.0" encoding="utf-8" standalone="yes"?>'
;
$xml
= "<response>
<code>0</code>
</response>";
//Send xml data
header(
"content-type: text/xml"
);
echo
$xml
;
|
注意header行. 该行告知浏览器发送的是xml类型的内容. 因此浏览器能正确的处理. 不少的javascript库也依赖头信息.
相似的有 javascript , css, jpg image, png image:
JavaScript
1
2
|
header(
"content-type: application/x-javascript"
);
echo
"var a = 10"
;
|
CSS
1
2
|
header(
"content-type: text/css"
);
echo
"#div id { background:#000; }"
;
|
曾经遇到过在mysql表中设置了unicode/utf-8编码, phpadmin也能正确显示, 但当你获取内容并在页面输出的时候,会出现乱码. 这里的问题出在mysql链接的字符编码.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
//Attempt to connect to database
$c
= mysqli_connect(
$this
->host ,
$this
->username,
$this
->password);
//Check connection validity
if
(!
$c
)
{
die
(
"Could not connect to the database host: <br />"
. mysqli_connect_error());
}
//Set the character set of the connection
if
(!mysqli_set_charset (
$c
,
'UTF8'
))
{
die
(
'mysqli_set_charset() failed'
);
}
|
一旦链接数据库, 最好设置链接的 characterset. 你的应用若是要支持多语言, 这么作是必须的.
php5.4前, 字符的默认编码是ISO-8859-1, 不能直接输出如À â等.
1
|
$value
= htmlentities(
$this
->value , ENT_QUOTES , CHARSET);
|
php5.4之后, 默认编码为UTF-8, 这將解决不少问题. 但若是你的应用是多语言的, 仍然要留意编码问题,.
考虑过使用 ob_gzhandler 吗? 不要那样作. 毫无心义. php只应用来编写应用. 不该操心服务器和浏览器的数据传输优化问题.
使用apache的mod_gzip/mod_deflate 模块压缩内容.
时常会用php输出动态javascript内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
$images
=
array
(
'myself.png'
,
'friends.png'
,
'colleagues.png'
);
$js_code
=
''
;
foreach
(
$images
as
$image
)
{
$js_code
.=
"'$image' ,"
;
}
$js_code
=
'var images = ['
.
$js_code
.
']; '
;
echo
$js_code
;
//Output is var images = ['myself.png' ,'friends.png' ,'colleagues.png' ,];
|
更聪明的作法, 使用 json_encode:
1
2
3
4
5
6
7
8
9
|
$images
=
array
(
'myself.png'
,
'friends.png'
,
'colleagues.png'
);
$js_code
=
'var images = '
. json_encode(
$images
);
echo
$js_code
;
//Output is : var images = ["myself.png","friends.png","colleagues.png"]
|
优雅乎?
写或保存文件前, 确保目录是可写的, 假如不可写, 输出错误信息. 这会节约你不少调试时间. linux系统中, 须要处理权限, 目录权限不当会致使不少不少的问题, 文件也有可能没法读取等等.
确保你的应用足够智能, 输出某些重要信息.
1
2
3
4
|
$contents
=
"All the content"
;
$file_path
=
"/var/www/project/content.txt"
;
file_put_contents
(
$file_path
,
$contents
);
|
这大致上正确. 但有些间接的问题. file_put_contents 可能会因为几个缘由失败:
>>父目录不存在
>>目录存在, 但不可写
>>文件被写锁住?
因此写文件前作明确的检查更好.
1
2
3
4
5
6
7
8
9
10
11
12
|
$contents
=
"All the content"
;
$dir
=
'/var/www/project'
;
$file_path
=
$dir
.
"/content.txt"
;
if
(
is_writable
(
$dir
))
{
file_put_contents
(
$file_path
,
$contents
);
}
else
{
die
(
"Directory $dir is not writable, or does not exist. Please check"
);
}
|
这么作后, 你会获得一个文件在何处写及为何失败的明确信息.
在linux环境中, 权限问题可能会浪费你不少时间. 从今日后, 不管什么时候, 当你建立一些文件后, 确保使用chmod设置正确权限. 不然的话, 可能文件先是由"php"用户建立, 但你用其它的用户登陆工做, 系统將会拒绝访问或打开文件, 你不得不奋力获取root权限, 更改文件的权限等等.
1
2
3
4
5
|
// Read and write for owner, read for everybody else
chmod
(
"/somedir/somefile"
, 0644);
// Everything for owner, read and execute for others
chmod
(
"/somedir/somefile"
, 0755);
|
15. 不要依赖submit按钮值来检查表单提交行为
1
2
3
4
|
if
(
$_POST
[
'submit'
] ==
'Save'
)
{
//Save the things
}
|
上面大多数状况正确, 除了应用是多语言的. 'Save' 可能表明其它含义. 你怎么区分它们呢. 所以, 不要依赖于submit按钮的值.
1
2
3
4
|
if
(
$_SERVER
[
'REQUEST_METHOD'
] ==
'POST'
and
isset(
$_POST
[
'submit'
]) )
{
//Save the things
}
|
如今你从submit按钮值中解脱出来了.
16. 为函数内总具备相同值的变量定义成静态变量
1
2
3
4
5
6
7
8
9
|
//Delay for some time
function
delay()
{
$sync_delay
= get_option(
'sync_delay'
);
echo
"<br />Delaying for $sync_delay seconds..."
;
sleep(
$sync_delay
);
echo
"Done <br />"
;
}
|
用静态变量取代:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
//Delay for some time
function
delay()
{
static
$sync_delay
= null;
if
(
$sync_delay
== null)
{
$sync_delay
= get_option(
'sync_delay'
);
}
echo
"<br />Delaying for $sync_delay seconds..."
;
sleep(
$sync_delay
);
echo
"Done <br />"
;
}
|
某些简单例子:
1
2
|
$_SESSION
[
'username'
] =
$username
;
$username
=
$_SESSION
[
'username'
];
|
这会致使某些问题. 若是在同个域名中运行了多个应用, session 变量可能会冲突. 两个不一样的应用可能使用同一个session key. 例如, 一个前端门户, 和一个后台管理系统使用同一域名.
从如今开始, 使用应用相关的key和一个包装函数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
define(
'APP_ID'
,
'abc_corp_ecommerce'
);
//Function to get a session variable
function
session_get(
$key
)
{
$k
= APP_ID .
'.'
.
$key
;
if
(isset(
$_SESSION
[
$k
]))
{
return
$_SESSION
[
$k
];
}
return
false;
}
//Function set the session variable
function
session_set(
$key
,
$value
)
{
$k
= APP_ID .
'.'
.
$key
;
$_SESSION
[
$k
] =
$value
;
return
true;
}
|
假如你在某文件中定义了不少工具函数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
function
utility_a()
{
//This function does a utility thing like string processing
}
function
utility_b()
{
//This function does nother utility thing like database processing
}
function
utility_c()
{
//This function is ...
}
|
这些函数的使用分散到应用各处. 你可能想將他们封装到某个类中:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
class
Utility
{
public
static
function
utility_a()
{
}
public
static
function
utility_b()
{
}
public
static
function
utility_c()
{
}
}
//and call them as
$a
= Utility::utility_a();
$b
= Utility::utility_b();
|
显而易见的好处是, 若是php内建有同名的函数, 这样能够避免冲突.
另外一种见解是, 你能够在同个应用中为同个类维护多个版本, 而不致使冲突. 这是封装的基本好处, 无它.
>>使用echo取代print
>>使用str_replace取代preg_replace, 除非你绝对须要
>>不要使用 short tag
>>简单字符串用单引号取代双引号
>>head重定向后记得使用exit
>>不要在循环中调用函数
>>isset比strlen快
>>始中如一的格式化代码
>>不要删除循环或者if-else的括号
不要这样写代码:
1
|
<span style=
"color:#333333;font-family:''Helvetica, Arial, sans-serif'';"
>
if
(
$a
== true)
$a_count
++;</span>
|
这绝对WASTE.
写成:
1
2
3
4
|
<span style=
"color:#333333;font-family:''Helvetica, Arial, sans-serif'';"
>
if
(
$a
== true)
{
$a_count
++;
}</span>
|
不要尝试省略一些语法来缩短代码. 而是让你的逻辑简短.
>>使用有高亮语法显示的文本编辑器. 高亮语法能让你减小错误.
好比说你想 trim 数组中的全部元素. 新手可能会:
1
2
3
4
|
foreach
(
$arr
as
$c
=>
$v
)
{
$arr
[
$c
] = trim(
$v
);
}
|
但使用 array_map 更简单:
1
|
$arr
=
array_map
(
'trim'
,
$arr
);
|
这会为$arr数组的每一个元素都申请调用trim. 另外一个相似的函数是 array_walk. 请查阅文档学习更多技巧.
你确定曾使用过正则表达式验证 email , ip地址等. 是的,每一个人都这么使用. 如今, 咱们想作不一样的尝试, 称为filter.
php的filter扩展提供了简单的方式验证和检查输入.
1
2
|
$amount
=
intval
(
$_GET
[
'amount'
] );
$rate
= (int)
$_GET
[
'rate'
];
|
这是个好习惯.
若是你使用php开发大型的应用, php承担了不少运算量, 速度会是一个很重要的指标. 使用profile帮助优化代码. 可以使用
xdebug和webgrid.
对于大的数组和字符串, 必须当心处理. 常见错误是发生数组拷贝致使内存溢出,抛出Fatal Error of Memory size 信息:
1
2
3
4
5
|
$db_records_in_array_format
;
//This is a big array holding 1000 rows from a table each having 20 columns , every row is atleast 100 bytes , so total 1000 * 20 * 100 = 2MB
$cc
=
$db_records_in_array_format
;
//2MB more
some_function(
$cc
);
//Another 2MB ?
|
当导入或导出csv文件时, 经常会这么作.
不要认为上面的代码会常常因内存限制致使脚本崩溃. 对于小的变量是没问题的, 但处理大数组的时候就必须避免.
确保经过引用传递, 或存储在类变量中:
1
2
|
$a
= get_large_array();
pass_to_function(&
$a
);
|
这么作后, 向函数传递变量引用(而不是拷贝数组). 查看文档.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
class
A
{
function
first()
{
$this
->a = get_large_array();
$this
->pass_to_function();
}
function
pass_to_function()
{
//process $this->a
}
}
|
尽快的 unset 它们, 让内存得以释放,减轻脚本负担.
确保你的脚本由始至终都使用单一的数据库链接. 在开始处正确的打开链接, 使用它直到结束, 最后关闭它. 不要像下面这样在函数中打开链接:
1
2
3
4
5
6
7
8
9
10
11
|
function
add_to_cart()
{
$db
=
new
Database();
$db
->query(
"INSERT INTO cart ....."
);
}
function
empty_cart()
{
$db
=
new
Database();
$db
->query(
"DELETE FROM cart ....."
);
}
|
使用多个链接是个糟糕的, 它们会拖慢应用, 由于建立链接须要时间和占用内存.
特定状况使用单例模式, 如数据库链接.
不厌其烦的写了太多以下的语句:
1
2
|
<span style=
"color:#333333;font-family:''Helvetica, Arial, sans-serif'';"
>
$query
=
"INSERT INTO users(name , email , address , phone) VALUES('$name' , '$email' , '$address' , '$phone')"
;
$db
->query(
$query
);
//call to mysqli_query()</span>
|
这不是个建壮的方案. 它有些缺点:
>>每次都手动转义值
>>验证查询是否正确
>>查询的错误会花很长时间识别(除非每次都用if-else检查)
>>很难维护复杂的查询
所以使用函数封装:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<span style=
"color:#333333;font-family:''Helvetica, Arial, sans-serif'';"
>
function
insert_record(
$table_name
,
$data
)
{
foreach
(
$data
as
$key
=>
$value
)
{
//mysqli_real_escape_string
$data
[
$key
] =
$db
->mres(
$value
);
}
$fields
= implode(
','
,
array_keys
(
$data
));
$values
=
"'"
. implode(
"','"
,
array_values
(
$data
)) .
"'"
;
//Final query
$query
=
"INSERT INTO {$table}($fields) VALUES($values)"
;
return
$db
->query(
$query
);
}
$data
=
array
(
'name'
=>
$name
,
'email'
=>
$email
,
'address'
=>
$address
,
'phone'
=>
$phone
);
insert_record(
'users'
,
$data
);</span>
|
看到了吗? 这样会更易读和扩展. record_data 函数当心的处理了转义.
最大的优势是数据被预处理为一个数组, 任何语法错误都会被捕获.
该函数应该定义在某个database类中, 你能够像 $db->insert_record这样调用.
查看本文, 看看怎样让你处理数据库更容易.
相似的也能够编写update,select,delete方法. 试试吧.
若是全部的内容都是从数据库获取的, 它们应该被缓存. 一旦生成了, 就將它们保存在临时文件中. 下次请求该页面时, 可直接从缓存中取, 不用再查数据库.
好处:
>>节约php处理页面的时间, 执行更快
>>更少的数据库查询意味着更少的mysql链接开销
基于文件的session策略会有不少限制. 使用基于文件的session不能扩展到集群中, 由于session保存在单个服务器中. 但数据库可被多个服务器访问, 这样就能够解决问题.
在数据库中保存session数据, 还有更多好处:
>>处理username重复登陆问题. 同个username不能在两个地方同时登陆.
>>能更准备的查询在线用户状态.
>>使用 defines/constants
>>使用函数获取值
>>使用类并经过$this访问
没据说过? 请看下面:
1
2
3
4
5
6
7
|
<head>
<base href=
"http://www.domain.com/store/"
>
</head>
<body>
<img src=
"happy.jpg"
/>
</body>
</html>
|
base 标签很是有用. 假设你的应用分红几个子目录, 它们都要包括相同的导航菜单.
www.domain.com/store/home.php
www.domain.com/store/products/ipad.php
在首页中, 能够写:
1
2
|
<a href=
"home.php"
>Home</a>
<a href=
"products/ipad.php"
>Ipad</a>
|
但在你的ipad.php不得不写成:
1
2
|
<span style=
"color:#333333;font-family:''Helvetica, Arial, sans-serif'';"
><a href=
"../home.php"
>Home</a>
<a href=
"ipad.php"
>Ipad</a></span>
|
由于目录不同. 有这么多不一样版本的导航菜单要维护, 很糟糕啊.
所以, 请使用base标签.
1
2
3
4
5
6
7
8
|
<span style=
"color:#333333;font-family:''Helvetica, Arial, sans-serif'';"
><head>
<base href=
"http://www.domain.com/store/"
>
</head>
<body>
<a href=
"home.php"
>Home</a>
<a href=
"products/ipad.php"
>Ipad</a>
</body>
</html></span>
|
如今, 这段代码放在应用的各个目录文件中行为都一致.
关闭不相的错误报告. E_FATAL 错误是很重要的.
1
2
|
<span style=
"color:#333333;font-family:'Helvetica, Arial, sans-serif';"
>
ini_set
(
'display_errors'
, 1);
error_reporting
(~E_WARNING & ~E_NOTICE & ~E_STRICT);</span>
|
integer在32位和64位体系结构中长度是不一样的. 所以某些函数如 strtotime 的行为会不一样.
在64位的机器中, 你会看到以下的输出.
1
2
3
4
5
6
7
8
9
|
<span style=
"color:#333333;font-family:''Helvetica, Arial, sans-serif'';"
>$ php -a
Interactive shell
php >
echo
strtotime
(
"0000-00-00 00:00:00"
);
-62170005200
php >
echo
strtotime
(
'1000-01-30'
);
-30607739600
php >
echo
strtotime
(
'2100-01-30'
);
4104930600</span>
|
但在32位机器中, 它们將是bool(false). 查看这里, 了解更多.
若是你想限制最小时间, 可使用下面的脚本:
1
2
3
|
<span style=
"color:#333333;font-family:''Helvetica, Arial, sans-serif'';"
>set_time_limit(30);
//Rest of the code</span>
|
高枕无忧吗? 注意任何外部的执行, 如系统调用,socket操做, 数据库操做等, 就不在set_time_limits的控制之下.
所以, 就算数据库花费了不少时间查询, 脚本也不会中止执行. 视状况而定.
一些例子:
>>mPDF -- 能经过html生成pdf文档
>>PHPExcel -- 读写excel
>>PhpMailer -- 轻松处理发送包含附近的邮件
>>pChart -- 使用php生成报表
使用开源库完成复杂任务, 如生成pdf, ms-excel文件, 报表等.
是时候使用像 codeigniter 这样的MVC框架了. MVC框架并不强迫你写面向对象的代码. 它们仅將php代码与html分离.
>>明确区分php和html代码. 在团队协做中有好处, 设计师和程序员能够同时工做.
>>面向对象设计的函数能让你更容易维护
>>内建函数完成了不少工做, 你不须要重复编写
>>开发大的应用是必须的
>>不少建议, 技巧和hack已被框架实现了
phpbench 提供了些php基本操做的基准测试结果, 它展现了一些徽小的语法变化是怎样致使巨大差别的.
查看php站点的评论, 有问题到IRC提问, 时常阅读开源代码, 使用Linux开发.