红酥手,黄滕酒,满城春色宫墙柳。东风恶,欢情薄。一怀愁绪,几年离索。错!错!错!
春如旧,人空瘦,泪痕红浥鲛绡透。桃花落,闲池阁。山盟虽在,锦书难托。莫!莫!莫!

Check Syntax or Execute PHP file in Vim

Jun 25th, 2007 | Filed under Vim

以前,我介绍过了如何在 VIM 中检查 PHP 的语法错误。这方面的知识,在网上也有很多人介绍。平时我们在编写 PHP 程序的时候,检查语法错误非常的方便,这样我们在浏览器中查看 PHP 程序运行效果时,就不会看到令人恼火的 PHP 语法错误了。

但有些时候,我们如果是想运行一小段简单的 PHP 代码时,该怎么办呢?以前我是通过 EditPlus 的 User Tools 来实现的,EP 的 User Tools 可以设定一个外部程序(比如 PHP 解析器),来运行 PHP 文件,然后在 Output Window 中显示运行的结果,非常方便。这次呢,我会介绍一下如何在 Vim 中实现相同的功能。

首先看一下下面的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
"""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Parse and execute current php file
"""""""""""""""""""""""""""""""""""""""""""""""""""""
 
function! PhpParseExecute()
    " Parse and execute current php file
    setlocal makeprg=C:\PHP5\php.exe\ -f
 
    " Set shellpipe
    setlocal shellpipe=>
 
    " Use error format for parsing PHP error output
    setlocal errorformat=%m\ in\ %f\ on\ line\ %l
    make %
    clist
endfunction
 
map <F6> :call PhpParseExecute()<CR>
imap <F6> <ESC>:call PhpParseExecute()<CR>

如果你能看的懂 Vim Script,上面的代码一定会让你豁然开朗,其实这也是 Vim 官方网站的中的一个 Script,并不是我的发现。在很早之前我就在不断的思考,如何在 Vim 中执行 PHP 文件。我当初的做法是使用 “!” 调用 php.exe 来运行 PHP 文件,这个方法的确可行,但糟糕的是,Vim 在 Windows 平台上调用外部程序会弹出一个 DOS 窗口,非常的 Ugly!于是我只好放弃了这种非常之不优雅的做法,呵呵!直到我前段时间看到这个 Script,于是一切问题迎刃而解:P。

上面的 Script 思路很清晰,首先设置 makeprg 指定 PHP 解析器的位置,并使用 :make 命令来运行 PHP 脚本,然后设定 shellpipe 将运行结果发送到 VIM 的 “error file”,这里的 “>” 是 DOS 系统上面的默认值。最后执行 clist,打开 QuickFix 窗口,显示运行结果,如果有错误则直接显示 PHP 错误信息,而配置的 errorformat 正则表达式,在返回 PHP 错误时,按下回车键后,直接关闭 QuickFix,并把光标定位到出错的一行。

同样,我们也可以根据上面的脚本模板来改造一下,PHP 语法检查的功能:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
"""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Check PHP Syntax using makeprg
"""""""""""""""""""""""""""""""""""""""""""""""""""""
 
function! PhpCheckSyntax()
    " Check php syntax
    setlocal makeprg=\"C:\php5\php.exe\"\ -l\ -n\ -d\ html_errors=off
 
    " Set shellpipe
    setlocal shellpipe=>
 
    " Use error format for parsing PHP error output
    setlocal errorformat=%m\ in\ %f\ on\ line\ %l
    make %
endfunction
 
" Perform :PhpCheckSyntax()
map <F5> :call PhpCheckSyntax()<CR>
imap <F5> <ESC>:call PhpCheckSyntax()<CR>

为什么要改造这个语法检查的功能呢?因为按照以前做法,两者会冲突的,呵呵 :)

Tags: , ,

WordPress Plugin – Fanfou Tools 1.0 Beta 3

Jun 25th, 2007 | Filed under WordPress
Comments Off

Fanfou Logo

解决无法从 Fanfou 删除消息以及无法删除好友的问题。

详细信息、下载,请访问:
WordPress Plugin – Fanfou Tools

Fanfou 的 Firefox 插件

Jun 23rd, 2007 | Filed under Firefox

