«
PHP基础学习笔记

时间:2008-5-31    作者:Deri    分类: 分享


   <p>  1、 PHP片段四种表示形式。</p><p>  标准tags:<?php ?></p><p>  short tags:<?   ?> 需要在php.ini中设置short _open_tag=on,默认是on</p><p>  asp tags: <%  %>需要在php.ini中设置asp_tags=on,默认是off</p><p>  script tags:<script language=”php”></script></p><p>  2、 PHP变量及数据类型</p><p>  1)$variable ,变量以字母、_开始,不能有空格</p><p>  2)赋值$variable=value;</p><p>  3)弱类型,直接赋值,不需要显示声明数据类型</p><p>  4)基本数据类型:Integer,Double,String,Boolean,Object(对象或类),Array(数组) </p><p>  5)特殊数据类型:Resourse(对第三方资源(如数据库)的引用),Null(空,未初始化的变量)</p><p>  3、 操作符</p><p>  1)赋值操作符:=</p><p>  2)算术操作符:+,-,*,/,%(取模)</p><p>  3)连接操作符:. ,无论操作数是什么,都当成String,结果返回String</p><p>  4)Combined Assignment Operators合计赋值操作符:+=,*=,/=,-=,%=,.=</p><p>  5)Automatically Incrementing and Decrementing自动增减操作符:</p><p>  (1)$variable+=1 <=>$variable++;$variable-=1 <=>$variable-,跟c语言一样,先做其他操作,后++或-</p><p>  (2)++$variable,-$variable,先++或-,再做其他操作</p><p>  6)比较操作符:= =(左边等于右边),!=(左边不等于右边),= = =(左边等于右边,且数据类型相同),>=,>,<,<=</p><p>  7)逻辑操作符:|| ó or,&&óand,xor(当左右两边有且只有一个是true,返回true),!</p><p>  4、 注释:</p><p>  单行注释:// ,#</p><p>  多行注释:/* */</p>
