PHP函数学习

2019-05-24  学习

range,根据范围创建数组

range ( mixed $start, mixed $end, [, number $step = 1 ] ) : array

例:

foreach ( range(1,5,2) as $number )
{
   echo $number;
}

输出:

135


2019-05-27 学习

count,统计数组元素的个数或者对象里面的东西

count ( mixed array_or_countable [, int $mode = COUNT_NORMAL ] ) : int

例:

$a[0] = 1;
$a[1] = 4;
$a[2] = 11;
var_dump($a);

$b[0] = 7;
$b[5] = 11;
$b[11] = 14;
var_dump($b);

var_dump(count(null));
var_dump(count(false));

输出:

int(3)
int(3)
Warning: count(): Parameter must be an array or an object that implements Countable;
Warning: count(): Parameter must be an array or an object that implements Countable;

例:

$food = array('fruits' => array('orange', 'banana', 'apple'),
              'veggie' => array('carrot', 'collard', 'pea'));
var_dump(count($food, COUNT_RECURSIVE))
var_dump(count($food))

输出:

int(8)
int(2)

substr,返回字符串的字串

substr( string $string, int $start [, int $length ] ): string 

例:

$string = 'string';

$start 为正数  start------>尾来计算
substr($string, 0)   //string

$start 为负数 start------->头来计算
substr($start,-3)    //art

$length 为正数, start---->尾 截取length位
substr($string, 0, 3)   //str

$length 为负数, start----->头 省略length位
substr($string, 0, -3) //str

explode,将字符串分割成数组

explode(string $delimiter, string $string [, int $limit ] ) : array

例:

$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pices = explode("", $pizza);
echo $prices[0]; //piece1
echo $pices[1]; //piece2

$pizza  = "piece1:piece2:piece3:piece4:piece5:piece6";
list($a $b $c $e $f $d) = explode(":", $pizza);
echo $a;  //piece1
echo $d;    // piece6
$str = 'one|two|three|four';
// 正数的 limitprint_r(explode('|', $str, 2));
// 负数的 limit(自 PHP 5.1 起)print_r(explode('|', $str, -1));
Array
(
    [0] => one
    [1] => two|three|four
)
Array
(
    [0] => one
    [1] => two
    [2] => three
)