Fanfou 闲逛,看人家的自言自语,结果看到一个奇怪的 API 名称:FFFB,打开一看发现了一个新的 Fanfou 插件 —— Fanfou Toolbar

这是 Michael@TheCore 开发的一个 Firefox 的插件,它可以:

在网页里选中一段文字, 单击右键, 从context 菜单中选取”Fanfou This”. 这段文字就会post到饭否上. 由于有140字的限制, 我们叫它”断章取义”. :)

对于经常使用 Firefox 访问饭否的网友来说,这个插件的确非常的方便。

另外还有两款不错的插件:

WordPress Plugin – Fanfou Tools 1.0 Beta 2

Jun 23rd, 2007 | Filed under WordPress
Comments Off

Fanfou Logo

完善细节,修正一些已知的 Bugs,如增加一些链接修正帐号测试功能等,另外也添加了一些新功能,包括:

  • 在 WordPress 中发布 Fanfou 消息
  • 删除好友
  • 管理已经缓冲到本地的饭否消息

等等,不过也发现了不少问题,比如删除好友和删除消息无法工作,多半是 API 功能尚未实现。

下一个版本,将主要实现文摘功能和消息、好友列表的分页,另外尝试修正上面提到的问题。

详细信息、下载,请访问:
WordPress Plugin – Fanfou Tools

WordPress 2.2.1 Released

Jun 21st, 2007 | Filed under WordPress

WordPress 2.2.1

早上做好 Fanfou Tools ,写完发布日志后,才发现 WordPress 2.2.1 已经发布了,在 Nicky 的博客上也看到了消息。

从版本号上面看来,应该没什么太大的变化,估计只是修正了一些 Bugs,但在开发插件的过程中,我也发现 2.2.1 中一些 WordPress 中定义的 PHP Constant,如 DDSG_* 等已经被删除了。如果你也是插件作者并且使用了这些常量,那么这里需要留意一下,这只是一个小问题,并不会影响插件运行,但如果 PHP 的 Error Reporting Level 级别比较高,可能会给出一些 Notice 报错,如果你使用的是自己的主机,最好还是关闭这些报错信息 (在 php.ini 中关闭 display_errors ),以免泄露服务器文件的路径信息。

Download:
WordPress 2.2.1

Tags:

WordPress Plugin – Fanfou Tools

Jun 21st, 2007 | Filed under WordPress

Fanfou Logo

这是一个 FanfouWordPress 插件,通过这个插件你可以在 WordPress 的侧边栏中显示最新的 fanfou 消息,每隔一段时间自动更新;同时当 Blog 发布新的文章时,向 fanfou.com 发布消息。

该插件模仿了 Alex King 的 Twitter Tools

Requirements:
这个插件的最早的开发平台为 WordPress 2.2.1 RC2,很多其它的版本限于精力我都没有测试,但能保证在最新的WP开发版上正常运行,如果您在使用中遇到问题,请升级您的WordPress或者联系我均可。

Latest Version: 1.2

Installation:
解压缩后上传到您的 wp-content/plugins/ 目录,并在后台激活该插件。
编辑你正在使用的 WordPress 主题的侧标栏模板,加入如下的代码即可在侧边栏中显示最新的 Fanfou 消息:

<!-- Fanfou Tools -->
<?php fanfou_list_posts();?>

Uninstallation:
这个插件将会创建一个新的数据表 wp_fanfou,并在 wp_options 表中创建多条以 fanfou_ 为前缀的 option 记录,此外如果激活 Notify 功能,那么在发布新文章或者编辑旧文章时,该插件将自动为文章加上名为 fanfou_marker 的 Custom field,并自动通知到 Fanfou(前提是旧文章不存在 fanfou_marker 这个 field,如果已有此标记,那么不会重复通知到 Fanfou)。

如果要卸载此插件,请按照如下步骤即可清理干净:

  1. Deactive Fanfou-tools
  2. 删除 wp-content/plugins/fanfou-tools 目录
  3. 删除数据表 wp_fanfou (这里需要非常小心…)
  4. 清理 wp_options 表
  5. 最后清理 wp_postmeta 表,即 Custom Field 信息
-- 请注意安全,不嫌麻烦的话,最好先备份数据库...
 
-- 清理 wp_options 表
DELETE FROM `wp_options`
    WHERE `option_name` LIKE 'fanfou_%';
 
