菜鳥學習:動態網頁PHP基礎學習筆記

來源:互聯網
上載者:User

1、  PHP片段四種表示形式。

標準tags:<?php   ?>

short tags:<?      ?> 需要在php.ini中設定short _open_tag=on,預設是on

asp tags: <%     %>需要在php.ini中設定asp_tags=on,預設是off

script tags:<script language=”php”></script>

2、  PHP變數及資料類型

1)$variable  ,變數以字母、_開始,不能有空格

2)賦值$variable=value;

3)弱類型,直接賦值,不需要顯示聲明資料類型

4)基礎資料型別 (Elementary Data Type):Integer,Double,String,Boolean,Object(對象或類),Array(數組)  

5)特殊資料類型:Resourse(對第三方資源(如資料庫)的引用),Null(空,未初始化的變數)

3、  操作符

1)賦值操作符:=

2)算術操作符:+,-,*,/,%(模數)

3)串連操作符:. ,無論運算元是什麼,都當成String,結果返回String

4)Combined Assignment Operators合計賦值操作符:+=,*=,/=,-=,%=,.=

5)Automatically Incrementing and Decrementing自動增減操作符:

(1)$variable+=1 <=>$variable++;$variable-=1 <=>$variable-,跟c語言一樣,先做其他動作,後++或-

(2)++$variable,-$variable,先++或-,再做其他動作

6)比較操作符:= =(左邊等於右邊),!=(左邊不等於右邊),= = =(左邊等於右邊,且資料類型相同),>=,>,<,<=

7)邏輯操作符:|| ó or,&&óand,xor(當左右兩邊有且只有一個是true,返回true),!

4、  注釋:

單行注釋:// ,#

多行注釋:/*  */

5、  每個語句以;號結尾,與java相同

6、  定義常量:define(“CONSTANS_NAME”,value)

7、  列印語句:print,與c語言相同

8、  流程式控制制語句

1)if語句:

(1)if(expression)

{

    //code to excute if expression evaluates to true

}

(2)if(expression)

      {

      }

     else

      {

      }

(3)if(expression1)

   {

}

elseif(expression2)

{

}

else

{

}

2)swich語句

switch ( expression )

{

     case result1:

         // execute this if expression results in result1

         break;

     case result2:

        // execute this if expression results in result2

        break;

     default:

       // execute this if no break statement

       // has been encountered hitherto

}

3)?操作符:

 ( expression )?returned_if_expression_is_true:returned_if_expression_is_false;
 

4)while語句:

(1) while ( expression )
{
      // do something
}
(2)do

  {

   // code to be executed

} while ( expression );

5)for語句:

    for ( initialization expression; test expression; modification expression ) {

   // code to be executed

}

6)break;continue

9、  編寫函數

1)定義函數:

function function_name($argument1,$argument2,……) //形參

{

   //function code here;

}

2)函數調用

function_name($argument1,$argument2,……); //形參

3)動態函數調用(Dynamic Function Calls):

  1: <html>

  2: <head>

  3: <title>Listing 6.5</title>

  4: </head>

  5: <body>

  6: <?php

  7: function sayHello() {   //定義函數sayHello

  8:     print "hello<br>";

  9: }

 10: $function_holder = "sayHello";  //將函數名賦值給變數$function_holder

 11: $function_holder();  //變數$function_holder成為函數sayHello的引用,調用$function_holder()相當於調用sayHello

 12: ?>

 13: </body>

 14: </html>

4)變數範圍:

全域變數:

  1: <html>

  2: <head>

  3: <title>Listing 6.8</title>

  4: </head>

  5: <body>

  6: <?php

  7: $life=42;

  8: function meaningOfLife() {

9: global $life; 

/*在此處重新聲明$life為全域變數,在函數內部訪問全域變數必須這樣,如果在函數內改變變數的值,將在所有程式碼片段改變*/

 10:      print "The meaning of life is $life<br>";

 11: }

 12: meaningOfLife();

 13: ?>

 14: </body>

 15: </html>

