<?php
//参数默认值
function abc($a,$b,$c = 0){
echo $a,$b,$c;
}
abc(1,3);
结果为:130;
echo "<br>";
整理代码,pre使一行的代码分成一行行的,方便查看;
echo "<pre>";
function def(){
$arr = func_get_args();
var_dump($arr);
}
def(1,2,3,4,5);
数组定义
两种定义方法
$arr1 = [];
$arr2 = array();
索引数组-
$arr1 = [1,2,3,"abc",1,2,3];
关联数组,最后一个不加逗号。one是key,1是value;
$arr2 = [
"one" => 1,
"two" => 2,
"three" => 3
];
echo $arr2["one"];
数组方法
弹出来东西
array_pop($arr2);
添加元素,返回索引值
array_push($arr2,"xyz");
echo "<pre>";
var_dump($arr2);
echo "<pre>";
长度
count($arr2);
echo count($arr2);
判断数组是否有某个值
in_array("1",$arr2);
var_dump(in_array("1111",$arr2));
合并数组
$arr3 = array_merge($arr1,$arr2);
var_dump($arr3);
数组反转
$arr4 = array_reverse($arr2);
var_dump($arr4);数组去重$arr5 = array_unique($arr1);var_dump($arr5);删除数组中的某个元素unset($arr1[0]);var_dump($arr1);遍历数组$arr1 = [1,2,3,4,5,6,7];$arr2 = [ "one" => 1, "two" => 2, "three" =>3, "four" =>4];for循环for($i = 0;$i < count($arr1);$i++){ var_dump($arr1[$i]);}foreach遍历,此处value为命名,可更改。foreach ($arr2 as $value){ var_dump($value);}此处key和value为命名,可更改,下面做出相应更改即可。foreach($arr2 as $key=>$value){ echo $key; echo "-----"; echo $value; echo "<br>";}指针遍历echo "<pre>";var_dump(each($arr2));
转载于:https://www.cnblogs.com/GP1014336455/p/7675841.html