1--ini_set
ini_set()所指的当前脚本包含了调用关系,不是以文件为基础划分。
所以下边的设置是允许的
set_error.php
<?php
function setError(){
ini_set('display_errors','off');
echo 'set error done!';
}
error.php
<?php
require('set_error.php');
setError();
......
?>
2--可变变量
$string="_POST";
$$string......
在 PHP 的函数和类的方法中,超全局变量不能用作可变变量。$this 变量也是一个特殊变量,不能被动态引用
3--如何判断流文件类型
我最近采用下边的方式判断:下载文件,得到文件流->存储到硬盘->判断文件类型。
不过觉得这样显得很多余
$image=file_get_contents($url);
file_put_contents($imagePath, $image); //将图片流存入服务器图片目录
$type=image_type_to_extension(exif_imagetype($imagePath)); //文件类型
完美解决
文件流都是有头标识的,根据这个可以判断
$image = file_get_contents($url);
echo check_image_type($image);
function check_image_type($image)
{
$bits = array(
'JPEG' => "xFFxD8xFF",
'GIF' => "GIF",
'PNG' => "x89x50x4ex47x0dx0ax1ax0a",
'BMP' => 'BM',
);
foreach ($bits as $type => $bit) {
if (substr($image, 0, strlen($bit)) === $bit) {
return $type;
}
}
return 'UNKNOWN IMAGE TYPE';
}
4--php wanning :module 'XXXXXX' already loaded in unkonwn on line 0
现在有两种方式可以载入大多数的php拓展:
- compiling the extension directly into the PHP binary。
- by loading a shared extension dynamically via an ini file。
这个是因为两种方式重复载入了拓展
把php.ini的extension=XXXXXX.so
注释掉就可以了
tip
To see which extensions are compiled-in to your PHP binary。
php -m
查看各种信息,类似phpinfo()
php -i
5--如何对中文进行排序
utf-8并不是按照中文拼音顺序排序的,但是常见中文在gbk中是按照拼音排序的,所以需要编码转换
foreach ($array as $key=>$value)
{
$new_array[$key] = iconv('UTF-8', 'GBK', $value);
}
asort($new_array);
foreach ($new_array as $key=>$value)
{
$array[$key] = iconv('GBK', 'UTF-8', $value);
}
如果需要自定义排序,可以使用usort()
bool usort ( array &$array , callable $cmp_function )
cmp_function
在第一个参数小于,等于或大于第二个参数时,该比较函数必须相应地返回一个小于,等于或大于 0 的整数。
比如:
function cmp($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
6--你不知道的return语句
如果在一个函数中调用 return 语句,将立即结束此函数的执行并将它的参数作为函数的值返回。return 也会终止 eval() 语句或者脚本文件的执行。
如果在全局范围中调用,则当前脚本文件中止运行。如果当前脚本文件是被 include 的或者 require 的,则控制交回调用文件。此外,如果当前脚本是被 include 的,则 return 的值会被当作 include 调用的返回值。如果在主脚本文件中调用 return,则脚本中止运行。如果当前脚本文件是在 php.ini 中的配置选项 auto_prepend_file 或者 auto_append_file 所指定的,则此脚本文件中止运行
7--php的<=标记
<= $name ?>
<?php echo $name ?>
上边的两行代码是等价的。并且<=和等短标记不同,是符合一般规范的。
以前使用此缩写需要 short_open_tag 的值为 On。 从 PHP 5.4.0 起, <?= 总是可用的。
8--php的curl报错
module returned CKR_DEVICE_ERROR, indicating that a problem has occurred with the token or slot.
这是因为centos编译好的curl在https下默认使用的是nss,需要换成openssl
#先安装openssl
yum install openssl openssl-devel
#编译安装curl
./configure --prefix=/usr/local/curl --with-ssl=/usr/local/curl
运行,/usr/local/curl/bin/curl --version
curl 7.59.0 (x86_64-pc-linux-gnu) libcurl/7.59.0 OpenSSL/1.0.2k zlib/1.2.7
Release-Date: 2018-03-14
Protocols: dict file ftp ftps gopher http https imap imaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IPv6 Largefile NTLM NTLM_WB SSL libz UnixSockets HTTPS-proxy
最后编译安装php的时候制定--with-curl=/usr/local/curl
就OK啦
9--mysqlnd什么意思
mysqlnd是mysql native driver的缩写,这个library是php自己实现的,不是mysql官方的libmysql,性能更好,在php5.4后所有和mysql相关的extension默认使用的都是mysqlnd