1.交互式运行 PHP
如果编译 PHP 时加入了 Readline 扩展(Windows 下不可用),那将会得到一个很好的外壳,包括一个自动完成的功能(例如可以在键入变量名的时候,按下TAB 键,PHP 会自动完成该变量名)以及命令历史记录,可以用上下键来访问。历史记录存在 ~/.php_history 文件中。
Note:
通过 auto_prepend_file 和 auto_append_file 包含的文件在此模式下会被解析,但有些限制,例如函数必须在被调用之前定义。
在php的cli模式下面用参数 php -d auto_prepend_file=/Users/czz/php/class/Person.php
这样在 test.php 下面执行的时候 就可以使用
$czz = new Person("chenzongzhi",23);
2.自动加载:让auto_prepend_file 载入一个能够自动吧__autoload 函数加上的.
所以就有了 spl_autoload_register()函数.
正确的做法是 写一个 Loader.php 用于对应找到相应的类的方法
<span style="font-size:14px;">class Loader
{
public function __construct()
{
spl_autoload_register('Loader::load');
}
public static function load($classname)
{
include_once($classname. ".php");
return true;
}
}</span>
然后 在prepend.php页面 require_once 'loader.php';
$loader = new Loader();
3.php -a交互模式
<?php
假如你在命令行下使用过php, 那么你可能知道用-r选项来执行代码. 这个特征非常棒, 但是很难正确处理"引用". 你需要知道是"单引"或"双引",还有你所使用的shell, 甚至你需要时刻保持对变量的警惕避免出错. 直接在STDIN中写入代码让php执行是件烦人的事情, 假设你想修改前面已写的代码的话. 在这种情况下,你只好把代码写入文件(即使那只有一行 + <?php)运行它. 为了解决这个问题, Marcus和我在php 5.1 cli sapi中加入了新的特征. 我很荣幸能够在这里介绍给大家: PHP交互控制台.
在你编译了PHP(GNU: --with-readline)或者(BSD: --with-libedit)后,你便能够通过使用PHP的-a选项进入这个控制台.
$ php -a
Interactive mode enabled
php >
你可以在这里输入些代码,然后得到反馈. 提示符会根据上下文而改变: php > $a = 1;
php > $b = 2;
php > echo $a + $b;
3
php > function foo() {
php { echo "foo
php " bar";
php { }
php > foo();
foo
bar
php >
但是这并非是我们全部拥有的. 更酷的特征是tab自动完成功能. 就象bash或者mysql控制台下你可以只输入前面几个字母,按下tab便获得可能的完成的单词列表. 目前,我们已经能够做到函数, 常量, 类名, 变量, 静态方法调用和类常量的自动完成. 目前我在进行对象方法和属性的自动完成工作. 目前自动完成尚有一些限制, 比如对于变量或对象, 它只能在先前行定义并执行后才会自动完成, 并不能在单行多次使用或在一个函数内很好地工作. 但是大多数情况下,它能避免我们打印很多字母.
eval('?>' . $data['content'] . '<?');
Please be sure to check that the short_open_tag is on in php.ini. Otherwise it will append the opening tag to the string.
If you are using eval function like
eval('?>' . $data['content'] . '<?php'); and you have short_open_tag on there will be parse error. So if it is required to use function like this turn off short_open_tags.
Also it is advisable to put a space after <?phpto avoid any accidental error.
<?php
$var = 'dynamic content';
echo eval('?>' . file_get_contents('template.phtml') .'<?');
?>
and the template.phtml:
< html>
<head>
<!-- ... -->
</head>
<body>
<!-- ... -->
<?=$var?>
<!-- ... -->
</body>
< /html>
This is something i use a lot (specially when studying new PHP Libraries or trying new jQuery plugins) and i think it might help others too.
<span style="font-size:14px;">class log{
public function _log($str){
//往数组中添加数据
}
public function flush()
{
if (! empty($this->_arrLogData['warning'])) {
file_put_contents($this->_strWarningFile, $this->_arrLogData['warning'], FILE_APPEND);
}
if (! empty($this->_arrLogData['normal'])) {
file_put_contents($this->_strNormalFile, $this->_arrLogData['normal'], FILE_APPEND);
}
$this->clean();
return $this;
}
public function clean()
{
$this->_arrLogData['warning'] = '';
$this->_arrLogData['normal'] = '';
return $this;
}
public function __destruct() //将写入放在析构函数中.
{
$this->flush();
}</span>
10. 将array key替换为value.
<span style="font-size:14px;">public static function format($tpl, $data)
{
if(isset($data) && is_array($data)){
$patten = array();
foreach($data as $_key => $_val){
$patten[$_key] = "/\{\?\=\s*".$_key."\s*\?\}/";
}
return preg_replace($patten, $data, $tpl);
}else{
return $tpl;
}
}
</span>
11. 调用函数,将输出保存到变量中
<span style="font-size:14px;">public function blockDataStart($varName)
{
$this->_tempVarName = $varName;
ob_start();
}
public function blockDataEnd()
{
$_buffer = ob_get_clean();
$this->setData($this->_tempVarName, $_buffer);
}</span>
12. error_report(E_ALL ^ E_NOTICE) 为什么是不报NOTICE错误呢?
因为很简单:
1 | E_ERROR (integer) | 致命的运行时错误。这类错误一般是不可恢复的情况,例如内存分配导致的问题。后果是导致脚本终止不再继续运行。 | |
2 | E_WARNING (integer) | 运行时警告 (非致命错误)。仅给出提示信息,但是脚本不会终止运行。 | |
4 | E_PARSE (integer) | 编译时语法解析错误。解析错误仅仅由分析器产生。 | |
8 | E_NOTICE (integer) | 运行时通知。表示脚本遇到可能会表现为错误的情况,但是在可以正常运行的脚本里面也可能会有类似的通知。 | |
16 | E_CORE_ERROR (integer) |
E_ALL ^ E_NOTICE 的意义是: 异或,相同的位变成0,即将 E_NOTICE为置0.而其他位不变.
E_ALL & ~E_NOTICE 的意义是: E_NOTICE反转,与和上式效果一样.
E_ERROR | E_WARNING | E_PARSE : 则是各位求与.
13.session.use_only_cookies
session有2种共享方式,cookie和get,sesion是否只通过cookie共享。
14.pear和pecl的区别?
Pear、Pecl都是PHP扩展模块的集合。扩展PHP有两种方法:
一种是用纯粹的PHP代码写函数和类。
Pear就是这样一个项目。PEAR是PHP的官方开源类库(PHP Extension and Application Repository的缩写)。Pear在英文中是梨子的意思。PEAR将PHP程序开发过程中常用的功能编写成类库,涵盖了页面呈面、数据库访问、文件操作、数据结构、缓存操作、网络协议等许多方面,用户可以很方便地使用。它是一个PHP扩展及应用的一个代码仓库,简单地说,PEAR就是PHP的cpan。其主页是pear.php.net。
另外一种是用c或者c++编写外部模块加载至php中。
Pecl(The PHP Extension Community Library)就是干这个事的,PHP的标准扩展,可以补充实际开发中所需的功能。所有的扩展都需要安装,在Windows下面以DLL的形式出现;在linux下面需要单独进行编译,它的表现形式为根据PHP官方的标准用C语言写成,尽管源码开放但是一般人无法随意更改源码。其主页是pecl.php.net。
最直接的表述:Pear是PHP的上层扩展,Pecl是PHP的底层扩展。
这两种方法其实都是为特定的应用提供现成的函数或者类,本质上来说都是一样的。
15.php-config是个什么意思?
php-config 是一个简单的命令行脚本用于获取所安装的 PHP 配置的信息。
在编译扩展时,如果安装有多个 PHP 版本,可以在配置时用 --with-php-config 选项来指定使用哪一个版本编译,该选项指定了相对应的 php-config 脚本的路径。
16.说说pcel?
安装
yum -y install php-pecl*
..............
Installed:
php-pecl-Fileinfo.i386 0:1.0.4-3.el5.centos php-pecl-memcache.i386 0:2.2.3-1.el5_2
Dependency Installed:
apr.i386 0:1.2.7-11.el5_6.5 apr-util.i386 0:1.2.7-11.el5_5.2 httpd.i386 0:2.2.3-82.el5.centos
php.i386 0:5.1.6-40.el5_9 php-cli.i386 0:5.1.6-40.el5_9 php-common.i386 0:5.1.6-40.el5_9
php-devel.i386 0:5.1.6-40.el5_9 php-pear.noarch 1:1.4.9-8.el5 postgresql-libs.i386 0:8.1.23-6.el5_8
Complete!
vim /usr/bin/pecl
可见pecl是peclcmd.php的封装。