|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
怎么培养啊 别光说不练啊,好 ,比如新人入门自己步是配置环境,虽然现在都有很多的集成环境,但是真实的体验下配置环境还是会有很多帮助,不论是你以后工作还是在真实的linux下开发。 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)根基数据类型: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();
?>
不过语法好学,但是怎么用语法来实现每个人都有每个人的方式,几乎是各有千秋。然而借鉴别人成功的代码,绝对是有益无害,因此,多看那些经过千锤百炼凝出来的经典代码,是进阶的最好方法。 |
|