-- 清理 wp_postmeta 表
DELETE FROM `wp_postmeta`
    WHERE `meta_key` LIKE 'fanfou_marker';

Download:
Fanfou Tools 1.2

SubVersion:

# 下载正在开发中的版本
svn co http://svn.wp-plugins.org/fanfou-tools/trunk
 
# 下载稳定的版本
svn co http://svn.wp-plugins.org/fanfou-tools/tags/1.2

WordPress 2.2.1 RC2

Jun 16th, 2007 | Filed under WordPress
Comments Off

WordPress 2.2.1 RC2

这是 WordPress 2.2.1 第二个 RC 版本,11号的时候发布了 RC1,这个版本仍然是修复了一些 Bug。

Download:
WordPress 2.2.1 RC2

Tags:

Floating Point Precision and Accuracy

Jun 12th, 2007 | Filed under PHP
Comments Off

其实这不是 PHP 的问题,其它编程语言中都会有类似的问题。

首先看看 PHP 手册上面怎么说的:

Floating point precision

It is quite usual that simple decimal fractions like 0.1 or 0.7 cannot be converted into their internal binary counterparts without a little loss of precision. This can lead to confusing results: for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8 as the result of the internal representation really being something like 7.9999999999….

This is related to the fact that it is impossible to exactly express some fractions in decimal notation with a finite number of digits. For instance, 1/3 in decimal form becomes 0.3333333. . ..

So never trust floating number results to the last digit and never compare floating point numbers for equality. If you really need higher precision, you should use the arbitrary precision math functions or gmp functions instead.

简单的说,float 存在精度问题,是不可依赖的,比如 0.1 + 0.7,一年级的小朋友都知道答案是 0.8,但是实际上计算机计算的值为 0.7999999……

所以永远不要拿 float 来做大小比较,比如下面的代码:

$sum = 0.1 + 0.7;
if ($sum == 0.8) {
    /** some code **/
}

上面 if 块中的代码永远不会执行,因为 $sum != 0.8,简单的方法就是把 $sum 转换为 string 类型,PHP 会自动调整精度。

$sum = 0.1 + 0.7;
if ((string) $sum === '0.8') {
    /** some code **/
}

这样就没有问题了:)

Tags:

PHP 的返回引用

Jun 12th, 2007 | Filed under PHP
Comments Off

返回引用,在定义和调用的时候 & 都不可以省略。

这是个比较容易混淆的概念,如果不是出于某些特殊的原因,建议还是不要使用。

说他很容易混,是因为 Reference 在 PHP5 中的变化,造成其在 PHP4/PHP5 中表现的行为差异比较大。

举个例子说明:

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
< ?php
 
$color = 'YellowGreen';
 
function &getRef() {
    global $color;
    return $color;
}
 
function getCopy() {
    global $color;
    return $color;
}
 
$colorRef = &getRef();
$colorRef = 'Blue';
 
//$colorCopy = getCopy();
//$colorCopy = 'Black';
 
 
var_dump($color);
var_dump($colorRef);
//var_dump($colorCopy);
 
 
/* vim: set expandtab tabstop=4 shiftwidth=4: */

运行一遍上面的代码,差不多就明白了,使用 &getRef() 将把 $colorRef 绑定到 $color 上面,也就是说 $colorRef 和 $color 两个变量是指向同一个值了。当改变其中一个值时,另一个也会跟着变化。

Read more…

Tags:

Vim for PHP Programmer

Jun 12th, 2007 | Filed under Vim

开始编写文档 Vim for PHP Programmer,主要参考Andrei Zmievski的在Vancouver PHP Conf上发表的同名文档,并添油加醋,追加分享自己的一些Vim经验。:)

目前只是刚开了个头,最近没工作也闲着没事,可以专心来做这个事情了。

其实很早之前就想写相关的东西了,本想用docbook来写,方便编译成HTML/PDF等格式发布,但自己没有电脑,在网吧里面搭建docbook可不是一件轻松的事情,我这个人最怕麻烦了,实在懒得搞,于是一直拖到现在。

想来这个毛病是时候改改了,想做什么事情,就应该立即动手去做,畏首畏尾的不太像话啊!

Vim for PHP Programmer

Tags: ,