php -a 交互式 0 === false???,PHP 的 Interactive shell 模式,Centos安装git并装Boris不用重新编译PHP实现类python命令行交互。

jackxiang 2015-8-26 11:38 | |
背景:有时PHP像python那样可交互能对小代码片段的验证提高效果,但默认编译好像是不行的。
[root@localhost htdocs]# php -i | grep readline
[root@localhost htdocs]# 主要的差異在於 Compile 時, 有沒有加入 "--with-readline",

试着安一个扩展:
readline 相关错误
现象:configure: error: Please reinstall libedit – I cannot find readline.h
解决办法:安装 Editline Library (libedit),官网:http://thrysoee.dk/editline/
下载最新版 libedit 编译安装即可。
直载后,直接:configure && make && make install

正确安装如下:
#php -a
Interactive shell

php > echo "hello" ;
hello

不对的输出如下,输入 php -a 之后,基本就没有反应了。:
#php -a
Interactive mode enabled

解决方法:
重新编译安装php,加入--with-readline选项。
php -i | grep "\-\-with\-readline" --color
不正确的输出是空,正确的输出编译项里有。
_____________________________________________________________________________
./configure --with-readline
configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.


./configure
make && make install

[root@iZ25dcp92ckZ readline]# make install
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/

安上了,但还是失败了:
[root@iZ25dcp92ckZ readline]# php -a
php > echo "helo";
php: symbol lookup error: /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/readline.so: undefined symbol: append_history

这儿有一个兄弟也在问一样的问题,说是:php 的 readline 模块和所使用的 readline 库的版本差异太大了。
摘自:http://segmentfault.com/q/1010000002455369
——————————————————————————————————————————————————————


0 === false???

php > var_dump( 0 === false);
bool(false)
php > $res =0;
php > var_dump( $res === false);
bool(false)



php -a
Interactive mode enabled

<?php
echo time() . "\n";
$a = 1;
echo $a;
?> Ctrl + d
1440560402
1





————————————————————————————————————————————————————
臨時要測個簡單的程式片段 或 Function, 一般都會於 CLI 寫來跑一跑, Interactive 模式 可以邊寫邊測試~ (互動模式, 寫完一行就 Compile 一行)

而 PHP CLI 的模式有兩種差異: (執行方法: $ php -a, 說明可見: PHP: Interactive shell)

Interactive shell 比較像是 互動式, 有問有答的執行程式.
Interactive mode enabled 則是輸入一整個區塊的程式碼, Ctrl-D 執行.

目前 (2012年) Ubuntu Linux 內建預設的是 Interactive shell, 而 Debian 是 Interactive mode enabled.

主要的差異在於 Compile 時, 有沒有加入 "--with-readline", 可使用 "php -i | grep readline" 查看.

Interactive mode enabled

$ php -a
Interactive mode enabled

<?php
echo time() . "\n";
$a = 1;
echo $a;
?> Ctrl + d
1330954647
1

Interactive shell

$ php -a
Interactive shell

php > echo time();
1330954675
php > $a = 1;
php > echo $a;
1

来自:http://blog.longwin.com.tw/2012/03/php-interactive-shell-2012/
解决办法就是重新编译并安装一次加上参数:
http://blog.csdn.net/pqhdp/article/details/9386185



在php Interactive mode里敲代码不需要<?php ?>的啊,直接输入echo "hello"; 回车就可以了。
还有php现在有一个还不错的repl实现
https://github.com/borisrepl/boris
比 php -a 要更舒服一些。

演示:http://segmentfault.com/a/1190000000353069
需要git。


—————————Centos安装git并装Boris不用重新编译PHP实现类python命令行交互。—————————
[root@localhost boris-1.0.10]# yum install git
Loading "fastestmirror" plugin
Loading mirror speeds from cached hostfile
* base: 10.64.5.100
* updates: 10.64.5.100
* addons: 10.64.5.100
* extras: 10.64.5.100
base                      100% |=========================| 1.1 kB    00:00    
updates                   100% |=========================| 1.9 kB    00:00    
primary.sqlite.bz2        100% |=========================| 504 kB    00:00    
addons                    100% |=========================| 1.9 kB    00:00    
extras                    100% |=========================| 2.1 kB    00:00    
Setting up Install Process
Parsing package install arguments
No package git available.
Nothing to do
[root@localhost boris-1.0.10]# cat /etc/redhat-release
CentOS release 5.2 (Final)
[root@localhost boris-1.0.10]# rpm -Uvh http://dl.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
Retrieving http://dl.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
warning: /var/tmp/rpm-xfer.Q9d5Ba: Header V3 DSA signature: NOKEY, key ID 217521f6
Preparing...                ########################################### [100%]
   1:epel-release           ########################################### [100%]
[root@localhost boris-1.0.10]# yum install git        
Loading "fastestmirror" plugin
Loading mirror speeds from cached hostfile

yum install git
Total download size: 7.5 M
Is this ok [y/N]: y
Downloading Packages:
(1/4): git-1.8.2.1-1.el5.  20% |=====                    | 1.5 MB    00:12 ETA

安git参看:http://www.letuknowit.com/post/19.html,为何要安?是因为想用它安装PHP的Boris——PHP也有REPL。
演示:http://segmentfault.com/a/1190000000353069  

你可以通过 Packagist 来安装 Boris。当然你也可以直接克隆它的 git 仓库:

git clone git://github.com/d11wtq/boris.git
cd boris
./bin/boris
Boris以MIT许可证发布。更多信息请访问Boris在GitHub上的项目主页。

[root@localhost local]# git clone git://github.com/d11wtq/boris.git
Cloning into 'boris'...
remote: Counting objects: 1061, done.
remote: Total 1061 (delta 0), reused 0 (delta 0), pack-reused 1061
Receiving objects: 100% (1061/1061), 204.32 KiB | 62 KiB/s, done.
Resolving deltas: 100% (506/506), done.


[root@localhost local]# cd boris
[root@localhost boris]# ./bin/boris

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:http://jackxiang.com/post/8234/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!


最后编辑: jackxiang 编辑于2018-10-23 15:27
评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]