PHP基础学习笔记.docx

上传人:小飞机 文档编号:3163687 上传时间:2023-03-11 格式:DOCX 页数:18 大小:41.29KB
返回 下载 相关 举报
PHP基础学习笔记.docx_第1页
第1页 / 共18页
PHP基础学习笔记.docx_第2页
第2页 / 共18页
PHP基础学习笔记.docx_第3页
第3页 / 共18页
PHP基础学习笔记.docx_第4页
第4页 / 共18页
PHP基础学习笔记.docx_第5页
第5页 / 共18页
亲,该文档总共18页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《PHP基础学习笔记.docx》由会员分享,可在线阅读,更多相关《PHP基础学习笔记.docx(18页珍藏版)》请在三一办公上搜索。

1、PHP基础学习笔记1、PHP片段四种表示形式。标准tags:<?php?>short tags:<?> 需要在php.ini中设置short _open_tag=on,默认是onasp tags: <%>需要在php.ini中设置asp_tags=on,默认是offscript tags:<script language=”php”></script>2、PHP变量及数据类型1)$variable,变量以字母、_开始,不能有空格2)赋值$variable=value;3)弱类型,直接赋值,不需要显示声明数据类型4)基本数据类型:Integer,Doubl

2、e,String,Boolean,Object,Array(数组)5)特殊数据类型:Resourse的引用),Null3、操作符1)赋值操作符:=2)算术操作符:+,-,*,/,%3)连接操作符:. ,无论操作数是什么,都当成String,结果返回String4)Combined Assignment Operators合计赋值操作符:+=,*=,/=,-=,%=,.=5)Automatically Incrementing and Decrementing自动增减操作符:$variable+=1 <=>$variable+;$variable-=1 <=>$variable-

3、,跟c语言一样,先做其他操作,后+或-+$variable,-$variable,先+或-,再做其他操作6)比较操作符:= =,!=,= = =,>=,>,<,<=7)逻辑操作符:| or,&and,xor(当左右两边有且只有一个是true,返回true),!4、注释:单行注释:/ ,#多行注释:/*/5、每个语句以;号结尾,与java相同6、定义常量:define(“CONSTANS_NAME”,value)7、打印语句:print,与c语言相同8、流程控制语句1)if语句:if(expression)/code to excute if expression evaluat

4、es to trueif(expression) else(3)if(expression1)elseif(expression2)else2)swich语句switch ( expression )case result1:/ execute this if expression results in result1break;case result2:/ execute this if expression results in result2break;default:/ execute this if no break statement/ has been encountered h

5、itherto3)?操作符:( expression )?returned_if_expression_is_true:returned_if_expression_is_false;4)while语句: while ( expression )/ do somethingdo/ code to be executed while ( expression );5)for语句:for ( initialization expression; test expression; modification expression ) / code to be executed6)break;conti

6、nue9、编写函数1)定义函数:function function_name($argument1,$argument2,) /形参 /function code here;2)函数调用function_name($argument1,$argument2,); /形参3)动态函数调用:1: <html>2: <head>3: <title>Listing 6.5</title>4: </head>5: <body>6: <?php7: function sayHello /定义函数sayHello8: print hello<br>

7、;9: 10: $function_holder = sayHello;/将函数名赋值给变量$function_holder11: $function_holder;/变量$function_holder成为函数sayHello的引用,调用$function_holder相当于调用sayHello12: ?>13: </body>14: </html>4)变量作用域:全局变量:1: <html>2: <head>3: <title>Listing 6.8</title>4: </head>5: <body>6: <?php

8、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)使用static1: <html>2: <head>3: <title>Listing 6.10</title>4: <

9、/head>5: <body>6: <?php7: 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值为113: print(We build a fine range of widgets<p>);14: numberedHeading(Doodads);/*第一

10、次调用时,打印$num_of_calls值为2,因为变量是static型的,static型是常驻内存的*/15: print(Finest in the world<p>);16: ?>17: </body>18: </html>6)传值和传址:传值:function function_name($argument)1: <html>2: <head>3: <title>Listing 6.13</title>4: </head>5: <body>6: <?php7: function addFive( $num

11、) 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: <?php7: function addFive( &$num ) 8:$num +

