侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

php中5中常用优化写法对比

php  /  管理员 发布于 2年前   530

php中5中常用优化写法对比,希望对你有帮助


1.PHP删除数组中空值元素,可以直接用array_filter函数

foreach( $arr as $k=>$v){   
    if( !$v )   
        unset( $arr[$k] );   
}  
改
array_filter($arr)

2.foreach循环或数组函数(array_ *)可以处理空数组

$items = [];
// ...
if (count($items) > 0) {
   foreach ($items as $item) {
       // process on $item ...
   }
}
改成
$items = [];
// ...
foreach ($items as $item) {
   // process on $item ...
}

3.将方法的所有内容封装在if语句中

function foo(User $user) {
   if (!$user->isDisabled()) {
       // ...
       // long process
       // ...
   }
}
改
function foo(User $user) {
   if ($user->isDisabled()) {
       return;
   }

   // ...
   // long process
   // ...
}

4.我们经常需要检查是否已定义变量(而不是null)。在PHP中,我们可以使用isset函数来做到这一点。而且,魔术,它可以采用多个参数!

$a = null;
$b = null;
$c = null;
// ...

if (!isset($a) || !isset($b) || !isset($c)) {
   throw new Exception("undefined variable");
}
// or
if (isset($a) && isset($b) && isset($c) {
   // process with $a, $b et $c
}
// or
$items = [];
//...
if (isset($items['user']) && isset($items['user']['id']) {
   // process with $items['user']['id']
}
改
$a = null;
$b = null;
$c = null;
// ...

if (!isset($a, $b, $c)) {
   throw new Exception("undefined variable");
}
// or
if (isset($a, $b, $c)) {
   // process with $a, $b et $c
}
// or
$items = [];
//...
if (isset($items['user'], $items['user']['id'])) {
   // process with $items['user']['id']
}


5.结合使用echo方法和sprintf,我们可以简单地使用printf方法

$name = "John Doe";
echo sprintf('Bonjour %s', $name);
改
$name = "John Doe";
printf('Bonjour %s', $name);


6.in_array和array_keys的联合使用,可以使用array_key_exists替换。

$items = [
   'one_key' => 'John',
   'search_key' => 'Jane',
];

if (in_array('search_key', array_keys($items))) {
   // process
}
改
$items = [
   'one_key' => 'John',
   'search_key' => 'Jane',
];

if (array_key_exists('search_key', $items)) {
   // process
}

收藏一下啦



  • 上一条:
    今天给大家介绍一款网络流量监控工具iftop,让你实时监控你的服务器
    下一条:
    centos7下载安装包编译安装Swoole步骤流程
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在php语言中实现将中文字符串以每个字为元素形成数组(0个评论)
    • phpstudy手动安装php8.0+版本流程步骤(0个评论)
    • 在php语言中实现内容推荐算法简单示例及推荐算法原理介绍(0个评论)
    • 在php中如何实现redis分布式锁功能示例代码(0个评论)
    • 在php语言中使用笛卡尔积实现多种sku组合功能代码示例(0个评论)
    • 近期文章
    • Laravel 10.13版本发布(0个评论)
    • 在github创建task的同时创建分支流程步骤(0个评论)
    • 在go语言中以邮件标题中获取SPF和DMARC,来判断是否为垃圾邮件之垃圾邮件过滤器功能实现(0个评论)
    • 在go语言中使用attr字段标签提取XML属性数据示例(0个评论)
    • 在laravel中介绍一个生成假数据的PHP库:FakerPHP(0个评论)
    • 在laravel框架中对环境配置文件的加载过程步骤浅析(0个评论)
    • Laravel 10.12版本发布(0个评论)
    • 在go语言中如何记录每个HTTP请求到你的Web服务器、日志记录器?(0个评论)
    • 在Go语言中如何查找一个IP地址的网络地址?(0个评论)
    • ELK + Filebeat 搭建日志系统流程步骤(0个评论)
    • 近期评论
    • 博主 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 @ xiaoB 你只管努力,剩下的叫给天意;天若有情天亦老,..
    • xiaoB 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 会不会春节放假后又阳一次?..
    • BUG4 在

      你翻墙过吗?国内使用vpn翻墙可能会被网警抓,你需了解的事中评论 不是吧?..
    • 博主 在

      go语言+beego框架中获取get,post请求的所有参数中评论 @ t1  直接在router.go文件中配就ok..
    • Jade 在

      如何在MySQL查询中获得当月记录中评论 Dear zongscan.com team, We can skyroc..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-11
    • 2017-12
    • 2018-01
    • 2018-02
    • 2018-03
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-09
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客