**
数组部分
**
循环输出
$myarray = array(
'first' => 'bob',
'second' => 'lisa',
'third' => 'john'
);
method 1:
foreach($myarray as $key => $value){
echo $key." is ".$value;
}
method 2:
while(list($key,$value) = each($myarray)){
echo $key." is ".$value;
}
拆分数组
array explode(string separator,string string);
string implode(string glue,array pieces);
元素数目
count($array);
查询数组中指定元素
mixed array_search (mixed needle,array haystack)
//在haystack中搜索needle并返回其键值,否则false
获取最后一个元素
mixed array_pop(array array);
//返回并获取最后一个元素,数组长度减一,数组为空则返回null
向数组中添加元素
int array_push(array array,mixed var [,mixed ..]);
//放入数组的尾部,返回数组新的元素总数
删除数组中的重复元素
array array_unique(array array);
**
函数部分:
**
函数无需再调用之前被定义,除非是有条件的函数,或者是函数中的函数。
$condition = true;
if($condition == true){
function funa(){}
}
funa();
function out(){
function inner(){}
}
out();
inner();
不支持函数重载。
函数名大小写无关。
参数类型(按值传递,引用传递,默认参数)
function funa($value){}
function funb(&$value){}
function func($value,$type = 'array'){}//默认参数在右
接收指定类型的参数
function funa(int $a, string $b,array $c,bool $d){}
//适用于继承和接口
function funb(My_class $a,array b){}
//可以接收My_class的子类或是实现其接口的其他类,若只想接收My_class此类的参数,则可以用final或者在函数内容中判断。注意build-in class不能当做参数的类名出现,如string,boolean等
strict typing
declare(strict_type = 1)
function sum(int $a,int $b){
return $a + $b;
}
sum(1,2);
sum(1.1,1.4);//wrong
weak typing
function sum(int $a,int $b){
return $a + $b;
}
sum(1,2);
sum(1.1,1.4);//right return 2
可变数量的参数列表
function sum(int ...$num){
$result = 0;
foreach($num as $value){
$result += $value;
}
return $result;
}
//...能够用来unpack an array or traversable variable or literal into the argument list
function add($a,$b){}
add(...[1,2]);
三个函数内常用的函数
int func_num_args()
//返回传参的个数
mixed func_get_arg(int $arg_num)
//返回参数列表的某一项(0,1...)
array func_get_args()
//返回参数列表数组(相应元素的副本)
函数的返回值
return
返回多个值的时候
function myfunc(){
return array(0,1,2);
}
list($a,$b,$c) = myfunc();
返回一个引用,函数声明和指派返回值给变量都要用&
function &myfunc(){
return $value;
}
$newref = &myfunc();
指定返回值的类型
function sum($a,$b):float{}
//在strict typing的情况下
declare(strict_types = 1);
function sum($a,$b):int{
return $a + $b;
}
var_dump(sum(1,2.5));//error
函数中的参数使用,一般都是对其副本的操作,也就是值传递,但参数中的类是引用传递。若想对参数自身做操作,就使用引用。
function &myfunc(&$value){
$value += 1;
return $value;
}
$num = 1;
$result = &myfunc($num);//2,2
可变函数
function foo(){};
$name = 'foo';
$name();
静态方法及变量的使用
class Foo{
static $value = 'value';
static function variable(){}
}
echo Foo::$value;
$variable = 'variable';
Foo::$variable();
匿名函数
$greet = function($name){
echo $name;
}
$greet('hello world');
//函数中用到的变量要再函数定义时声明,并且传参一般为传值,除非引用或对象。
$massage = 'hello';
$example = function () use ($massage){
var_dump($massage);
};
echo $example();
针对不同情况的例子
//改变对象的属性 :nooo
//传递的时T对象,$objec一直指向它,并对name做了修改
class T{
public $name = "hi";
}
$objec = new T();
$callback = function () use ($objec) {
print "This is a closure use object, msg is: {$objec->name}. <br />/n";
};
$objec->name = "nooo";
$callback();
//$objec指向不同的对象:hi
//$objec只是T对象的名字,传递的是T对象,后面$objec又指向新的对象N
class T{
public $name = "hi";
}
class N{
public $name = "hello";
}
$objec = new T();
$callback = function () use ($objec) {
print "This is a closure use object, msg is: {$objec->name}. <br />/n";
};
$objec = new N();
$callback();
//$obj为对象的名字,闭包产生的是对hello,everyone对象的引用,只是名字代表的实体变了:everyone,by:http://blog.csdn.net/lgg201/article/details/6127564
$obj = (object) "Hello, everyone";
$callback = function () use ($obj) {
print "This is a closure use object lazy bind, msg is: {$obj->scalar}. <br />/n";
};
$obj = (object) "Hello, everybody";
//counter1和counter2是分别调用了函数,等于调用了两次,分别计数。
function counter() {
$counter = 1;
return function() use(&$counter) {return $counter ++;};
}
$counter1 = counter();
$counter2 = counter();