<p> </p>

   <p>  5、 每个语句以;号结尾,与java相同</p><p>  6、 定义常量:define(“CONSTANS_NAME”,value)</p><p>  7、 打印语句:print,与c语言相同</p><p>  8、 流程控制语句</p><p>  1)if语句:</p><p>  (1)if(expression)</p><code>{<br />  //code to excute if expression evaluates to true<br />}</code><p>  (2)if(expression)</p><code>   {<br />   }<br />   else<br />   {<br />   }</code><p>  (3)if(expression1)</p><code>{<br />}<br />elseif(expression2)<br />{<br />}<br />else<br />{<br />}</code><p>  2)swich语句</p><code>switch ( expression )<br />{<br />  case result1:<br />    // execute this if expression results in result1<br />    break;<br />  case result2:<br />    // execute this if expression results in result2<br />    break;<br />  default:<br />   // execute this if no break statement<br />   // has been encountered hitherto<br />}</code><p>  3)?操作符:</p><p>  ( expression )?returned_if_expression_is_true:returned_if_expression_is_false;</p><p>  4)while语句:</p><p>  (1) while ( expression )</p><code>{<br />   // do something<br />}</code></p><p>  (2)do</p><code> {<br /> // code to be executed<br />} while ( expression );</code><p>  5)for语句:</p><code>  for ( initialization expression; test expression; modification expression ) {<br /> // code to be executed<br />}</code><p>  6)break;continue</p><p>  9、 编写函数</p><p>  1)定义函数:</p><code>function function_name($argument1,$argument2,……) //形参<br />{<br />  //function code here;<br />}</code><p>  2)函数调用</p>
 <p> </p>

   <p>  function_name($argument1,$argument2,……); //形参</p><p>  3)动态函数调用(Dynamic Function Calls):</p><code> 1: <html><br /> 2: <head><br /> 3: <title>Listing 6.5</title><br /> 4: </head><br /> 5: <body><br /> 6: <?php<br /> 7: function sayHello() {  //定义函数sayHello<br /> 8:   print "hello<br>";<br /> 9: }<br />10: $function_holder = "sayHello"; //将函数名赋值给变量$function_holder<br />11: $function_holder(); //变量$function_holder成为函数sayHello的引用,调用$function_holder()相当于调用sayHello<br />12: ?><br />13: </body><br />14: </html></code><p>  4)变量作用域:</p><p>  全局变量:</p><code> 1: <html><br /> 2: <head><br /> 3: <title>Listing 6.8</title><br /> 4: </head><br /> 5: <body><br /> 6: <?php<br /> 7: $life=42;<br /> 8: function meaningOfLife() {<br />9:global $life; <br />/*在此处重新声明$life为全局变量,在函数内部访问全局变量必须这样,如果在函数内改变变量的值,将在所有代码片段改变*/<br />10:   print "The meaning of life is $life<br>";<br />11: }<br />12: meaningOfLife();<br />13: ?><br />14: </body><br />15: </html></code><p>  5)使用static</p><code> 1: <html><br /> 2: <head><br /> 3: <title>Listing 6.10</title><br /> 4: </head><br /> 5: <body><br /> 6: <?php<br /> 7: function numberedHeading( $txt ) {<br /> 8:   static $num_of_calls = 0;<br /> 9:   $num_of_calls++;<br />10:   print "<h1>$num_of_calls. $txt</h1>";<br />11: }<br />12: numberedHeading("Widgets"); //第一次调用时,打印$num_of_calls值为1<br />13: print("We build a fine range of widgets<p>"); <br />14: numberedHeading("Doodads"); /*第一次调用时,打印$num_of_calls值为2,因为变量是static型的,static型是常驻内存的*/<br />15: print("Finest in the world<p>");<br />16: ?><br />17: </body><br />18: </html></code><p>  6)传值(value)和传址(reference):</p>
 <p> </p>

   <p>  传值:function function_name($argument)</p><code> 1: <html><br /> 2: <head><br /> 3: <title>Listing 6.13</title><br /> 4: </head><br /> 5: <body><br /> 6: <?php<br /> 7: function addFive( $num ) {<br /> 8:   $num += 5;<br /> 9: }<br />10: $orignum = 10;<br />11: addFive( &$orignum );<br />12: print( $orignum );<br />13: ?><br />14: </body><br />15: </html></code><p>  结果:10</p><p>  传址:funciton function_name(&$argument)</p><code> 1: <html><br /> 2: <head><br /> 3: <title>Listing 6.14</title><br /> 4: </head><br /> 5: <body><br /> 6: <?php<br /> 7: function addFive( &$num ) {<br /> 8:   $num += 5; /*传递过来的是变量$num的引用,因此改变形参$num的值就是真正改变变量$orignum物理内存中保存的值*/<br /> 9: }<br />10: $orignum = 10;<br />11: addFive( $orignum );<br />12: print( $orignum );<br />13: ?><br />14: </body><br />15: </html></code><p>  结果:15</p><p>  7)创建匿名函数:create_function(‘string1’,’string2’); create_function是PHP内建函数,专门用于创建匿名函数,接受两个string型参数,第一个是参数列表,第二个是函数的主体</p><code> 1: <html><br /> 2: <head><br /> 3: <title>Listing 6.15</title><br /> 4: </head><br /> 5: <body><br /> 6: <?php<br /> 7: $my_anon = create_function( '$a, $b', 'return $a+$b;' );<br /> 8: print $my_anon( 3, 9 );<br /> 9: // prints 12<br />10: ?><br />11: </body><br />12: </html></code><p>  8)判断函数是否存在:function_exists(function_name),参数为函数名</p>
 <p> </p>

   <p>  10、用PHP连接MySQL</p><p>  1)连接:&conn=mysql_connect("localhost", "joeuser", "somepass");</p><p>  2)关闭连接:mysql_close($conn);</p><p>  3) 数据库与连接建立联系:mysql_select_db(database name, connection index);</p><p>  4) 将SQL语句给MySQL执行:$result = mysql_query($sql, $conn); //增删改查都是这句</p><p>  5) 检索数据:返回记录数:$number_of_rows = mysql_num_rows($result);</p><p>  将记录放入数组:$newArray = mysql_fetch_array($result);</p><p>  例子:</p><code> 1: <?php<br /> 2: // open the connection<br /> 3: $conn = mysql_connect("localhost", "joeuser", "somepass");<br /> 4: // pick the database to use<br /> 5: mysql_select_db("testDB",$conn);<br /> 6: // create the SQL statement<br /> 7: $sql = "SELECT * FROM testTable";<br /> 8: // execute the SQL statement<br /> 9: $result = mysql_query($sql, $conn) or die(mysql_error());<br />10: //go through each row in the result set and display data<br />11: while ($newArray = mysql_fetch_array($result)) {<br />12:   // give a name to the fields<br />13:   $id = $newArray['id'];<br />14:   $testField = $newArray['testField'];<br />15:   //echo the results onscreen<br />16:   echo "The ID is $id and the text is $testField <br>";<br />17: }<br />18: ?></code><p>  11、接受表单元素:$_POST[表单元素名],</p><p>  如<input type=text name=user>ó$_POST[user]</p>
 <p> </p>

   <p>  接受url中queryString中值(GET方式):$_GET[queryString]</p><p>  12、转向其他页面:header("Location: http://www.webjx.com");</p><p>  13、字符串操作:</p><p>  1)explode(“-”,str)óJava中的splite</p><p>  2)str_replace($str1,$str2,$str3) =>$str1要查找的字符串,$str2用来替换的字符串,$str3从这个字符串开始查找替换</p><p>  3)substr_replace:</p><p>  14、session:</p><p>  1)打开session:session_start(); //也可以在php.ini设置session_auto_start=1,不必再每个script都写这句,但是默认为0,则必须要写。</p><p>  2)给session赋值:$_SESSION[session_variable_name]=$variable;</p><p>  3)访问session:$variable =$_SESSION[session_variable_name];</p><p>  4)销毁session:session_destroy();</p><p>  15、显示分类的完整例子:</p><code>1: <?php<br /> 2: //connect to database<br /> 3: $conn = mysql_connect("localhost", "joeuser", "somepass")<br /> 4:   or die(mysql_error());<br /> 5: mysql_select_db("testDB",$conn) or die(mysql_error());<br /> 6:<br /> 7: $display_block = "<h1>My Categories</h1><br /> 8: <P>Select a category to see its items.</p>";<br /> 9:<br />10: //show categories first<br />11: $get_cats = "select id, cat_title, cat_desc from<br />12:   store_categories order by cat_title";<br />13: $get_cats_res = mysql_query($get_cats) or die(mysql_error());<br />14:<br />15: if (mysql_num_rows($get_cats_res) < 1) { //如果返回记录行数小于1,则说明没有分类<br />16:  $display_block = "<P><em>Sorry, no categories to browse.</em></p>";<br />17: } else {<br />18:<br />19:  while ($cats = mysql_fetch_array($get_cats_res)) { //将记录放入变量$cats中<br />20:$cat_id = $cats[id];<br />21:$cat_title = strtoupper(stripslashes($cats[cat_title]));<br />22:$cat_desc = stripslashes($cats[cat_desc]);<br />23:<br />24:$display_block .= "<p><strong><a<br />25:href="$_SERVER[PHP_SELF][U1] ?cat_id=$cat_id">$cat_title</a></strong>//点击此url,刷新本页,第28行读取cat_id,显示相应分类的条目<br />26:<br>$cat_desc</p>";<br />27:<br />28:if ($_GET[cat_id] == $cat_id) { //选择一个分类,看下面的条目<br />29:  //get items<br />30:  $get_items = "select id, item_title, item_price<br />31:  from store_items where cat_id = $cat_id<br />32:  order by item_title";<br />33:  $get_items_res = mysql_query($get_items) or die(mysql_error());<br />34:<br />35:  if (mysql_num_rows($get_items_res) < 1) {<br />36:    $display_block = "<P><em>Sorry, no items in<br />37:     this category.</em></p>";<br />38:  } else {<br />39:<br />40:    $display_block .= "<ul>";<br />41:<br />42:    while ($items = mysql_fetch_array($get_items_res)) {<br />43:      $item_id = $items[id];<br />44:      $item_title = stripslashes($items[item_title]);<br />45:      $item_price = $items[item_price];<br />46:<br />47:      $display_block .= "<li><a<br />48:       href="showitem.php?item_id=$item_id">$item_title</a><br />49:       </strong> ($$item_price)";<br />[U2] 50:    }<br />51:<br />52:    $display_block .= "</ul>";<br />53:  }<br />54:}<br />55:   }<br />56: }<br />57: ?><br />58: <HTML><br />59: <HEAD><br />60: <TITLE>My Categories</TITLE><br />61: </HEAD><br />62: <BODY><br />63: <? print $display_block; ?><br />64: </BODY><br />65: </HTML></code><p>  16、PHP连接Access:</p></p><code><? <br />$dbc=new com("adodb.connection"); <br />$dbc->open("driver=microsoft access driver (*.mdb);dbq=c:member.mdb"); <br />$rs=$dbc->execute("select * from tablename"); <br />$i=0; <br />while (!$rs->eof){ <br />$i+=1 <br />$fld0=$rs->fields["UserName"]; <br />$fld0=$rs->fields["Password"];<br />.... <br />echo "$fld0->value $fld1->value ...."; <br />$rs->movenext(); <br />} <br />$rs->close(); <br />?></code></p></p>