php的做为web编程语应用是非常广的,做为一个php程序员要知道并总结一些应用写法方式以方便日常的开发。
1、自动判断当前http协议类型,这个应用还是我在支付宝sdk中发现的,是一个不错的写法。
$protocol = (!empty($_server['https']) && $_server['https'] !== 'off' || $_server['server_port'] == 443) ? "https://" : "http://";
2、php判断浏览器终端pc、平板、手机的实现方法,经实测,非常好用和准备,可以在后端实现根据不同的终端调用不同的模块处理。
mobile_detect项目地址:https://github.com/serbanghita/mobile-detect/
require_once 'mobile_detect.php';
$detect = new mobile_detect;
$devicetype = ($detect->ismobile() ? ($detect->istablet() ? 'tablet' :'phone') :'computer');
3、php通过curl上传文件,特别注意的是下面的file路径,如果在windows环境下,在realpath后的路径前面加上@字符是可以正常发送的。如果在linux中是不能正常发送的。正确的写法是curl_file_create($fileurl)
$fileurl = realpath($fileobj->getrealfile());//获取文件的实际物理路径
$post_data = array(
'token' => '88e780d49ff812c644c89ff31e5de196',
"file" => curl_file_create($fileurl),
);
4、php生成二维码,使用的是phpqrcode。
5、php浮点数精度丢失问题解决方案。原则:在进行浮点数计算的时候一定要对浮点数进行格式化!再代入进行计算处理。
var_dump(intval(sprintf("%.2f", $f*100)));//string '58.00'//int 58
var_dump(intval(round($f*100, 1)));//float 58//int 58
var_dump(intval(number_format($f*100,2)));//string '58.00'//int 58
6、根据ip定位用户所在城市信息
使用第三方ip库来获取内容:http://www.ipip.net/download.html
7、随机排列数组。shuffle()函数把数组中的元素按随机顺序重新排列。常在数据库中查出数据后,用这个函数处理一下就可以生成随机排列的新数组
8、php获取上传文件大小函数
/**
* 文件大小格式化
*@param type $filename 文件路径
*/
function getfilesize($filename) {
$filename = $_server['document_root'] . . $filename;
$size=filesize($filename);
$mod = 1024;
$units = explode(' ','b kb mb gb tb pb');
for ($i = 0; $size > $mod; $i++) {
$size /= $mod;
}
return round($size, 2) . ' ' . $units[$i];
}
出色的企业网站建设应当具有什么特点提升网站转化率的八种优化技巧百度排名浮动情况该如何解决?软件开发专用服务器托管的好处SEO优化怎么做有助于提升网站排名?怎么样才可以不花钱做好网络推广呢?网站建设是什么?企业建设网站有什么用?网站内部seo优化排名的要点