1,在HTML嵌入PHP指令碼有三種辦法:
< script language = " php " >
// 嵌入方式一
echo ( " test " );
</ script >
<?
// 嵌入方式二
echo " <br>test2 " ;
?>
<? php
// 嵌入方式三
echo " <br>test3 " ;
?>
還有一種嵌入方式,即使用和Asp相同的標記<%%>,但要修改PHP.ini 相關配置,不推薦使用。
2,PHP注釋分單行和多行注釋,和java注釋方式相同。
<?
// 這裡是單行注釋
echo " test " ;
/*
這裡是多行注釋。可以寫很多行注釋內容
*/
?>
注意不要有嵌套注釋,如/*aaaa/*asdfa*/asdfasdfas*/,這樣的注釋會出現問題。
3,PHP主要的資料類型有5種,integer,double,string,array,object。
4,函數內調用函數外部變數,需要先用global進行聲明,否則無法訪問,這是PHP與其他程式語言的一個區別。案例代碼: 注意:PHP可以在函數內部聲明靜態變數。用途同C語言中。
5,變數的變數,變數的函數
<?
$a = 1 ;
function test(){
echo $a ;
}
test(); // 這裡將不能輸出結果“1”。
function test2(){
global $a ;
echo $a ;
}
test2(); // 這樣可以輸出結果“1”。
?>
<?
// 變數的變數
$a = " hello " ;
$ $a = " world " ;
echo " $a $hello " ; // 將輸出"hello world"
echo " $a ${$a} " ; // 同樣將輸出"hello world"
?>
<?
// 變數的函數
function func_1(){
print ( " test " );
}
function fun( $callback ){
$callback ();
}
fun( " func_1 " ); // 這樣將輸出"test"
?>
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"
/*
注意:當使用參數預設值時所有有預設值的參數應該在無預設值的參數的後邊定義。否則,程式將不會按照所想的工作。
*/