PHP基礎知識總結____PHP

來源:互聯網
上載者:User

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"
/*
注意:當使用參數預設值時所有有預設值的參數應該在無預設值的參數的後邊定義。否則,程式將不會按照所想的工作。
*/

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.