5)使用static

  1: <html>

  2: <head>

  3: <title>Listing 6.10</title>

  4: </head>

  5: <body>

  6: <?php

  7: function numberedHeading( $txt ) {

  8:      static $num_of_calls = 0;

  9:      $num_of_calls++;

 10:      print "<h1>$num_of_calls. $txt</h1>";

 11: }

 12: numberedHeading("Widgets");  //第一次調用時,列印$num_of_calls值為1

 13: print("We build a fine range of widgets<p>"); 

 14: numberedHeading("Doodads");  /*第一次調用時,列印$num_of_calls值為2,因為變數是static型的,static型是常駐記憶體的*/

 15: print("Finest in the world<p>");

 16: ?>

 17: </body>

 18: </html>

6) 傳值(value)和傳址(reference):

傳值:function function_name($argument)

  1: <html>

  2: <head>

  3: <title>Listing 6.13</title>

  4: </head>

  5: <body>

  6: <?php

  7: function addFive( $num ) {

  8:      $num += 5;

  9: }

 10: $orignum = 10;

 11: addFive( &$orignum );

 12: print( $orignum );

 13: ?>

 14: </body>

 15: </html>

結果:10

傳址:funciton function_name(&$argument)

  1: <html>

  2: <head>

  3: <title>Listing 6.14</title>

  4: </head>

  5: <body>

  6: <?php

  7: function addFive( &$num ) {

  8:      $num += 5;  /*傳遞過來的是變數$num的引用,因此改變形參$num的值就是真正改變變數$orignum實體記憶體中儲存的值*/

  9: }

 10: $orignum = 10;

 11: addFive( $orignum );

 12: print( $orignum );

 13: ?>

 14: </body>

 15: </html>

結果:15

7)建立匿名函數:create_function(‘string1’,’string2’); create_function是PHP內建函數,專門用於建立匿名函數,接受兩個string型參數,第一個是參數列表,第二個是函數的主體

  1: <html>

  2: <head>

  3: <title>Listing 6.15</title>

  4: </head>

  5: <body>

  6: <?php

  7: $my_anon = create_function( '$a, $b', 'return $a+$b;' );

  8: print $my_anon( 3, 9 );

  9: // prints 12

 10: ?>

 11: </body>

 12: </html>

8)判斷函數是否存在:function_exists(function_name),參數為函數名

10、用PHP串連MySQL

1)串連:&conn=mysql_connect("localhost", "joeuser", "somepass");

2)關閉串連:mysql_close($conn);

3) 資料庫與串連建立聯絡:mysql_select_db(database name, connection index);

4) 將SQL語句給MySQL執行:$result = mysql_query($sql, $conn); //增刪改查都是這句

5) 檢索資料:返回記錄數:$number_of_rows = mysql_num_rows($result);

   將記錄放入數組:$newArray = mysql_fetch_array($result);

   例子:

  1: <?php
  2: // open the connection
  3: $conn = mysql_connect("localhost", "joeuser", "somepass");
  4: // pick the database to use
  5: mysql_select_db("testDB",$conn);
  6: // create the SQL statement
  7: $sql = "SELECT * FROM testTable";
  8: // execute the SQL statement
  9: $result = mysql_query($sql, $conn) or die(mysql_error());
 10: //go through each row in the result set and display data
 11: while ($newArray = mysql_fetch_array($result)) {
 12:     // give a name to the fields
 13:     $id = $newArray['id'];
 14:     $testField = $newArray['testField'];
 15:     //echo the results onscreen
 16:     echo "The ID is $id and the text is $testField <br>";
 17: }
 18: ?>

11、接受表單元素:$_POST[表單元素名],

如<input type=text  name=user>ó$_POST[user]

接受url中queryString中值(GET方式):$_GET[queryString]

12、轉向其他頁面:header("Location: http://www.webjx.com");

