|
6,PHP同时支持标量数组和关联数组,可以使用list()和array()来创建数组,数组下标从0开始。如: <? $a[0]="abc"; $a[1]="def"; $b["foo"]=13; $a[]="hello"; //$a[2]="hello" $a[]="world"; //$a[3]="world" $name[]="jill"; //$name[0]="jill" $name[]="jack"; //$name[1]="jack" ?> 7,关联参数传递(&的使用),两种方法。例: <? //方法一: function foo(&$bar){ $bar.=" and something extra"; } $str="This is a String,"; foo($str); echo $str; //output:This is a String, and something extra echo "<br>"; //方法二: function foo1($bar){ $bar.=" and something extra"; } $str="This is a String,"; foo1($str); echo $str; //output:This is a String, echo "<br>"; foo1(&$str); echo $str; //output:This is a String, and something extra ?> 8,函数默认值。PHP中函数支持设定默认值,与C++风格相同。 <? function makecoffee($type="coffee"){ echo "making a cup of $type.\n"; } echo makecoffee(); //"making a cup of coffee" echo makecoffee("espresso"); //"making a cup of espresso" /* 注意:当使用参数默认值时所有有默认值的参数应该在无默认值的参数的后边定义。否则,程序将不会按照所想的工作。 */ function test($type="test",$ff){ //错误示例 return $type.$ff; } 9,PHP的几个特殊符号意义。 $ 变量 & 变量的地址(加在变量前) @ 不显示错误信息(加在变量前) -> 类的方法或者属性 => 数组的元素值 ?: 三元运算子 10,include()语句与require()语句 如果要根据条件或循环包含文件,需要使用include(). require()语句只是被简单的包含一次,任何的条件语句或循环等对其无效。 由于include()是一个特殊的语句结构,因此若语句在一个语句块中,则必须把他包含在一个语句块中。 <? //下面为错误语句 if($condition) include($file); else include($other); //下面为正确语句 if($condition){ include($file); }else { include($other); } ?> 上一页 [1] [2] |