php-fpm 进程优化 pm.start_servers 设置多少最优

细心的朋友在查看php-php日志的时候会发现有时会出现下面的警告:

WARNING: [pool www] seemsbusy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 8 children, thereare 0 idle, and 29 totalchildren

该信息是建议增加pm.start_servers数量,请注意下上面红色标注的[pool www], 不同配置可能这里会有差异,在具体修改的找到对应pool的配置文件即可;
首先找到所安装php-fpm的配置文件存放路径(注意:不同版本的php-fpm配置文件路径可能有细微的差别),我们可以通过phpinfo()打印查看。

pm.max_children = 50
pm.start_servers = 5 
pm.min_spare_servers = 10
pm.max_spare_servers = 20

Continue reading “php-fpm 进程优化 pm.start_servers 设置多少最优”

在PHP中发送原生 HTTP header头

通过header content-type设置页面编码为utf-8:

<?php
header('Content-Type: text/html; charset=utf-8');
?>

通过header location设置页面跳转:

<?php
header('Location: https://www.dustit.me/');
?>

页面缓存设置:

<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>

 

PHP删除数组中指定的元素

删除数组元素有不同的方法,可以根据具体的需要选择不同的方法。

1.unset() 方法

注意:当您使用unset()方法时,数组Key值不会更改。 如果要重新编号键,您可以在unset()之后使用array_values(),它将所有键转换为从0开始的数值枚举键。

<?php

    $array = array(0 => "a", 1 => "b", 2 => "c");
    unset($array[1]);
               //↑ 需要删除的键
?>

输出

Array (
    [0] => a
    [2] => c
)

2.array_splice() 方法

Continue reading “PHP删除数组中指定的元素”

PHP获取数组最后一个元素

一个解决方案是使用end和key的组合:

end()将数组的内部指针提升到最后一个元素,并返回其值。
key()返回当前数组位置的索引元素。
所以,这样的代码的一部分应该做的诀窍:

<?php
$array = array(
    'first' => 123,
    'second' => 456,
    'last' => 789, 
);

end($array);         // move the internal pointer to the end of the array
$key = key($array);  // fetches the key of the element pointed to by the internal pointer

var_dump($key);
?>

输出:

string 'last' (length=4)

另一种更简单的获取最后一个元素的方法:

<?php
echo $array[count($array) - 1];
?>