设为首页 友情链接
在线留言 发表文章
加入收藏 广告联系

刺猬首页

| 专案技术 | 网络技术 | 图形图象 | 网络编程 | 网页设计 | 操作系统 | 服务器 | 技术白皮书 | 在线实验室 | 刺猬论坛 |
小说专版  | 数据库 | 设计赏析 | 存储频道 | 网络安全 | 私服架设 |  Solaris | 网站评估 | PC维护技巧 | 下载中心 | 博 客 |
专   题: | Linux | java | cisco | 防病毒 | 刀片 | SOA | iscsi | ASP.NET | SQL | Oracle |
您现在的位置: IT公社 IT community >> 网络编程 >> PHP >> 教程正文 用户登录 新用户注册
专 题 栏 目
最 新 热 门
最 新 推 荐
相 关 文 章
PHP has encountered an…
PHP中for循环语句的几种…
为什么PHP令人不爽(对于…
如何利用PHP和CSS改变网…
PHP程序与服务器端通讯的…
PHPUnit袖珍指南之自动测…
用php或js获取图片大小,…
还是说php实现singleton…
PHP实现简单线性回归之数…
使用PHP的Socket写的POP…
  PHP基础学习笔记         
PHP基础学习笔记
 

 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:

[1] [2] 下一页

频道声明:本频道的文章除部分特别声明禁止转载的专稿外,可以自由转载.但请务必注明出出处和原始作者 文章版权归本频道与文章作者所有.对于被频道转载文章的个人和网站,我们表示深深的谢意。

原始作者:佚名 录入时间:2007-1-3 3:33:15
信息来源:不详 投稿信箱:itqoo@126.com
教程录入:itqoo    责任编辑:itqoo 
  • 上一个教程:

  • 下一个教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    - 关于我们 - 合作伙伴 - 友情链接 - 广告刊登 - 投稿热线 - 在线留言版权声明联系方式 -
    IT公社版权所有 粤ICP备05127012号
    Copyrigh@2005-2006 itqoo.com.Inc All Rights Reserved  推荐分辨率 1024*768
    联系站长:E-Mail:itqoo@126.com     MSN:urchincc@hotmail.com    QQ:点击这里给我发消息
    特别感谢:亿太网络提供空间支持