|
1、 PHP片段四種表示形式。 標(biāo)準(zhǔn)tags:<?php ?> short tags:<? ?> 需要在php.ini中設(shè)置short _open_tag=on,默認(rèn)是on asp tags: <% %>需要在php.ini中設(shè)置asp_tags=on,默認(rèn)是off script tags:<script language=”php”></script> 2、 PHP變量及數(shù)據(jù)類型 1) $variable ,變量以字母、_開(kāi)始,不能有空格 2) 賦值$variable=value; 3) 弱類型,直接賦值,不需要顯示聲明數(shù)據(jù)類型 4) 基本數(shù)據(jù)類型:Integer,Double,String,Boolean,Object(對(duì)象或類),Array(數(shù)組) 5) 特殊數(shù)據(jù)類型:Resourse(對(duì)第三方資源(如數(shù)據(jù)庫(kù))的引用),Null(空,未初始化的變量) 3、 操作符 1) 賦值操作符:= 2) 算術(shù)操作符:+,-,*,/,%(取模) 3) 連接操作符:. ,無(wú)論操作數(shù)是什么,都當(dāng)成String,結(jié)果返回String 4) Combined Assignment Operators合計(jì)賦值操作符:+=,*=,/=,-=,%=,.= 5) Automatically Incrementing and Decrementing自動(dòng)增減操作符: (1)$variable+=1 <=>$variable++;$variable-=1 <=>$variable-,跟c語(yǔ)言一樣,先做其他操作,后++或- (2)++$variable,-$variable,先++或-,再做其他操作 6) 比較操作符:= =(左邊等于右邊),~=(左邊不等于右邊),= = =(左邊等于右邊,且數(shù)據(jù)類型相同),>=,>,<,<= 7) 邏輯操作符:|| ó or,&&óa(chǎn)nd,xor(當(dāng)左右兩邊有且只有一個(gè)是true,返回true),! 4、 注釋: 單行注釋:// ,# 多行注釋:/* */ 5、 每個(gè)語(yǔ)句以;號(hào)結(jié)尾,與java相同 6、 定義常量:define(“CONSTANS_NAME”,value) 7、 打印語(yǔ)句:print,與c語(yǔ)言相同 8、 流程控制語(yǔ)句 1) if語(yǔ)句: (1)if(expression) { //code to excute if expression evaluates to true } (2)if(expression) { } else { } (3)if(expression1) { } elseif(expression2) { } else { } 2) swich語(yǔ)句 switch ( expression ) { case result // execute this if expression results in result1 break; case result // 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語(yǔ)句: (1) while ( expression ) { // do something } (2)do { // code to be executed } while ( expression ); 5) for語(yǔ)句: for ( initialization expression; test expression; modification expression ) { // code to be executed } 6) break;continue 9、 編寫函數(shù) 1) 定義函數(shù): function function_name($argument1,$argument2,??) //形參 { //function code here; } 2) 函數(shù)調(diào)用 function_name($argument1,$argument2,??); //形參 3) 動(dòng)態(tài)函數(shù)調(diào)用(Dynamic Function Calls): <h<head> <title>Listing 6.5</title> </head> <body> <?php function sayHello() { //定義函數(shù)sayHello print "hello<br>"; } $function_holder = "sayHello"; //將函數(shù)名賦值給變量$function_holder $function_holder(); //變量$function_holder成為函數(shù)sayHello的引用,調(diào)用 $function_holder()相當(dāng)于調(diào)用sayHello ?> </body> </html> 4) 變量作用域: 全局變量: <html> <head> <title>Listing 6.8</title> </head> <body> <?php $life=42; function meaningOfLife() { global $life; /*在此處重新聲明$life為全局變量,在函數(shù)內(nèi)部訪問(wèn)全局變量必須這樣,如果在函數(shù)內(nèi)改 變變量的值,將在所有代碼片段改變*/ print "The meaning of life is $life<br>"; } meaningOfLife(); ?> </body> </html> 5) 使用static <html> <head> <title>Listing 6.10</title> </head> <body> <?php function numberedHeading( $txt ) { static $num_of_calls = 0; $num_of_calls++; print "<h1>$num_of_calls. $txt</h1>"; } numberedHeading("Widgets"); //第一次調(diào)用時(shí),打印$num_of_calls值為1 print("We build a fine range of widgets<p>"); numberedHeading("Doodads"); /*第一次調(diào)用時(shí),打印$num_of_calls值為2,因?yàn)樽兞渴?/p> static型的,static型是常駐內(nèi)存的*/ print("Finest in the world<p>"); ?> </body> </html> 6) 傳值(value)和傳址(reference): 傳值:function function_name($argument) <html> <head> <title>Listing 6.13</title> </head> <body> <?php function addFive( $num ) { $num += 5; } $orignum = 10; addFive( &$orignum ); print( $orignum ); ?> </body> </html> 結(jié)果:10 傳址:funciton function_name(&$argument) <html> <head> <title>Listing 6.14</title> </head> <body> <?php function addFive( &$num ) { $num += 5; /*傳遞過(guò)來(lái)的是變量$num的引用,因此改變形參$num的值就是真正改變變量 $orignum物理內(nèi)存中保存的值*/ } $orignum = 10; addFive( $orignum ); print( $orignum ); ?> </body> </html> 結(jié)果:15 7) 創(chuàng)建匿名函數(shù):create_function(‘string1’,’string2’); create_function是PHP內(nèi)建函數(shù),專門用于創(chuàng)建匿名函數(shù),接受兩個(gè)string型參數(shù),第一個(gè)是參數(shù)列表,第 二個(gè)是函數(shù)的主體 <html> <head> <title>Listing 6.15</title> </head> <body> <?php $my_anon = create_function( '$a, $b', 'return $a+$b;' ); print $my_anon( 3, 9 ); // prints 12 ?> </body> </html> 8) 判斷函數(shù)是否存在:function_exists(function_name),參數(shù)為函數(shù)名 10、 用PHP連接MySQL 1) 連接:&conn=mysql_connect("localhost", "joeuser", "somepass"); 2) 關(guān)閉連接:mysql_close($conn); 3) 數(shù)據(jù)庫(kù)與連接建立聯(lián)系:mysql_select_db(database name, connection index); 4) 將SQL語(yǔ)句給MySQL執(zhí)行:$result = mysql_query($sql, $conn); //增刪改查都是這 句 5) 檢索數(shù)據(jù):返回記錄數(shù):$number_of_rows = mysql_num_rows($result); 將記錄放入數(shù)組:$newArray = mysql_fe 例子: <?php // open the connection $conn = mysql_connect("localhost", "joeuser", "somepass"); // pick the database to use mysql_select_db("testDB",$conn); // create the SQL statement $sql = "SELECT * FROM testTable"; // execute the SQL statement $result = mysql_query($sql, $conn) or die(mysql_error()); //go through each row in the result set and display data while ($newArray = mysql_fetch_array($result)) { // give a name to the fields $id = $newArray['id']; $testField = $newArray['testField']; //echo the results onscreen echo "The ID is $id and the text is $testField <br>"; } ?> 11、 接受表單元素:$_POST[表單元素名], 如<input type=text name=user>ó$_POST[user] 接受url中queryString中值(GET方式):$_GET[queryString] 12、轉(zhuǎn)向其他頁(yè)面:header("Location: "); 13、字符串操作: 1)explode(“-”,str)óJava中的splite 2)str_replace($str1,$str2,$str3) =>$str1要查找的字符串,$str2用來(lái)替換的字符串, $str3從這個(gè)字符串開(kāi)始查找替換 3)substr_replace: 14、session: 1)打開(kāi)session:session_start(); //也可以在php.ini設(shè)置session_auto_start=1,不 必再每個(gè)script都寫這句,但是默認(rèn)為0,則必須要寫。 2)給session賦值:$_SESSION[session_variable_name]=$variable; 3)訪問(wèn)session:$variable =$_SESSION[session_variable_name]; 4)銷毀session:session_destroy(); 15、顯示分類的完整例子: <?php //connect to database $conn = mysql_connect("localhost", "joeuser", "somepass") or die(mysql_error()); mysql_select_db("testDB",$conn) or die(mysql_error()); $display_block = "<h1>My Categories</h1> <P>Select a category to see its items.</p>"; //show categories first $get_cats = "select id, cat_title, cat_desc from store_categories order by cat_title"; $get_cats_res = mysql_query($get_cats) or die(mysql_error()); if (mysql_num_rows($get_cats_res) < 1) { //如果返回記錄行數(shù)小于1,則說(shuō)明沒(méi)有分類 $display_block = "<P><em>Sorry, no categories to browse.</em></p>"; } else { while ($cats = mysql_fetch_array($get_cats_res)) { //將記錄放入變量$cats中 $cat_id = $cats[id]; $cat_title = strtoupper(stripslashes($cats[cat_title])); $cat_desc = stripslashes($cats[cat_desc]); $display_block .= "<p><strong><a href="$_SERVER[PHP_SELF][U1] ?cat_id=$cat_id">$cat_title</a></strong>//點(diǎn)擊此url,刷新本頁(yè),第28行讀取cat_id,顯示相應(yīng)分類的條目 <br>$cat_desc</p>"; if ($_GET[cat_id] == $cat_id) { //選擇一個(gè)分類,看下面的條目 //get items $get_items = "select id, item_title, item_price from store_items where cat_id = $cat_id order by item_title"; $get_items_res = mysql_query($get_items) or die(mysql_error()); if (mysql_num_rows($get_items_res) < 1) { $display_block = "<P><em>Sorry, no items in this category.</em></p>"; } els$display_block .= "<ul>"; while ($items = mysql_fetch_array($get_items_res)) { $item_id = $items[id]; $item_title = stripslashes($items[item_title]); $item_price = $items[item_price]; $display_block .= "<li><a href="showitem.php?item_id=$item_id">$item_title</a> </strong> ($$item_price)"; [U2] } $display_block .= "</ul>"; } } } } ?> <HTML> <HEAD> <TITLE>My Categories</TITLE> </HEAD> <BODY> <? print $display_block; ?> </BODY> </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(); ?>
信息發(fā)布:廣州名易軟件有限公司 http://m.jetlc.com
|