1,在HTML嵌入PHP腳本有三種辦法:
// 嵌入方式一
echo ( " test " );
</ script >
<?
// 嵌入方式二
echo ? " <br>test2 " ;
?>
<? php
// 嵌入方式三
echo ? " <br>test3 " ;
?>
2,PHP注釋分單行和多行注釋?zhuān)蚸ava注釋方式相同。
// 這里是單行注釋
echo ? " test " ;
/*
這里是多行注釋?zhuān)】梢詫?xiě)很多行注釋內(nèi)容
*/
?>
3,PHP主要的數(shù)據(jù)類(lèi)型有5種, integer,double,string,array,object。
4,函數(shù)內(nèi)調(diào)用函數(shù)外部變量,需要先用global進(jìn)行聲明,否則無(wú)法訪問(wèn),這是PHP與其他程序語(yǔ)言的一個(gè)區(qū)別。事例代碼:
$a = 1 ;
function ?test(){
echo ? $a ;
}
test();???? // 這里將不能輸出結(jié)果“1”。
function ?test2(){
???? global ? $a ;
???? echo ? $a ;
}
test2();???? // 這樣可以輸出結(jié)果“1”。
?>
5,變量的變量,變量的函數(shù)
// 變量的變量
$a = " hello " ;
$ $a = " world " ;
echo ? " $a?$hello " ;???? // 將輸出"hello?world"
echo ? " $a?${$a} " ;???? // 同樣將輸出"hello?world"
?>
<?
// 變量的函數(shù)
function ?func_1(){
?? print ( " test " );
}
function ?fun( $callback ){
?? $callback ();
}
fun( " func_1 " );???? // 這樣將輸出"test"
?>
6,PHP同時(shí)支持標(biāo)量數(shù)組和關(guān)聯(lián)數(shù)組,可以使用list()和array()來(lái)創(chuàng)建數(shù)組,數(shù)組下標(biāo)從0開(kāi)始。如:
$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,關(guān)聯(lián)參數(shù)傳遞(&的使用),兩種方法。例:
// 方法一:
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,函數(shù)默認(rèn)值。PHP中函數(shù)支持設(shè)定默認(rèn)值,與C++風(fēng)格相同。
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"
/*
注意:當(dāng)使用參數(shù)默認(rèn)值時(shí)所有有默認(rèn)值的參數(shù)應(yīng)該在無(wú)默認(rèn)值的參數(shù)的后邊定義。否則,程序?qū)⒉粫?huì)按照所想的工作。
*/
function ?test( $type = " test " , $ff ){???? // 錯(cuò)誤示例
?? return ? $type . $ff ;
}
9,PHP的幾個(gè)特殊符號(hào)意義。
???$??????變量
? &??????變量的地址(加在變量前)
?@??????不顯示錯(cuò)誤信息(加在變量前)
?->??????類(lèi)的方法或者屬性
=>??????數(shù)組的元素值
?:?????????三元運(yùn)算子
10,include()語(yǔ)句與require()語(yǔ)句
??? 如果要根據(jù)條件或循環(huán)包含文件,需要使用include().
??? require()語(yǔ)句只是被簡(jiǎn)單的包含一次,任何的條件語(yǔ)句或循環(huán)等對(duì)其無(wú)效。
?? 由于include()是一個(gè)特殊的語(yǔ)句結(jié)構(gòu),因此若語(yǔ)句在一個(gè)語(yǔ)句塊中,則必須把他包含在一個(gè)語(yǔ)句塊中。
???
// 下面為錯(cuò)誤語(yǔ)句
if ( $condition )
???? include ( $file );
else
???? include ( $other );
// 下面為正確語(yǔ)句????
if ( $condition ){
?? include ( $file );
} else
{
?? include ( $other );
}
?>
?
?
轉(zhuǎn)自: http://www.phpweblog.net/yemoo/archive/2006/06/25/170.html
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
