蒋金阳 发表于 2009-6-13 02:09:23

PHP基础知识总结

1,在HTML嵌入PHP脚本有三种办法:<br />&lt;script language=&quot;php&quot;&gt;<br />//嵌入方式一<br />echo(&quot;test&quot;);<br />&lt;/script&gt;<br />&lt;?<br />//嵌入方式二<br />echo &quot;&lt;br&gt;test2&quot;;<br />?&gt;<br />&lt;?php<br />//嵌入方式三<br />echo &quot;&lt;br&gt;test3&quot;;<br />?&gt;<br />还有一种嵌入方式,即使用和Asp相同的标记&lt;%%&gt;,但要修改PHP.ini 相关配置,不推荐使用。<br />2,PHP注释分单行和多行注释,和java注释方式相同。<br />&lt;?<br />//这里是单行注释<br />echo &quot;test&quot;;<br />/*<br />这里是多行注释!可以写很多行注释内容<br />*/<br />?&gt;<br />注意不要有嵌套注释,如/*aaaa/*asdfa*/asdfasdfas*/,这样的注释会出现问题。<br />3,PHP主要的数据类型有5种,integer,double,string,array,object。<br />4,函数内调用函数外部变量,需要先用global进行声明,否则无法访问,这是PHP与其他程序语言的一个区别。事例代码: <br />&lt;?<br />$a=1;<br />function test(){<br />echo $a;<br />}<br />test(); //这里将不能输出结果“1”。<br />function test2(){<br />global $a;<br />echo $a;<br />}<br />test2(); //这样可以输出结果“1”。<br />?&gt;<br />注意:PHP可以在函数内部声明静态变量。用途同C语言中。<br />5,变量的变量,变量的函数<br />&lt;?<br />//变量的变量<br />$a=&quot;hello&quot;;<br />$$a=&quot;world&quot;;<br />echo &quot;$a $hello&quot;; //将输出&quot;hello world&quot;<br />echo &quot;$a ${$a}&quot;; //同样将输出&quot;hello world&quot;<br />?&gt;<br />&lt;?<br />//变量的函数<br />function func_1(){<br />print(&quot;test&quot;);<br />}<br />function fun($callback){<br />$callback();<br />}<br />fun(&quot;func_1&quot;); //这样将输出&quot;test&quot;<br />?&gt;<br />6,PHP同时支持标量数组和关联数组,可以使用list()和array()来创建数组,数组下标从0开始。如:<br />&lt;?<br />$a=&quot;abc&quot;;<br />$a=&quot;def&quot;;<br />$b[&quot;foo&quot;]=13;<br />$a[]=&quot;hello&quot;; //$a=&quot;hello&quot;<br />$a[]=&quot;world&quot;; //$a=&quot;world&quot;<br />$name[]=&quot;jill&quot;; //$name=&quot;jill&quot;<br />$name[]=&quot;jack&quot;; //$name=&quot;jack&quot;<br />?&gt;<br />7,关联参数传递(&amp;的使用),两种方法。例:<br />&lt;?<br />//方法一:<br />function foo(&amp;$bar){<br />$bar.=&quot; and something extra&quot;;<br />}<br />$str=&quot;This is a String,&quot;;<br />foo($str);<br />echo $str; //output:This is a String, and something extra<br />echo &quot;&lt;br&gt;&quot;;<br />//方法二:<br />function foo1($bar){<br />$bar.=&quot; and something extra&quot;;<br />}<br />$str=&quot;This is a String,&quot;;<br />foo1($str);<br />echo $str; //output:This is a String,<br />echo &quot;&lt;br&gt;&quot;;<br />foo1(&amp;$str);<br />echo $str; //output:This is a String, and something extra<br />?&gt;<br />8,函数默认值。PHP中函数支持设定默认值,与C++风格相同。<br />&lt;?<br />function makecoffee($type=&quot;coffee&quot;){<br />echo &quot;making a cup of $type.\n&quot;;<br />}<br />echo makecoffee(); //&quot;making a cup of coffee&quot;<br />echo makecoffee(&quot;espresso&quot;); //&quot;making a cup of espresso&quot;<br />/*<br />注意:当使用参数默认值时所有有默认值的参数应该在无默认值的参数的后边定义。否则,程序将不会按照所想的工作。<br />*/<br />function test($type=&quot;test&quot;,$ff){ //错误示例<br />return $type.$ff;<br />}<br />9,PHP的几个特殊符号意义。<br />$ 变量<br />&amp; 变量的地址(加在变量前)<br />@ 不显示错误信息(加在变量前)<br />-&gt; 类的方法或者属性<br />=&gt; 数组的元素值<br />?: 三元运算子<br />10,include()语句与require()语句<br />如果要根据条件或循环包含文件,需要使用include().<br />require()语句只是被简单的包含一次,任何的条件语句或循环等对其无效。<br />由于include()是一个特殊的语句结构,因此若语句在一个语句块中,则必须把他包含在一个语句块中。<br />&lt;?<br />//下面为错误语句<br />if($condition)<br />include($file);<br />else<br />include($other);<br />//下面为正确语句 <br />if($condition){<br />include($file);<br />}else<br />{<br />include($other);<br />}<br />?&gt;<br />回复:芯儿 <br />------------------------------------------------ <br />1,PHP同时支持标量数组和关联数组。 <br />解释: 标量数组,即以数字为下标的数组,如$a,$a,$a等。 <br />关联数组,即用某个关键字来关联数组值,如$a[&quot;first&quot;]=&quot;ccc&quot;,$a[&quot;second&quot;]=&quot;dddd&quot;,访问时下标不用1,2 等数组索引,而应该用关联索引,如访问数组first的值时用$a[&quot;first&quot;]. <br />2,可以使用list()和array()来创建数组 <br />解释,这只是php创建数组的两种方式。 <br />A,使用array()创建数组方法:$arr=array(&quot;aaa&quot;,&quot;bbb&quot;,&quot;ccc&quot;);或$arr=array(&quot;first&quot;=&gt;&quot;aaa&quot;,&quot;second&quot;=&gt;&quot;bbb&quot;,&quot;third&quot;=&gt;&quot;ccc&quot;). <br />B,使用list创建数组是一种不严格的说法(偶的错误),准确说,使用list()可以把数组中的值赋给一些变量。 <br />如:$info=array(&quot;coffee&quot;,&quot;brown&quot;,&quot;caffeine&quot;); <br />list($drink,$color,$power)=$info; <br />echo &quot;$drink的颜色为$color&quot;; <br />返回:coffee 的颜色为brown <br />(注意:list()仅能用于数字索引的数组并假定数字索引从0开始) <br />3,数组下标从0开始。 <br />解释:这句话应该不用解释了,在js,java,c等语言都是这样规定的。<br /><br /><blockquote class="blockquote">From: http://www.ia56.com/read-htm-tid-234.htmlPowered by PHPWind.com</blockquote>
页: [1]
查看完整版本: PHP基础知识总结