13、字串操作:

1)explode(“-”,str)óJava中的splite

2)str_replace($str1,$str2,$str3) =>$str1要尋找的字串,$str2用來替換的字串,$str3從這個字串開始尋找替換

3)substr_replace:

14、session:

1)開啟session:session_start(); //也可以在php.ini設定session_auto_start=1,不必再每個script都寫這句,但是預設為0,則必須要寫。

2)給session賦值:$_SESSION[session_variable_name]=$variable;

3)訪問session:$variable =$_SESSION[session_variable_name];

4)銷毀session:session_destroy();

15、顯示分類的完整例子:

1: <?php

  2: //connect to database

  3: $conn = mysql_connect("localhost", "joeuser", "somepass")

  4:     or die(mysql_error());

  5: mysql_select_db("testDB",$conn) or die(mysql_error());

  6:

  7: $display_block = "<h1>My Categories</h1>

  8: <P>Select a category to see its items.</p>";

  9:

 10: //show categories first

 11: $get_cats = "select id, cat_title, cat_desc from

 12:     store_categories order by cat_title";

 13: $get_cats_res = mysql_query($get_cats) or die(mysql_error());

 14:

 15: if (mysql_num_rows($get_cats_res) < 1) { //如果返回記錄行數小於1,則說明沒有分類

 16:    $display_block = "<P><em>Sorry, no categories to browse.</em></p>";

 17: } else {

 18:

 19:    while ($cats = mysql_fetch_array($get_cats_res)) { //將記錄放入變數$cats中

 20:$cat_id = $cats[id];

 21:$cat_title = strtoupper(stripslashes($cats[cat_title]));

 22:$cat_desc = stripslashes($cats[cat_desc]);

 23:

 24: $display_block .= "<p><strong><a

 25: href=\"$_SERVER[PHP_SELF][U1] ?cat_id=$cat_id\">$cat_title</a></strong>//點擊此url,重新整理本頁,第28行讀取cat_id,顯示相應分類的條目

 26: <br>$cat_desc</p>";

 27:

 28:if ($_GET[cat_id] == $cat_id) { //選擇一個分類,看下面的條目

 29:    //get items

 30:    $get_items = "select id, item_title, item_price

 31:    from store_items where cat_id = $cat_id

 32:     order by item_title";

 33:    $get_items_res = mysql_query($get_items) or die(mysql_error());

 34:

 35:    if (mysql_num_rows($get_items_res) < 1) {

 36:         $display_block = "<P><em>Sorry, no items in

 37:          this category.</em></p>";

 38:     } else {

 39:

 40:         $display_block .= "<ul>";

 41:

 42:         while ($items = mysql_fetch_array($get_items_res)) {

 43:             $item_id = $items[id];

 44:             $item_title = stripslashes($items[item_title]);

 45:             $item_price = $items[item_price];

 46:

 47:             $display_block .= "<li><a

 48:              href=\"showitem.php?item_id=$item_id\">$item_title</a>

 49:              </strong> (\$$item_price)";

[U2]  50:         }

 51:

 52:         $display_block .= "</ul>";

 53:    }

 54: }

 55:     }

 56: }

 57: ?>

 58: <HTML>

 59: <HEAD>

 60: <TITLE>My Categories</TITLE>

 61: </HEAD>

 62: <BODY>

 63: <? print $display_block; ?>

 64: </BODY>

 65: </HTML>

16、PHP串連Access:

   <? 
$dbc=new com("adodb.connection"); 
$dbc->open("driver=microsoft access driver (*.mdb);dbq=c:\member.mdb"); 
$rs=$dbc->execute("select * from tablename"); 
$i=0; 
while (!$rs->eof){ 
$i+=1 
$fld0=$rs->fields["UserName"]; 
$fld0=$rs->fields["Password"];
.... 
echo "$fld0->value $fld1->value ...."; 
$rs->movenext(); 

$rs->close(); 
?>



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

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

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