PHP笔记

本文环境 PHP7.3不懂的可以评论或?

?系我邮箱:owen@owenzhang.com, 系我邮箱:owen@owenzhang.com

著作权归OwenZhang所有。商业转载请联系OwenZhang获得授权,非商业转载请注明出处。

隐藏index.php

//laravel
location / {
try_files $uri $uri/ /index.php?$query_string;
}
//tp
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
}
}

小数

保留两位小数不进行四舍五入

$num=3.149;
$new_num=floor($num*100)/100;
echo $new_num;//结果3.14

四书五入保留2位小数,保留0

echo number_format(5.228,2);    //  5.23
echo number_format(5.224,2);    //  5.22
echo number_format(5,2);        //  5.00

四书五入保留2位小数,不保留0

echo sprintf(“%.1f”, floatval(5.201)); // 5.2

字符

某字符截取

$rest = substr("abcdef", 2);  // 返回 "cdef"
$rest = substr("abcdef", 2, -1);  // 返回 "cde"

是否存在某字符

strpos($value, ‘.ts’) !== false

字符串替换

string1内a替换为b

 $string2     = str_replace('a', 'b', $string1);

过滤字符串只保留字母、数字

function getFilterStr($str)
{
    preg_match_all('/[a-zA-Z0-9]/u', $str, $result);
    return implode('', $result[0]);
}

字母转大小写

strtolower() 函数把字符串转换为小写。

strtoupper() 函数把字符串转换为大写。

laravel 框架打印完整sql语句

DB::connection()->enableQueryLog();
 User::query()->where(['id' => 1])->first();
 dd( DB::getQueryLog());

PHP代码加密

https://github.com/pk-fr/yakpro-po
由于opcode可反编译的问题,要想做到真的源码保护,100%无法还原,可用的方案目前有3个:

kphp\
https://github.com/VKCOM/kphp

peachpie\
https://github.com/peachpiecompiler/peachpie

BPC\
https://bpc.dev/

kphp把PHP编译成C++,不只能保护源码,还能大幅提升性能,但使用上有 一些限制, 比如不能使用dynamic function call.\

peachpie把PHP编译成C#,同样不只能保护源码,也能提升性能,但是引入了.NET,如果你你喜欢.NET,那正合适.不过从它的issue看,问题不少.\

BPC最终把PHP编译成C,同样不只能保护源码,还内置软件授权机制,但BPC不以性能为目标.\
BPC是我们公司的内部项目,上周刚release了6.0版本,并且成功编译了workerman手册里的一些简单的例子.\
你可以从 github release 页面下载回来这些编译好的例子试一下,它们都是静态编译的,放到ubuntu 18.04的机器上就能直接运行.\
当然我在本站发帖后,有网友说不开源不能用,这就是个人选择了. Swoole Compiler和ionCube也都不开源的.\

try{}catch{} 异常捕获

异常最好catch (\Throwable $e)不要用catch(\Exception $e)。 catch \Throwable 可以捕获任何异常和Error错误,catch \Exception 只能捕获异常,不能捕获Error

json_encode实现中文不转码

为json_encode()填入第二个参数:JSON_UNESCAPED_UNICODE,可以实现中文不转码

Buy me a cup of coffee 🙂

觉得对你有帮助,就给我打赏吧,谢谢!

微信赞赏码链接,点击跳转:

PHP笔记插图

Tags: