php编程中容易忽略的地方
一:fopen ( string $filename , string $mode [, bool $use_include_path = false [, resource $context ]] )
注意一:
如果 $mode = 'r' 时候,$filename 不存在 或者 $mode = 'w' 时候,$filename指定的文件不可以创建(如错误的路径或者不存在的路径名就会创建不成功)
则fopen会返回false,并产生一条notice错误日志,一定要记得用 @ 把notice 给屏蔽掉(有次跑个脚本,因为没屏蔽这个notice,而产生了好几百G的错误日志,把硬盘给搞满了)
注意二:
当$mode为’w‘ 时,默认会新建不存在的文件,如果指定的路径不存在,则新建不成功,当$mode = 'r' 不会新建文件
注意三:
Note - using fopen in 'w' mode will NOT update the modification time (filemtime) of a file like you may expect. You may want to issue a touch() after writing and closing the file which update its modification time. This may become critical in a caching situation, if you intend to keep your hair.
注意四:
Note that if specifying the optional 'b' (binary) mode, it appears that it cannot be the first letter for some unaccountable reason. In other words, "br" doesn't work, while "rb" is ok!
二:bool error_log ( string $message [, int $message_type = 0 [, string $destination [, string $extra_headers ]]] )
注意一:$message_type = 3 时会自动创建文件,但是不会自动创建文件夹,当文件路径中又文件夹时要注意
三:尽管 display_errors 也可以在运行时设置 (使用 ini_set() ), 但是脚本出现致命错误时任何运行时的设置都是无效的。 因为在这种情况下预期运行的操作不会被执行。
display_startup_errors boolean
即使 display_errors 设置为开启, PHP 启动过程中的错误信息也不会被显示。强烈建议除了调试目的以外,将 display_startup_errors 设置为关闭。
例如:
error_reporting(E_ALL);
ini_set('display_errors','Off');
echo 234/0
exit;
这样仍然会报错:Parse error: syntax error, unexpected T_EXIT, expecting ',' or ';'
四:mixed pathinfo ( string $path [, int $options = PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME ] )
并不检验传递的$path是否合法,是否存在。
<?php
print_r(pathinfo('/no/where/file.txt'));
?>
which will output:
Array
(
[dirname] => /no/where
[basename] => file.txt
[extension] => txt
[filename] => file
)
五:parse_str — 将字符串解析成多个变量 void parse_str ( string $str [, array &$arr ] )
注意:
1:This function automatically urldecodes values
2:be careful using parse_str() without the [array &$arr] parameter, as this may override values of global and existing variables.
<?php
$var1 = 1;
parse_str('var1=one&var2=two');
// $var1 is now 'one'
六:int file_put_contents ( string $filename , mixed $data [, int $flags = 0 [, resource $context ]] )
注意:
1:If filename does not exist, the file is created. Otherwise, the existing file is overwritten, unless the FILE_APPEND flag is set. (文件不存在可以创建,但是文件所在目录不存在则不能创建)
解决办法:
function file_force_contents($filename, $data, $flags = 0){
if(!is_dir(dirname($filename)))
mkdir(dirname($filename).'/', 0777, TRUE);
return file_put_contents($filename, $data,$flags);
}
2:This function returns the number of bytes that were written to the file, or FALSE on failure. (所以如果判断是否正确需要用 === )
3:参数$data可以是 字符串 或 一维数组,具体看:http://php.net/manual/en/function.file-put-contents.php
4:$flags(FILE_USE_INCLUDE_PATH | FILE_APPEND | LOCK_EX)
5:This function is
binary-safe.
例子一:// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file, $person, FILE_APPEND | LOCK_EX);
例子二:It should be obvious that this should only be
used if you're making one write, if you are writing multiple times to
the same file you should handle it yourself with fopen and fwrite, the
fclose when you are done writing.
Benchmark below:
file_put_contents() for 1,000,000 writes - average of 3 benchmarks:
real 0m3.932s
user 0m2.487s
sys 0m1.437s
fopen() fwrite() for 1,000,000 writes, fclose() - average of 3 benchmarks:
real 0m2.265s
user 0m1.819s
sys 0m0.445s
例子四:
It's important to understand that LOCK_EX will
not prevent reading the file unless you also explicitly acquire a read
lock (shared locked) with the PHP 'flock' function.
i.e. in concurrent scenarios file_get_contents may return empty if you don't wrap it like this:
<?php
$myfile=fopen('test.txt','rt');
flock($myfile,LOCK_SH);
$read=file_get_contents('test.txt');
fclose($myfile);
?>
If
you have code that does a file_get_contents on a file, changes the
string, then re-saves using file_put_contents, you better be sure to do
this correctly or your file will randomly wipe itself out.
七: string file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = -1 [, int $maxlen ]]]] )
The function returns the read data or FALSE on failure.
注意:
1:An E_WARNING level error is generated if filename cannot be found, maxlength is less than zero, or if seeking to the specified offset in the stream fails.
2:This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
php编程中容易忽略的地方的更多相关文章
- Java编程中“为了性能”尽量要做到的一些地方
最近的机器内存又爆满了,除了新增机器内存外,还应该好好review一下我们的代码,有很多代码编写过于随意化,这些不好的习惯或对程序语言的不了解是应该好好打压打压了. 下面是参考网络资源总结的一些在Ja ...
- Java之基础(1) - 编程中“为了性能”尽量要做到的一些地方
最近的机器内存又爆满了,除了新增机器内存外,还应该好好review一下我们的代码,有很多代码编写过于随意化,这些不好的习惯或对程序语言的不了解是应该好好打压打压了. 下面是参考网络资源总结的一些在Ja ...
- java编程中'为了性能'一些尽量做到的地方
原文地址:http://blog.csdn.NET/m13666368773/article/details/7796924 最近的机器内存又爆满了,出了新增机器内存外,还应该好好review一下我们 ...
- 写代码的心得,怎么减少编程中的 bug?
遭遇 bug 的时候,理性的程序员会说:这个 bug 能复现吗? 自负型:这不可能,在我这是好好的. 经验型:不应该,以前怎么没问题? 幻想型:可能是数据有问题. 无辜型:我好几个星期都没碰这块代码了 ...
- PHP编程中10个最常见的错误
PHP是一种非常流行的开源服务器端脚本语言,你在万维网看到的大多数网站都是使用php开发的.本篇经将为大家介绍PHP开发中10个最常见的问题,希望能够对朋友有所帮助. 错误1:foreach循环后留下 ...
- [转] 怎么减少编程中的 bug?
[转]http://macshuo.com/?p=1361 怎么减少编程中的 bug? Posted on 2016 年 2 月 17 日 为什么要编程?因为代码没在那里.创造一个世界是如此让人着迷, ...
- (转)Attribute在.net编程中的应用
Attribute在.net编程中的应用(一)Attribute的基本概念 经常有朋友问,Attribute是什么?它有什么用?好像没有这个东东程序也能运行.实际上在.Net中,Attribute是一 ...
- Microsoft.VisualC 命名空间包含支持用 c + + 语言的代码生成和编译的类。 混合编程中使用COM接口指针
Microsoft.VisualC 命名空间包含支持用 c + + 语言的代码生成和编译的类. Microsoft.VisualC.StlClr Unmanaged Code 和 Managed Co ...
- Java编程中的一些常见问题汇总
转载自 http://macrochen.iteye.com/blog/1393502 每天在写Java程序,其实里面有一些细节大家可能没怎么注意,这不,有人总结了一个我们编程中常见的问题.虽然一般 ...
随机推荐
- Geodesic-based robust blind watermarking method for three-dimensional mesh animation by using mesh segmentation and vertex trajectory
之前因为考试,中断了实验室的工作,现在结束考试了,不能再荒废了. 最近看了一篇关于序列水印的文章,大体思想是:对于一个网格序列,首先对第一帧进行处理,在第一帧上,用网格分割算法(SDF)将网格分割成几 ...
- Rust 中的继承与代码复用
在学习Rust过程中突然想到怎么实现继承,特别是用于代码复用的继承,于是在网上查了查,发现不是那么简单的. C++的继承 首先看看c++中是如何做的. 例如要做一个场景结点的Node类和一个Sprit ...
- 利用HTML 5中的Menu和Menuitem元素快速创建菜单
原文:Introducing the HTML5 “Menu” and “Menuitem” Elements 译文:HTML 5中Menu和Menuitem的元素介绍 译者:dwqs 今天向你介绍H ...
- document.body、document.documentElement和window获取视窗大小的区别
来源:http://www.ido321.com/906.html 在w3school关于window对象的介绍中,介绍了获取浏览器窗口大小的三种方法(浏览器的视口,不包括工具栏和滚动条). 对于In ...
- bzoj 2393 Cirno的完美算数教室(容斥原理+搜索)
[题意] 定义C数为只包含数字2和9的数,求[L,R]内能被C数整除的个数. [思路] Dfs预处理出C数,并去除其中倍数的情况. Dfs搜索出现情况,奇数加,偶数减,当数值大于R时剪枝. [代码] ...
- C++实现ping功能
今天接到需求要实现ping的功能,然后网上查了一些资料,对网络编程的一些函数熟悉了一下,虽然还有一些细节不清楚,但是慢慢积累. 要实现这样的功能: 基础知识 ping的过程是向目的IP发送一个type ...
- PyBayes的安装和使用
PyBayes 主页 文档 PyBayes is an object-oriented Python library for recursive Bayesian estimation (Bayesi ...
- Chapter 2 Build Caffe
Caffe for windows 的build药按照一定的顺序进行. ============================================================ 先以b ...
- FIREDAC连接MSSQL 2000报不能支持连接MSSQL2000及更低版本的解决办法
FIREDAC连接MSSQL 2000的时候会报错,原因是MSSQL CLIENT11或MSSQL CLIENT10客户端驱动程序已经不支持连接MSSQL2000及更低版本的数据库. 解决办法: 设置 ...
- Unity3D之Mecanim动画系统学习笔记(十一):高级功能应用
动作游戏 还记得读书的时候熬夜打<波斯王子>的时光,我们的王子通过跳跃穿过墙壁的小洞.在高层建筑上进行攀爬和跳跃,还有在操作失误掉下高楼和触发必死机关后使用时之沙的时光倒流功能回归死亡之前 ...