用error_log函数写缓存

恩,没错,@伪造 在看ezSQL源代码的时候,总是看到作者用error_log函数写缓存,如:

error_log( serialize($result_cache), 3, $cache_file);

来看看error_log的原型:

bool error_log ( string $message [,
        int $message_type = 0 [, string $destination [, string $extra_headers ]]] )
error_log() log types
0 message is sent to PHP’s system logger, using the Operating System’s system logging mechanism or a file, depending on what the error_log configuration directive is set to. This is the default option.
1 message is sent by email to the address in the destination parameter. This is the only message type where the fourth parameter, extra_headers is used.
2 No longer an option.
3 message is appended to the file destination. A newline is not automatically added to the end of the message string.
4 message is sent directly to the SAPI logging handler.

当$message_type 值为3时,代表写文件,而且是以append的方式写文件。同类型的函数有file_put_contents,不过它是以覆盖方式写文件。而使用这两个函数,都能节约代码,因为file_put_conents和error_log不需要提前获得文件句柄,当然也不需要关闭它们。

深入理解JSON在PHP中的应用

一直都认为JSON很简单,自己完全掌握了,今天才知道,自己只是“好像”掌握了,真正用起来连格式都记不住。

群里的@TP新人(其实他是老人,不是什么新人,装纯的。。。:))今天问了个问题,引起了我的兴趣,并让我真正了解了JSON。

一、什么是JSON

先看看什么是JSON,可以在http://www.json.org/json-zh.html/详细查看,当然也可以看看阮一峰的博客,有一篇介绍数据类型和JSON格式:http://www.ruanyifeng.com/blog/2009/05/data_types_and_json.html

1、对象

对象是一个无序的“‘名称/值’对”集合。一个对象以“{”(左括号)开始,“}”(右括号)结束。每个“名称”后跟一个“:”(冒号);“‘名称/值’ 对”之间使用“,”(逗号)分隔。

也即,对象格式必须是如下形式:

{"foo":"bar", "hello":{"roy":"gu"}}

Continue reading

editplus3.30支持PHP代码预览

EditPlus 3.30的新特性:

* Supports TSVN commands (‘File’->’TSVN’).
* Supports ‘Use Pageant’ option for sftp.
* Supports menu bitmaps on Vista/7.
* Highlights all occurrences of selected word (‘View’->’Word Highlighting’).
* ‘Copy on Mouse Release’ option (‘Edit’->’Clipboard’).
* Hex Viewer supports ‘Show non-ASCII letters’ option.
* ‘Run php.exe on php files in browser preview’ option (‘Preferences’->’Tools’).
* ‘Preferences’->’Print’ dialog box supports ‘Hide line numbers’ option.
* ‘Min. line number digits’ option (‘Preferences’->’Layout’).
* Internal browser supports IE8/IE9.
* Internal browser supports displaying the information bar.
* Internal browser now opens new window as a tab.
* Combobox ‘auto append’ option (‘Preferences’->’General’).
* Supports $(CurLineText) argument macro for passing current line to user tool.
* Find in Files displays current file name on the Status Bar.
* ‘HTML Color’ command on the popup menu.
* ‘Copy Name’ command on the Document Selector popup menu.
* Updated toolbar buttons.
* Allows double click on the Change File Format dialog box.

哈哈,看到了吧,上面标红的那行,不过该版本并没有实现这个功能,直到前26号放出的bugfix才解决了这个问题。

* Could not run php.exe if it was added to PATH manually.

在系统环境变量中添加php.exe目录就可以了。不过在editplus浏览PHP文件时,editplus重新生成了一个新的html文件,我觉得不是很好,为什么不能直接显示解析后的结果呢。。。

PHP CLI Bat

恩,在Windows下用BAT调用PHP。

TITLE PHP CLI BAT
COLOR B0
@echo off
cls
echo.
php -n test.php
echo.
pause

test.php内容:

<?php
    echo "Hello, Wordl!\n";
    echo "你好,世界!";

结果(当然,要显示中文,要用ASCII编码):

用PHP查找数组中重复值或用SQL查找某字段重复值

用PHP实现:

<?php
    $a = array("red", "green", "pink", "yellow", "red", "pink", "red");
    $b = array_unique($a);
    //var_dump($b);
    $r = array_diff_key($a, $b);
    var_dump($r);

执行结果:

array(3) {
  [4]=>
  string(3) "red"
  [5]=>
  string(4) "pink"
  [6]=>
  string(3) "red"
}

用SQL:

SELECT fullname, COUNT(*) AS total FROM `ppusers`  GROUP BY `fullname` HAVING total > 1;

结果:

+----------+-------+
| fullname | total |
+----------+-------+
| roy      |     2 |
| solar    |     2 |
+----------+-------+
2 rows in set (0.00 sec)

用框架Model太多了,好久没有用SQL,竟然忘了Having子句,真是汗颜啊!!!

没事儿多翻翻手册,多用PHP常量

没事儿应多翻翻PHP手册,PHP函数极其丰富,很多时候我们都会重复造轮子。比如:今天有个问题,有一个路径如“/adurl.co/BullSoft.org/极客/Solar-PHP”形式,要判断有多少层怎么写?

群里lychee写了一个:

size(explode('/', '/a/b/c/1/2/3'))

我也写了个:

substr_count('/a/b/c/1/2/3', '/');

不错,其实PHP已经有函数可以处理。
第2个问题来了,如何它变成../../这种形式呢?也就是说有几层目录就有几个../
桦写了这个:

while($temp!='/'){
    $per .='../';
    dirname($temp);
}

咋看上去没问题,哈哈,但是一运行就是死循环,为什么?原来运行到根目录时,在Windows平台上,dirname函数返回的’\',而不是’/',所以要改为:

while($temp!='\\'){
    $per .='../';
    dirname($temp);
}

如果改成这样,在linux上用就麻烦了,因为一般大家在windows下开发,在linux下跑程序,所以这里最好用PHP常量:

while($temp!=DIRECTORY_SEPARATOR){
    $per .='../';
    dirname($temp);
}

我给出的方法是正则,哈哈。。。就像图中这样,