其实就是总结了一下这个帖子
文档
PHP文档的特点:
浏览方便
用户评论是一大亮点
要查询某个函数,输入:www.php.net/function 就会跳转到该函数的具体页
数组
PHP的数组是我用过的语言中最强大的,其实就是个有序映射表(ordered map),并为各种使用作了优化。可以看作列表、哈希表、字典、集合、栈、队列。
流处理
可以扩展文件流,比如excel流处理,可以通过下面的语句来创建一个excel文件
$fp = fopen("xlsfile://tmp/test.xls", "wb");
if (!is_resource($fp)) {
die("Cannot open excel file");
}
$data= array(
array("Name" => "Bob Loblaw", "Age" => 50),
array("Name" => "Popo Jijo", "Age" => 75),
array("Name" => "Tiny Tim", "Age" => 90)
);
fwrite($fp, serialize($data));
fclose($fp);
魔术方法
public function __get($key) { ... }
public function __set($key, $value) { ... }
public function __call($func, $args) { ... }
public function __toString() { ... }
//...
or/and
isset($foo) OR print '$foo is not defined';
spl_autoload_register
使用spl_autoload_register,而不是__autoload,因为spl_autoload_register允许设置多个实例,而不会覆盖。
spl_autoload_register('autoloader');
function autoloader($class)
{
//这里可以对$class进行处理}
set_include_path
设置引用路径,比如引用zend的libraries时。
set_include_path(get_include_path() . PATH_SEPARATOR . '../libs/');`
处理远程文件/url
$fp = fopen('http://example.com');
$str = file_get_contents('http://example.com/file');
$imageInfo = getimagesize('ftp://user:password@ftp.example.com/image/name.jpg');
虽然这些函数的健壮性和稳定性不如curl,但毕竟使用方便。
strtr
使用方便,效率高,用来替换字符串内的特定值。有点类似str_replace,但更灵活。
echo strtr('hello world', array('world' => 'hell'));
extract
顾名思义,释放一个数组内的元素,构造模板引擎的时候尤为有用
$arr = array('foo' => 'bar');
extract($arr);
echo $foo; // output 'bar'
func_get_args
获取传递过来的参数
function test() {
$args = func_get_args();
echo $args[2]; // will print 'd' echo $args[1]; // will print 3}
test(1,3,'d',4);
cli
command line interface,可以在命令行里运行php脚本,用来处理日常任务也非常方便。对了别忘了一个非常有用的参数 -a,进入交互模式
php -a
Interactive php >
载入的文件可以有返回值
这个比.ini文件更加灵活,你可以在文件里运行一段程序后,返回运行结果
// config.phpreturn array(
'db' => array(
'host' => $_SERVER['HTTP_HOST'] == '127.0.0.1'?'localhost':'example.org',
'user' => 'usr',
// ... ),
// ...);
// index.php$config = include 'config.php';
echo $config['db']['host']; // example.org
在函数内定义静态变量
把类的特性移植到函数里了,比如有一些运算量较大的函数,可以把运算结果放到这个静态变量,下次调用时检查一下该变量是否已经被赋值。
function foo($arg1)
{
static $cache;
if( !isset($cache[md5($arg1)]) )
{
// Do the work here $cache[md5($arg1)] = $results;
}
return $cache[md5($arg1)];
}
heredoc
不用再为引号发愁了
echo <<