看了些PHP的基础知识,自己在这里总结下:
1,在HTML嵌入PHP脚本有三种办法:
2,PHP注释分单行和多行注释,和java注释方式相同。
<?
//
这里是单行注释
echo
"
test
"
;
/*
这里是多行注释!可以写很多行注释内容
*/
?>
注意不要有嵌套注释,如
/*aaaa/*asdfa*/asdfasdfas*/,这样的注释会出现问题。
3,PHP主要的数据类型有5种, integer,double,string,array,object。
4,函数内调用函数外部变量,需要先用global进行声明,否则无法访问,这是PHP与其他程序语言的一个区别。事例代码:
5,变量的变量,变量的函数
<?
//
变量的变量
$a
=
"
hello
"
;
$
$a
=
"
world
"
;
echo
"
$a $hello
"
;
//
将输出"hello world"
echo
"
$a ${$a}
"
;
//
同样将输出"hello world"
?>
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
<?
//
变量的函数
function
func_1(){
print
(
"
test
"
);
}
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
function
fun(
$callback
){
$callback
();
}
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
fun(
"
func_1
"
);
//
这样将输出"test"
?>
6,PHP同时支持标量数组和关联数组,可以使用list()和array()来创建数组,数组下标从0开始。如:
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,
"
;
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
foo1(
$str
);
echo
$str
;
//
output:This is a String,
echo
"
<br>
"
;
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
foo1(
&
$str
);
echo
$str
;
//
output:This is a String, and something extra
?>
8,函数默认值。PHP中函数支持设定默认值,与C++风格相同。
9,PHP的几个特殊符号意义。
$ 变量
& 变量的地址(加在变量前)
@ 不显示错误信息(加在变量前)
-> 类的方法或者属性
=> 数组的元素值
?: 三元运算子
10,include()语句与require()语句
如果要根据条件或循环包含文件,需要使用include().
require()语句只是被简单的包含一次,任何的条件语句或循环等对其无效。
由于include()是一个特殊的语句结构,因此若语句在一个语句块中,则必须把他包含在一个语句块中。
1,在HTML嵌入PHP脚本有三种办法:
<
script language
=
"
php
"
>
// 嵌入方式一
echo ( " test " );
</ script >
<?
// 嵌入方式二
echo " <br>test2 " ;
?>
<? php
// 嵌入方式三
echo " <br>test3 " ;
?>
还有一种嵌入方式,即使用和Asp相同的标记<%%>,但要修改PHP.ini 相关配置,不推荐使用。
// 嵌入方式一
echo ( " test " );
</ script >
<?
// 嵌入方式二
echo " <br>test2 " ;
?>
<? php
// 嵌入方式三
echo " <br>test3 " ;
?>
2,PHP注释分单行和多行注释,和java注释方式相同。
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
3,PHP主要的数据类型有5种, integer,double,string,array,object。
4,函数内调用函数外部变量,需要先用global进行声明,否则无法访问,这是PHP与其他程序语言的一个区别。事例代码:
<?
$a = 1 ;
function test(){
echo $a ;
}
test(); // 这里将不能输出结果“1”。
function test2(){
global $a ;
echo $a ;
}
test2(); // 这样可以输出结果“1”。
?>
注意:PHP可以在函数内部声明静态变量。用途同C语言中。
$a = 1 ;
function test(){
echo $a ;
}
test(); // 这里将不能输出结果“1”。
function test2(){
global $a ;
echo $a ;
}
test2(); // 这样可以输出结果“1”。
?>
5,变量的变量,变量的函数
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
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"
?>
$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,关联参数传递(&的使用),两种方法。例:
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
![](http://xuedong.iteye.com/Images/OutliningIndicators/None.gif)
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 ;
}
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 );
}
?>
// 下面为错误语句
if ( $condition )
include ( $file );
else
include ( $other );
// 下面为正确语句
if ( $condition ){
include ( $file );
} else
{
include ( $other );
}
?>
转自:http://www.phpweblog.net/yemoo/archive/2006/06/25/170.html