12、= 5;/*传递过来的是变量$num的引用,因此改变形参$num的值就是真正改变变量$orignum物理内存中保存的值*/9: 10: $orignum = 10;11: addFive( $orignum );12: print( $orignum );13: ?>14: </body>15: </html>结果:157)创建匿名函数:create_function(string1,string2); create_function是PHP内建函数,专门用于创建匿名函数,接受两个string型参数,第一个是参数列表,第二个是函数的主体1: <html>2: <

13、head>3: <title>Listing 6.15</title>4: </head>5: <body>6: <?php7: $my_anon = create_function( $a, $b, return $a+$b; );8: print $my_anon( 3, 9 );9: / prints 1210: ?>11: </body>12: </html>8)判断函数是否存在:function_exists(function_name),参数为函数名10、用PHP连接MySQL1)连接:&conn=mysql_connec

14、t(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

15、2: / open the connection3: $conn = mysql_connect(localhost, joeuser, somepass);4: / pick the database to use5: mysql_select_db(testDB,$conn);6: / create the SQL statement7: $sql = SELECT * FROM testTable;8: / execute the SQL statement9: $result = mysql_query($sql, $conn) or die(mysql_error);10: /go

16、through each row in the result set and display data11: while ($newArray = mysql_fetch_array($result) 12: / give a name to the fields13: $id = $newArrayid;14: $testField = $newArraytestField;15: /echo the results onscreen16: echo The ID is $id and the text is $testField <br>17: 18: ?>11、接受表单元素

17、:$_POST表单元素名,如<input type=textname=user>$_POSTuser接受url中queryString中值:$_GETqueryString12、转向其他页面:header(Location: );13、字符串操作:1)explode(“-”,str)Java中的splite2)str_replace =>$str1要查找的字符串,$str2用来替换的字符串,$str3从这个字符串开始查找替换3)substr_replace:14、session:1)打开session:session_start; /也可以在php.ini设置session_aut

18、o_start=1,不必再每个script都写这句,但是默认为0,则必须要写。2)给session赋值:$_SESSIONsession_variable_name=$variable;3)访问session:$variable =$_SESSIONsession_variable_name;4)销毁session:session_destroy;15、显示分类的完整例子:1: <?php2: /connect to database3: $conn = mysql_connect(localhost, joeuser, somepass)4: or die(mysql_error);5:

19、 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 first11: $get_cats = select id, cat_title, cat_desc from12: store_categories order by cat_title;13: $get_cats_res = mysq

20、l_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 = $catsid;21:$cat_tit

21、le = strtoupper(stripslashes($catscat_title);22:$cat_desc = stripslashes($catscat_desc);23:24:$display_block .= <p><strong><a25:href=$_SERVERPHP_SELFU1 ?cat_id=$cat_id>$cat_title</a></strong>/点击此url,刷新本页,第28行读取cat_id,显示相应分类的条目26:<br>$cat_desc</p>27:28:if ($_GETcat_id = $

22、cat_id) /选择一个分类,看下面的条目29:/get items30:$get_items = select id, item_title, item_price31:from store_items where cat_id = $cat_id32: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>Sorr

23、y, no items in37:this category.</em></p>38: else 39:40:$display_block .= <ul>41:42:while ($items = mysql_fetch_array($get_items_res) 43:$item_id = $itemsid;44:$item_title = stripslashes($itemsitem_title);45:$item_price = $itemsitem_price;46:47:$display_block .= <li><a48:href=show

24、item.php?item_id=$item_id>$item_title</a>49:</strong> ($item_price);U250: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&g

25、t16、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->fieldsUserName;$fld0=$rs->fieldsPassword;.echo $fld0->value $fld1->value .;$rs->movenext;$rs->close;?>

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号